Skip to content
Advertisement

Add a call to PHP in a jQuery button with Ajax

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.

Advertisement

Answer

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)

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement