通过AJAX处理后返回多变量php
我需要用Ajax返回回答PHP处理 但我不知道如何转换两个变量 HTML通过AJAX处理后返回多变量php,php,ajax,Php,Ajax,我需要用Ajax返回回答PHP处理 但我不知道如何转换两个变量 HTML 阿贾克斯酒店 $(文档).ready(函数(){ $.ajax({ 类型:“POST”, url:“ajax.php”, 成功:功能(结果1){ 变量名称=结果_1; $(“#名称”).html(名称); } }); }); ajax.php <?php $name = 'My Name'; $family = 'My Family'; echo ( $name ); ?> 如何做到这一点请帮助我将所
阿贾克斯酒店
$(文档).ready(函数(){
$.ajax({
类型:“POST”,
url:“ajax.php”,
成功:功能(结果1){
变量名称=结果_1;
$(“#名称”).html(名称);
}
});
});
ajax.php
<?php
$name = 'My Name';
$family = 'My Family';
echo ( $name );
?>
如何做到这一点请帮助我将所有要返回的值放入一个数组中,将其编码为JSON,然后在javascript中读回: 因此PHP变得
<?php
$name = 'My Name';
$family = 'My Family';
$result = array ('name' => $name, 'family' => $family);
echo ( json_encode($name ));
?>
您的javascript如下所示:
<script type="text/javascript">
$(document).ready(function(){
$.ajax ({
type: "POST",
url: "ajax.php",
success: function( result ) {
var name = result.name;
var family = result.family;
$("#name").html(name);
}
});
});
</script>
$(文档).ready(函数(){
$.ajax({
类型:“POST”,
url:“ajax.php”,
成功:功能(结果){
var name=result.name;
var family=result.family;
$(“#名称”).html(名称);
}
});
});
将所有要返回的值放入一个数组中,将其编码为JSON,然后在javascript中读回:
因此PHP变得
<?php
$name = 'My Name';
$family = 'My Family';
$result = array ('name' => $name, 'family' => $family);
echo ( json_encode($name ));
?>
您的javascript如下所示:
<script type="text/javascript">
$(document).ready(function(){
$.ajax ({
type: "POST",
url: "ajax.php",
success: function( result ) {
var name = result.name;
var family = result.family;
$("#name").html(name);
}
});
});
</script>
$(文档).ready(函数(){
$.ajax({
类型:“POST”,
url:“ajax.php”,
成功:功能(结果){
var name=result.name;
var family=result.family;
$(“#名称”).html(名称);
}
});
});
您得到了json格式的答案。
您得到了json格式的答案。您应该返回json对象而不是字符串 在PHP中,使用值创建一个数组并从中创建json字符串:
$response = [
'name' => 'My Name',
'family' => 'My Family'
];
// Encode the array as a json string
echo json_encode($response);
将dataType:'json'
添加到ajax调用中,以使jQuery将响应解析为json对象,然后从中获取值:
$(document).ready(function(){
$.ajax ({
type: "POST",
url: "ajax.php",
dataType: 'json', // <-- This will make jQuery handle the json response correctly
success: function( response ) {
// Now you can get both values from the json object
console.log(response.name);
console.log(response.family);
}
});
});
$(文档).ready(函数(){
$.ajax({
类型:“POST”,
url:“ajax.php”,
dataType:'json',/您应该返回一个json对象而不是字符串
在PHP中,使用值创建一个数组并从中创建json字符串:
$response = [
'name' => 'My Name',
'family' => 'My Family'
];
// Encode the array as a json string
echo json_encode($response);
将dataType:'json'
添加到ajax调用中,以使jQuery将响应解析为json对象,然后从中获取值:
$(document).ready(function(){
$.ajax ({
type: "POST",
url: "ajax.php",
dataType: 'json', // <-- This will make jQuery handle the json response correctly
success: function( response ) {
// Now you can get both values from the json object
console.log(response.name);
console.log(response.family);
}
});
});
$(文档).ready(函数(){
$.ajax({
类型:“POST”,
url:“ajax.php”,
数据类型:“json”,在PHP中创建一个数组或对象来包含所有数据
<?php
$reply['name'] = 'My Name';
$reply['family' = 'My Family';
// this will convert the array to a JSON String
// for transmission to the browser
echo json_encode($reply);
?>
在javascript代码中,希望返回一个对象
<script type="text/javascript">
$(document).ready(function(){
$.ajax ({
type: "POST",
url: "ajax.php",
dataType = 'json', // tell jquery to expect a JSON String
//and auto convert to a js object
success: function( data ) {
$("#name").val(data.name);
$("#family").val(data.family);
}
});
});
</script>
$(文档).ready(函数(){
$.ajax({
类型:“POST”,
url:“ajax.php”,
dataType='json',//告诉jquery需要一个json字符串
//并自动转换为js对象
成功:功能(数据){
$(“#name”).val(data.name);
$(“#family”).val(data.family);
}
});
});
在PHP中创建一个数组或对象来包含所有数据
<?php
$reply['name'] = 'My Name';
$reply['family' = 'My Family';
// this will convert the array to a JSON String
// for transmission to the browser
echo json_encode($reply);
?>
在javascript代码中,希望返回一个对象
<script type="text/javascript">
$(document).ready(function(){
$.ajax ({
type: "POST",
url: "ajax.php",
dataType = 'json', // tell jquery to expect a JSON String
//and auto convert to a js object
success: function( data ) {
$("#name").val(data.name);
$("#family").val(data.family);
}
});
});
</script>
$(文档).ready(函数(){
$.ajax({
类型:“POST”,
url:“ajax.php”,
dataType='json',//告诉jquery需要一个json字符串
//并自动转换为js对象
成功:功能(数据){
$(“#name”).val(data.name);
$(“#family”).val(data.family);
}
});
});
我尝试了但没有成功jsonjson\u编码(数组('name'=>$name,'family'=>$family))
我尝试了但没有成功jsonjson\u编码(数组('name'=>$name,'family'=>$family))
不工作!控制台错误是:未捕获的语法错误:无效的速记属性初始化您的页面上是否有带有id=“family”
的内容不工作!控制台错误是:未捕获的语法错误:无效的速记属性初始化您的页面上是否有带有id=“family”