18

I want to pass values to a PHP script so i am using AJAX to pass those, and in the same function I am using another AJAX to retrieve those values.

The problem is that the second AJAX is not retrieving any value from the PHP file. Why is this? How can I store the variable passed on to the PHP script so that the second AJAX can retrieve it?

My code is as follows:

AJAX CODE:

$(document).ready(function() {    
    $("#raaagh").click(function(){    
        $.ajax({
            url: 'ajax.php', //This is the current doc
            type: "POST",
            data: ({name: 145}),
            success: function(data){
                console.log(data);
            }
        });  
        $.ajax({
            url:'ajax.php',
            data:"",
            dataType:'json',
            success:function(data1){
                var y1=data1;
                console.log(data1);
            }
        });
    });
});

PHP CODE:

<?php
$userAnswer = $_POST['name'];    
echo json_encode($userAnswer);    
?>
2
  • 1
    There is no need of second ajax, you can get data in first ajax itself. Commented Mar 26, 2013 at 12:32
  • The first AJAX can be used to return data. Commented Mar 26, 2013 at 12:35

5 Answers 5

27

Use dataType:"json" for json data

$.ajax({
     url: 'ajax.php', //This is the current doc
     type: "POST",
     dataType:'json', // add json datatype to get json
     data: ({name: 145}),
     success: function(data){
         console.log(data);
     }
});  

Read Docs http://api.jquery.com/jQuery.ajax/

Also in PHP

<?php
  $userAnswer = $_POST['name']; 
  $sql="SELECT * FROM <tablename> where color='".$userAnswer."'" ;
  $result=mysql_query($sql);
  $row=mysql_fetch_array($result);
  // for first row only and suppose table having data
  echo json_encode($row);  // pass array in json_encode  
?>
Sign up to request clarification or add additional context in comments.

7 Comments

see data is the value which i sent to the php. What if i am retrieving another value from the php based on the variable which i sent. For example i am sending value blue, and in php i am using a mysql condition select * from cars where color="blue"; how will i get the value for this using the same ajax?
Check the above I have changed.
So you are saying that single AJAX call would even give me the json_encoded value from the database? but isn't data in AJAX the value we are passing on to the PHP script?
Yes, in a single call of ajax you can get data from database.Also you can pass multiple data at once in ajax, like data:{color:'blue',name:'ABC',age:15}
Suppose you are passing data:{color:'blue',name:'ABC',age:15} this data in ajax and at server side you query can be like: $sql="SELECT * FROM <tablname> where color='".$_POST['color']."' and name='".$_POST['name']."' and age='".$_POST['age']."'";
|
2
$(document).ready(function() {
    $("#raaagh").click(function() {
        $.ajax({
            url: 'ajax.php', //This is the current doc
            type: "POST",
            data: ({name: 145}),
            success: function(data) {
                console.log(data);
                $.ajax({
                    url:'ajax.php',
                    data: data,
                    dataType:'json',
                    success:function(data1) {
                        var y1=data1;
                        console.log(data1);
                    }
                });
            }
        });
    });
});

Use like this, first make a ajax call to get data, then your php function will return u the result which u wil get in data and pass that data to the new ajax call

Comments

1

No need to use second ajax function, you can get it back on success inside a function, another issue here is you don't know when the first ajax call finished, then, even if you use SESSION you may not get it within second AJAX call.

SO, I recommend using one AJAX call and get the value with success.

example: in first ajax call

    $.ajax({
        url: 'ajax.php', //This is the current doc
        type: "POST",
        data: ({name: 145}),
        success: function(data){
            console.log(data);
            alert(data);
            //or if the data is JSON
            var jdata = jQuery.parseJSON(data);
        }
    }); 

4 Comments

so how to call the data from the php file? Because what i was planning to do is pass this variable so that it can be used in a mysql query to fetch data from the database. Could you help me on how to call the data back on first ajax itself? And is there any way to make the passed value to be stored in the php script so that it can keep on querying if i keep a timeout.
see the example, once you send the parameter to your PHP then work on it, and on same time, at the end, you can print the output back to the ajax...
see data is the value which i sent to the php. What if i am retrieving another value from the php based on the variable which i sent. For example i am sending value blue, and in php i am using a mysql condition select * from cars where color="blue"; how will i get the value for this using the same ajax?
yes, echo the output on same php file, and use the code above :)
1

In your PhP file there's going to be a variable called $_REQUEST and it contains an array with all the data send from Javascript to PhP using AJAX.

Try this: var_dump($_REQUEST); and check if you're receiving the values.

Comments

0

you have to pass values with the single quotes

$(document).ready(function() {    
    $("#raaagh").click(function(){    
        $.ajax({
            url: 'ajax.php', //This is the current doc
            type: "POST",
            data: ({name: '145'}), //variables should be pass like this
            success: function(data){
                console.log(data);
                           }
        });  
        $.ajax({
    url:'ajax.php',
    data:"",
    dataType:'json',
    success:function(data1){
            var y1=data1;
            console.log(data1);
            }
        });

    });
});

try it it may work.......

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.