Trekdrop
Trekdrop

Reputation: 495

Can't echo Ajax variable pass

I'm passing 2 variables with ajax to the same .php file. I see them in the console.log but I can't seem to echo them. I already tried several things I found in other questions but it doesn't seem to work. What do I need to add/change?

My code:

<script>
$(document).ready(function () {
    $("#slider").bind("valuesChanged", function (e, data) {
        $.ajax({
            type : "POST",
            url : "../wp-content/themes/twentytwelve/fields/test.php",
            data : {
                minValue : data.values.min,
                maxValue : data.values.max
            },
            cache : true,
            async : false,
            success : function (data) {
                console.log(data);
            },
            error : function (xhr) {
                alert('fail')
            }
        });
    });
});
</script>

and the php part:

<?php if ( $_POST ) {
    echo $_POST['minValue'];
 }

?>

Btw: it only passes the first value (minValue) and not the other one. How to pass them both? Thanks!

Upvotes: 0

Views: 645

Answers (3)

Maciej Małecki
Maciej Małecki

Reputation: 2745

To pass more values try something like this on php side:

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: application/json");
echo json_encode(array(
    'a' => 1,
    'b' => 2
));

Upvotes: 0

Vitaly
Vitaly

Reputation: 651

Can you see your request details in the Debug panels like Firebug in FF or (I don't know how it called) in Chromium-based browsers? Are there all the passed data? If no then the error in JS.

Also print out the whole POST data on server side to see the POST content.

Upvotes: 0

matino
matino

Reputation: 17735

Maybe by printing both values?

<?php 
if (!empty($_POST)) {
    echo $_POST['minValue'];
    echo $_POST['maxValue'];
}
?>

Upvotes: 2

Related Questions