在多个文本字段中向jQueryAjax返回多个PHP回显值
这是我的Php文件,其中我将通过jQueryAjax返回两个回音在多个文本字段中向jQueryAjax返回多个PHP回显值,php,jquery,ajax,Php,Jquery,Ajax,这是我的Php文件,其中我将通过jQueryAjax返回两个回音 <?php $data=$_POST['data1']; $data2="h"; if($data2==$data) { echo "john"; echo "usa"; } else { echo "Error"; } ?> 这就是我用ajax调用它并将名称和位置设置为文本字段的地方 <input type="text" id="name"> <input type="text" id=
<?php
$data=$_POST['data1'];
$data2="h";
if($data2==$data)
{
echo "john";
echo "usa";
}
else
{
echo "Error";
}
?>
这就是我用ajax调用它并将名称和位置设置为文本字段的地方
<input type="text" id="name">
<input type="text" id="location">
<button id="submit">Click</button>
<script>
$(document).ready(function() {
$("#submit").click(function() {
var a = "h";
$.ajax({
url: "test.php",
method: "POST",
data: {
data1: a
},
success: function(response) {
alert(response);
}
});
});
});
</script>
点击
$(文档).ready(函数(){
$(“#提交”)。单击(函数(){
var a=“h”;
$.ajax({
url:“test.php”,
方法:“张贴”,
数据:{
数据1:a
},
成功:功能(响应){
警报(响应);
}
});
});
});
现在我希望John将显示在name字段中,而USA将显示在location字段中很简单,您可以尝试此代码。json_编码方法,用于通过数组发送多个值
<?php
header('Content-type:application/json');
$data=$_POST['data1'];
$data2="h";
if($data2==$data)
{
echo json_encode(array('john', 'usa'));
}
else
{
echo json_encode(array('Error'));
}
?>
很简单,你可以试试这段代码。json_编码方法,用于通过数组发送多个值
<?php
header('Content-type:application/json');
$data=$_POST['data1'];
$data2="h";
if($data2==$data)
{
echo json_encode(array('john', 'usa'));
}
else
{
echo json_encode(array('Error'));
}
?>
您需要更改
data:{
data1: a
},
与
并在html中创建如下字段
<input type="text" id="name">
<input type="text" id="location">
<button id="submit">Click</button>
<div id="result"></div>
与
与
你需要改变
data:{
data1: a
},
与
并在html中创建如下字段
<input type="text" id="name">
<input type="text" id="location">
<button id="submit">Click</button>
<div id="result"></div>
与
与
您应该将
json数组
回送为:
<?php
$data=$_POST['data1'];
$data2="h";
if($data2==$data)
{
echo json_encode(array("name"=>"John", "country"=> "usa"));die;
}
else
{
echo "Error";
}
?>
并将ajax中的json响应解析为:
<input type="text" id="name">
<input type="text" id="location">
<button id="submit">Click</button>
<script>
$(document).ready(function() {
$("#submit").click(function() {
var a = "h";
$.ajax({
url: "test.php",
method: "POST",
data: {
data1: a
},
success: function(response) {
var respData = JSON.parse(response);
$('#name').val(respData.name); // set name
$('#location').val(respData.country);// set country name
//alert(response);
}
});
});
});
</script>
点击
$(文档).ready(函数(){
$(“#提交”)。单击(函数(){
var a=“h”;
$.ajax({
url:“test.php”,
方法:“张贴”,
数据:{
数据1:a
},
成功:功能(响应){
var respData=JSON.parse(响应);
$('#name').val(respData.name);//集合名称
$('#location').val(respData.country);//设置国家名称
//警报(响应);
}
});
});
});
您应该将json数组
回送为:
<?php
$data=$_POST['data1'];
$data2="h";
if($data2==$data)
{
echo json_encode(array("name"=>"John", "country"=> "usa"));die;
}
else
{
echo "Error";
}
?>
并将ajax中的json响应解析为:
<input type="text" id="name">
<input type="text" id="location">
<button id="submit">Click</button>
<script>
$(document).ready(function() {
$("#submit").click(function() {
var a = "h";
$.ajax({
url: "test.php",
method: "POST",
data: {
data1: a
},
success: function(response) {
var respData = JSON.parse(response);
$('#name').val(respData.name); // set name
$('#location').val(respData.country);// set country name
//alert(response);
}
});
});
});
</script>
点击
$(文档).ready(函数(){
$(“#提交”)。单击(函数(){
var a=“h”;
$.ajax({
url:“test.php”,
方法:“张贴”,
数据:{
数据1:a
},
成功:功能(响应){
var respData=JSON.parse(响应);
$('#name').val(respData.name);//集合名称
$('#location').val(respData.country);//设置国家名称
//警报(响应);
}
});
});
});
最好的方法是将参数作为JSON返回,然后使用javascript解析这些参数
<?php
$data=$_POST['data1'];
$data2="h";
if($data2==$data)
{
echo json_encode(array(
"name"=>"john"
"country"=>"usa"
));
}
else
{
echo "Error";
}
?>
然后更新输入
<script>
$(document).ready(function() {
$("#submit").click(function() {
var a = "h";
$.ajax({
url: "test.php",
method: "POST",
data: {
data1: a
},
success: function(response) {
var data = jQuery.parseJSON(response).
$('#name').val(data.name);
}
});
});
});
</script>
$(文档).ready(函数(){
$(“#提交”)。单击(函数(){
var a=“h”;
$.ajax({
url:“test.php”,
方法:“张贴”,
数据:{
数据1:a
},
成功:功能(响应){
var data=jQuery.parseJSON(响应)。
$('#name').val(data.name);
}
});
});
});
最好的方法是将参数作为JSON返回,然后使用javascript解析这些参数
<?php
$data=$_POST['data1'];
$data2="h";
if($data2==$data)
{
echo json_encode(array(
"name"=>"john"
"country"=>"usa"
));
}
else
{
echo "Error";
}
?>
然后更新输入
<script>
$(document).ready(function() {
$("#submit").click(function() {
var a = "h";
$.ajax({
url: "test.php",
method: "POST",
data: {
data1: a
},
success: function(response) {
var data = jQuery.parseJSON(response).
$('#name').val(data.name);
}
});
});
});
</script>
$(文档).ready(函数(){
$(“#提交”)。单击(函数(){
var a=“h”;
$.ajax({
url:“test.php”,
方法:“张贴”,
数据:{
数据1:a
},
成功:功能(响应){
var data=jQuery.parseJSON(响应)。
$('#name').val(data.name);
}
});
});
});
大家好,欢迎来到StackOverflow。请花些时间阅读帮助页面,特别是命名和的部分。更重要的是,请阅读。您可能还想了解。在数组中填充值,然后用json对数组进行编码。在客户端,对其进行解码并在字段中设置值。您只需回显“john”。你试过返回“约翰美国”吗;大家好,欢迎来到StackOverflow。请花些时间阅读帮助页面,特别是命名和的部分。更重要的是,请阅读。您可能还想了解。在数组中填充值,然后用json对数组进行编码。在客户端,对其进行解码并在字段中设置值。您只需回显“john”。你试过返回“约翰美国”吗;问题在于使用输出,而不是输入的问题。需要在两个文本字段中显示输出,而不是在类似div的结果中。问题在于使用输出,而不是输入的问题。需要在两个文本字段中显示输出,而不是在类似div的结果中。捕获类型错误:$.parseJSON(…)。$不是Object.success的函数(test1.php:31)在i(jquery.min.js:2)在Object.fireWith[as resolved with](jquery.min.js:2)在A(jquery.min.js:4)在XMLHttpRequest.(jquery.min.js:4)或者你可以使用JSON.parse(JSON);我正在使用jquery 3.2.1使用这个var respData=JSON.parse(response)Uncaught TypeError:$.parseJSON(…)。$不是Object.success的函数(test1.php:31)在i(jquery.min.js:2)在Object.fireWith[as resolved with](jquery.min.js:2)在A(jquery.min.js:4)在XMLHttpRequest.(jquery.min.js:4)或者你可以使用JSON.parse(JSON);我正在使用jquery 3.2.1使用这个var respData=JSON.parse(response)Uncaught TypeError:$.parseJSON(…)。$不是Object.success的函数(test1.php:31)在i(jquery.min.js:2)在Object.fireWith[as resolved with](jquery.min.js:2)在A(jquery.min.js:4)在XMLHttpRequest.(jquery.min.js:4)成功@test1.php:31 i@jquery.min.js:2防火墙@jquery.min.js:4(匿名)@jquery.min.js:4加载(异步)发送@jquery.min.js:4 ajax@jquery.min.js:4