1

Been trying to figure this out for hours...

I am attempting to use AJAX by grabbing values from a jQuery slider I have contained in an <input> tag. AJAX does not fail (see code), and when I console.log the variable I am trying to send through Data:, it prints out correctly.

note: The jQuery and php are on the same page, so no url: parameter, or whatever you will call it is used.

Here is the <script> portion:

   jQuery(function($){
        $("input[id='protein-slider']").ready(function(){
            var p_range;
            var request;
            $("input[id='protein-slider']").each(function () {
            p_range = $(this).val();
            });
            console.log(p_range) //Returns correct values

            request = $.ajax({
                type: "POST",
                data: p_range,
            });  
            request.done(function (response, textStatus, jqXHR){
                console.log("Hooray, it worked!"); //This prints, no error
            });
            request.fail(function (jqXHR, textStatus, errorThrown){
                console.error(
                    "The following error occurred: "+
                    textStatus, errorThrown
                );
            });
        });
    });

and the <?php ?> code in correlation with this function is:

                echo $_SERVER['QUERY_STRING']; //returns nothing
                $income_data = $_GET['p_range']; 
                echo $income_data; //returns nothing
                var_dump($income_data); //returns "NULL"

I've lost track of all of the other methods I have done and the other countless awnsers I have read on here. None seem to work and it's getting quite depressing...

Hopefully one of you guys can spot something that I am doing stupid...

Thanks!

7
  • 1
    It should be $_POST['p_range'])
    – anpsmn
    Apr 10, 2015 at 5:07
  • How have you verified that the PHP code doesn't return the desired result? You're not logging the response in your js code.
    – Kris
    Apr 10, 2015 at 5:32
  • @Kris do you know how to log data? Apr 10, 2015 at 5:35
  • @Kris I used: error: function(req, err){ console.log('my message' + err); } and nothing was printed to the console. As for checking if PHP has the desired result, wouldn't var_dump() show me something if it did give me the desired result? Apr 10, 2015 at 5:45
  • But how are you looking at the PHP output? Do you use e.g. the Net tab of Firebug to see the response? What happens when you add console.log(response); to your done function?
    – Kris
    Apr 10, 2015 at 5:50

2 Answers 2

2

Your ajax is for POST, change it to GET

request = $.ajax({
    type: "POST",
    data: p_range,
}); 

or you access your params in php as

$var = $_POST['p_range'];
2
  • what are you getting in response and in print_r($_POST); Apr 10, 2015 at 6:17
  • Sorry just saw this not. print_r($_POST); gives me nothing, however print_r($_REQUEST); gives me Array () Apr 11, 2015 at 5:20
1

Your data needs to be in key/value pair

data: {p_range: p_range},

and

$income_data = $_POST['p_range']; 
1
  • Could it maybe be because I am calling the jQuery function after the variables are set in PHP? Therefore, when the variables are echoed they don't contain anything? Apr 10, 2015 at 5:23

Your Answer

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

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