我想将值传递给PHP脚本,所以我使用AJAX传递它们,并在同一个函数中我使用另一个AJAX来检索这些值.
问题是第二个AJAX没有从PHP文件中检索任何值.为什么是这样?如何将传递给PHP脚本的变量存储起来,以便第二个AJAX可以检索它?
我的代码如下:
AJAX代码:
$(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);
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
PHP代码:
<?php
$userAnswer = $_POST['name'];
echo json_encode($userAnswer);
?>
Run Code Online (Sandbox Code Playgroud)
Roh*_*mar 22
使用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);
}
});
Run Code Online (Sandbox Code Playgroud)
阅读文档http://api.jquery.com/jQuery.ajax/
还有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
?>
Run Code Online (Sandbox Code Playgroud)