0

UPDATE 2/28/2012: This question has a solution at the end of it, thanks to @charlietfl.

I have a form and an AJAX call within some JQuery script, and the AJAX appears to be executing successfully, however, the $_POST variable in the PHP file is still empty. Not sure what I'm doing wrong. My code is commented below.

The main question concerns the PHP file. Why is the PHP $_POST variable not set to 'yes? If I do a var_dump, it consistently shows NULL. However, I believe I am manually setting 'removeall' to an arbitrary value, in this case 'yes', using the typical AJAX method. Shouldn't the PHP file be picking up the $_POST variable with a label of 'removeall' as being set to 'yes'?

I hope whatever I'm doing wrong will be completely obvious to someone.

Javascript:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<script type="text/javascript">
    $(document).ready(function(){
        $('tr.assoc_row').show();
        $('#settings-removed-msg').hide();
        $('#new-assoc-msg').hide();
        $('#formdeleteassoc').submit(function(e){
            e.preventDefault(); //This is working to prevent normal submission of the form.

            $.ajax ({
                type: 'POST',
                url: '<?php echo $cb_t2c_remove_all_url; ?>', //I have checked this to make sure it is the correct url for the PHP file.
                data: {removeall: 'yes' //This is the data that is NOT getting passed to the PHP file.
                },
                success: function() {
                    $('#settings-removed-msg').fadeIn('fast'); //This gets triggered.
                    $('tr.assoc_row').fadeOut('fast'); //This gets triggered
                    }
            });
        });
    });

PHP Code:

<?php
//remove_all.php

global $wpdb;
$prefix = $wpdb->prefix;

$remove_var_dump = $_POST['removeall']; //returning NULL
var_dump($remove_var_dump); //returning NULL

if ( $_POST['removeall'] == 'yes' ) {
    echo 'This was set to yes, everything is working.';
    }
else {
    echo 'This was not set to yes, it is still not working.';
}

?>

Solution:

/*
JQUERY action processed by AJAX
*/

add_action('init', 'cb_t2c_action_javascript');

function cb_t2c_action_javascript() {
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
    $('tr.assoc_row').show();
    $('#settings-removed-msg').hide();
    $('#new-assoc-msg').hide();
    $('#formdeleteassoc').submit(function(e){
        e.preventDefault(); //Works to prevent normal submission of the form.

        var data = {
            action: 'cb_t2c_ajax_action',
            removeall: 'yes'
            };

        // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php

        $.ajax ({
                type: 'POST',
                url: ajaxurl,
                data: data,
                success: function() {
                    $('#settings-removed-msg').fadeIn('fast'); //Working now
                    $('tr.assoc_row').fadeOut('fast'); //Working now
                    }
            });

        $('#formsavesettings').submit(function(){
            $('#new-assoc-msg').fadeIn('fast'); //Working now
            });
    });
});
</script>
<?php   
}
//Add the action to process the AJAX.
add_action('wp_ajax_cb_t2c_ajax_action', 'cb_t2c_action_callback');
add_action('wp_ajax_nopriv_cb_t2c_ajax_action', 'cb_t2c_action_callback');

function cb_t2c_action_callback() {
    global $wpdb; // this is how you get access to the database
    $prefix = $wpdb->prefix;

    $remove_var_dump = $_POST['removeall']; //returning NULL
    var_dump($remove_var_dump);
    $removeall = $_POST['removeall'];

    if ( isset($removeall) ){
    $wpdb->query("DELETE FROM wp_cb_tags2cats");
    }

    die(); // this is required to return a proper result
}
6
  • What does your packet sniffer say? Commented Feb 28, 2012 at 5:18
  • how are you checking the outpit of remove_all.php? Commented Feb 28, 2012 at 5:20
  • Somesh, I'm actually testing by using something other than the echo, it's a simple action on the database. E.g., if ( $_POST['removeall'] == 'yes' ){ $wpdb->query("DELETE FROM ".$prefix."cb_tags2cats"); } If the removeall variable is set to 'yes', then it should delete all the data from the particular table. This works with using a form that posts to the server, but for other reasons, I need the $_POST variable to be sent via AJAX to another PHP file. Commented Feb 28, 2012 at 5:40
  • Also, not sure if it's relevant, but the JQuery won't execute the success function using either 'remove_all.php' or '/remove_all.php'. It only works with the url entered as a PHP variable. Commented Feb 28, 2012 at 5:46
  • You should work through the wordpress ajax API. You could well be running into wordpress securtiy issues by including only part of the framework EDIT- you could check this theory by not including the $wpdb references and dump the POST back to see what gets returned then Commented Feb 28, 2012 at 6:24

2 Answers 2

1

try setting this:

var data = 'removeall=yes';

and setting this in your $.ajax({})

data: data,

and see if this does the trick.

Sign up to request clarification or add additional context in comments.

3 Comments

was this helpful? also, @Harry_F1 will work as well, but his ajax submission type is set to 'get' and should be changed to 'post' to match your backend code. in my example, i just tried to abstract the value.
This was definitely helpful, hence the +1. The Wordpress API structures the data variable similar to this. Thinking about putting the data into a 'data' variable got me part of the way there. Appreciate it.
np, i'm glad i could help :) do you need any other assistance?
0

I have modify your ajax function please try it.

jQuery.ajax({
        type:"GET",
        cache:false,
        url:'display_alert_msg.php',
         data: 'removeall=yes',
        success:function(html){ 
             $('#settings-removed-msg').fadeIn('fast'); //This gets triggered.
             $('tr.assoc_row').fadeOut('fast'); //This gets triggered
        }
    });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.