ChrisBuck
ChrisBuck

Reputation: 102

How do I get a $_POST value in a PHP file using Javascript and AJAX?

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
}

Upvotes: 0

Views: 1489

Answers (2)

Robert Van Sant
Robert Van Sant

Reputation: 1507

try setting this:

var data = 'removeall=yes';

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

data: data,

and see if this does the trick.

Upvotes: 1

Hkachhia
Hkachhia

Reputation: 4539

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
        }
    });

Upvotes: 0

Related Questions