3

I created a custom table in wordpress that manages a vocabulary list for each user. This list appears as a table in the browser. I added a button so that the user can remove vocabulary from their list. For that I use a jquery script. I retrieve the user's id and the id of the selected vocabulary that I pass on to my function thanks to ajax. This function must remove the data stored in the database.

it seems to me that my variables are well transmitted to my function but it doesn't work. Can anyone explain to me what my mistake is? And how to check if the variables are transmitting well. here my code in "vocabulary.js"

    jQuery('#button').click( function () {

     var vocabulary = jQuery('.selected').children("input").attr("id");
     var user = jQuery('#id_user').attr("name");

        jQuery.ajax({
        url: removeVoca.ajax_url,
        type:'post',
        data : {
            action : 'renove_vocabulary',
            id_vocabulary : vocabulary,
            id_user : user
        },
        success : function(reponse){

          //delete the row
          table.row('.selected').remove().draw( false );
        },
        error: function(xhr){
           //error handling
           console.log("suppression echoué")
        }});

    } );
} );

Here my fonction in ajax-vocabulary.php

add_action( 'wp_ajax_renove_vocabulary', 'renove_vocabulary' );

function renove_vocabulary() {

$id_user = $_POST['user_id'];
$id_vacabulary = $_POST['vocabulary_id'];


global $wpdb;

$reponse = $wpdb-> delete ('wp_user_vocabulary', array ('vocabulary_id' => $id_vacabulary , 'user_id' => $id_user ), array('%d', '%d'));


wp_die();

}

I checked with the debugger. the variables seem to be sent

enter image description here

3
  • you can use the network monitor to debug the AJAX call, e.g. with firefox : developer.mozilla.org/en-US/docs/Tools/Network_Monitor
    – mmm
    Commented Jan 4, 2018 at 12:31
  • Try Adding this add_action('wp_ajax_nopriv_renove_vocabulary','renove_vocabulary'); keep both actions.
    – DHL17
    Commented Jan 4, 2018 at 13:37
  • I added it, but there is no change. Commented Jan 4, 2018 at 17:41

1 Answer 1

2

Assuming that your script localization and other stuff are set up properly, there is an issue in your back-end script. Your data is in the following format:

data : {
    action : 'renove_vocabulary',
    id_vocabulary : vocabulary,
    id_user : user
}

But you are trying to get the data as follows:

$id_user       = $_POST['user_id'];
$id_vacabulary = $_POST['vocabulary_id'];

These 2 should be changed to match the above. So, change them into:

$id_user       = $_POST['id_user'];
$id_vacabulary = $_POST['id_vocabulary'];

Also, don't forget to use nonces, sanitize the inputs and do some security checks. I would recommend using the REST-API altogether.

2
  • Thank you for your help. I didn't see it this ridiculous mistake. I've modified the variable names and added nonce. everything is ok now. but what do you mean by "sanitize the inputs and do some security checks". can you give an example ? Commented Jan 5, 2018 at 16:32
  • You're welcome. To add extra layers of security, check the nonces and sanitation function. Everything is provided in the links.
    – Johansson
    Commented Jan 5, 2018 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.