0

I tried to modify the behaviour of the Save button in my DokuWiki (see my post on their forum without answer yet).

I want to call a specific file.php when the user hits the Save Button, i.e. every time there is an edit in the wiki. The PHP file recalculates all the connections in the Wiki and creates a new network diagram of the links in-between pages using Rscript.

I think the best way to do that is to use Ajax when the button is clicked (see this answer about how to do that). This is the original jQuery code of the button in DokuWiki:

// reset change memory var on submit
jQuery('#edbtn__save').on('click',
    function() {
        window.onbeforeunload = '';
        textChanged = false;
    }
);

I would like to add something like:

jQuery('#edbtn__save').on('click',
    function() {
        window.onbeforeunload = '';
        textChanged = false;
        $.ajax({
          url: "./data/forms/file.php",
          }).done(function( msg ) {
            alert( "Data Saved: " + msg );
          });
        }
);

But this is not working yet (I tried different URLs too, but it is not clear what to use as I run DokuWiki on my localhost for the moment). I never studied jQuery nor Ajax, so this is new to me. I don't really understand how it works, but it might just need a simple fix.

This is what the file.php looks like:

<?php
include("./data/forms/myFunctions.php");
matrixer();
 ?>

PS: the documentation I found about the function on() and the DokuWiki source code.

  • Remove the `.` from the url in your jQuery code. – Harpreet Singh Mar 27 '21 at 22:00
  • Also, I highly doubt that your the url is correct in your jQuery code. So, open the console, check the console when you click the submit button. See someone kind of error should be coming up there. – Harpreet Singh Mar 27 '21 at 22:02
  • A POST request is useless without the data to send – charlietfl Mar 27 '21 at 22:26
  • @HarpreetSingh, yes, I get the error ; `Cannot read property 'ajax' of undefined.` for the moment (related posts [one](https://stackoverflow.com/q/31760159/15473629) and [two](https://stackoverflow.com/questions/23588941/uncaught-typeerror-cannot-read-property-ajax-of-undefined)). I will try different modifications. Thanks – FractalCitta Mar 28 '21 at 06:42
  • @charlietfl, Yep, I don't need any data, just to run the file.php. I removed it from my question. Thanks – FractalCitta Mar 28 '21 at 06:44

1 Answers1

0

I found a very useful link this morning. from this I was able to work it out.

So the proper code is :

jQuery('#edbtn__save').on('click',
    function() {
        window.onbeforeunload = '';
        textChanged = false;
        jQuery.ajax({
          type: 'POST',
          url: '/~User/dokuwiki/data/forms/file.php',
          success: function(data) {
              alert(data);
              $("p").text(data);
            }
        });
    }

It works ! Make sure the file is accessible via browser to avoid Error 403 (Forbidden)