Using AJAX to pass variable to PHP and retrieve those using AJAX again

前端 未结 5 1958
别跟我提以往
别跟我提以往 2020-11-30 06:41

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 th

相关标签:
5条回答
  • 2020-11-30 07:24

    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);
            }
        }); 
    
    0 讨论(0)
  • 2020-11-30 07:35
    $(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

    0 讨论(0)
  • 2020-11-30 07:35

    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.

    0 讨论(0)
  • 2020-11-30 07:39

    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.......

    0 讨论(0)
  • 2020-11-30 07:40

    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  
    ?>
    
    0 讨论(0)
提交回复
热议问题