使用 AJAX 将变量传递给 PHP 并再次使用 AJAX 检索变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15637101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:40:17  来源:igfitidea点击:

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

phpjqueryajax

提问by jibin dcruz

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.

我想将值传递给 PHP 脚本,因此我使用 AJAX 传递这些值,并且在同一个函数中我使用另一个 AJAX 来检索这些值。

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?

问题是第二个 AJAX 没有从 PHP 文件中检索任何值。为什么是这样?如何存储传递给 PHP 脚本的变量,以便第二个 AJAX 可以检索它?

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);    
?>

回答by Rohan Kumar

Use dataType:"json"for jsondata

使用dataType:"json"json数据

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

阅读文档http://api.jquery.com/jQuery.ajax/

Also in PHP

也在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  
?>

回答by jibin dcruz

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.

不需要使用第二个 ajax 函数,你可以在一个函数内成功返回它,另一个问题是你不知道第一个 ajax 调用何时完成,那么,即使你使用 SESSION 你也可能无法在第二个 AJAX 中得到它称呼。

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

所以,我建议使用一个 AJAX 调用并成功获得价值。

example: in first ajax call

示例:在第一个 ajax 调用中

    $.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);
        }
    }); 

回答by Aks1357

$(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

像这样使用,首先进行ajax调用以获取数据,然后您的php函数将返回您将在数据中获取的结果并将该数据传递给新的ajax调用

回答by Sathish Kumar D

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

试试看可能有用......

回答by Juan Martínez

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

在您的 PhP 文件中,将有一个变量被调用$_REQUEST,它包含一个数组,其中包含使用 AJAX 从 Javascript 发送到 PhP 的所有数据。

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

试试这个:var_dump($_REQUEST);并检查你是否收到了这些值。