1

I'm trying to post a variable through ajax. But it won't echo the variable in php.

This are the variables (these are working, seen in the log):

 $("#slider").bind("valuesChanged", function(e, data){console.log("min: " + data.values.min + " max: " + data.values.max);});

The Ajax part:

$("#slider").bind("valuesChanged", function (e, data) {

  $.ajax({
   type: "POST",
   dataType: "text",
   url: "../test.php",
   data: { minValue: data.values.min, maxValue: data.values.max },
   async: false,
   success: function(data){
      alert('yeah')

    },
     error: function(xhr) {
             alert('fail') // if your PHP script return an erroneous header, you'll land here
            }
 });
});

</script>

And php echo:

<?php

if ( $_POST ) {

        echo $_POST[ 'minValue' ];
            }
?>

Now why does it not echo the post? Thanks!

3
  • 2
    Maybe because the ajax success callback merely alerts, but does nothing with the response data? Commented Jul 5, 2013 at 9:42
  • @EliasVanOotegem That makes sense indeed.. thought it was just a check to see if it works. Commented Jul 5, 2013 at 9:48
  • I've posted an answer, based on my comment with an additional recommendation on the dataType property Commented Jul 5, 2013 at 11:21

4 Answers 4

1

When you do echo in your PHP script it will be sent back to the ajax call as a response. So you must check in the success part of the $.ajax. So do,

....
success: function(data) {
  alert(data);
},
....
Sign up to request clarification or add additional context in comments.

Comments

1

Ok seeing as my comment turned out to be the answer, here goes:
Change the success callback to do something with the data:

success: function(data)
{
    console.log(data);
}

I'd also recommend not setting the dataType explicitly. jQ does a good job at figuring out what the response's datatype is, and when sending an object litaral (like you're doing) it also does a smashing job at dealing with that, too.

Check this question for more details on how jQ "guesses" the datatype of the resonse
As you can read in the API docs, the default datatype is application/x-www-form-urlencoded; charset=UTF-8, so sending an object literal is no problem at all.

Comments

1

The echo result is returned in the data variable passed to success method. It won't appear unless you do

alert(data)

6 Comments

okay that makes sense.. than what is the line to just use the variable in php? So no alerts.
why you want to use it in php? you may want to use it in jQuery you get the data and insert it in a specific html element.
I know, it's is just a check to see if it recieves the value. Eventually I will use the values so they can be saved and updated
Yes you can use the values and then whatever you want to return or view as a response to the ajax request you will echo back it and then in JS you will put it the appropriate html element.
You are doing alert(data) in the success method inside the js right?
|
0
$("#slider").bind("valuesChanged", function(e, data) {
    $.ajax({
        type: "POST",
        dataType: "text",
        url: "../test.php",
        data: { minValue: data.values.min, maxValue: data.values.max },
        async: false,
        success: function(data) {
//          alert('yeah')
            alert(data)
        },
        error: function(xhr) {
            alert('fail') // if your PHP script return an erroneous header, you'll land here
        }
    });
});

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.