从PHPAJAX post从数组中获取值
我目前正在与ajax用户一起进行一个php项目。我需要将数据库中的值填充到一个html下拉框中,我想从一篇ajax文章中完成这项工作 到目前为止,对于项目中的所有其他ajax,我一直在使用$.post,但我不需要在php脚本中发布任何内容,因为它只需要从数据库中检索所有内容。我需要做的是php脚本从数据库检索信息,并将信息填充到数组中,然后将数组返回给调用该ajax的php脚本 数组是最好的主意吗?如果是的话,我如何从ajax中获取数组的内容,或者如果我没有发布任何内容,是否有更好的方法以及如何调用ajax从PHPAJAX post从数组中获取值,php,ajax,Php,Ajax,我目前正在与ajax用户一起进行一个php项目。我需要将数据库中的值填充到一个html下拉框中,我想从一篇ajax文章中完成这项工作 到目前为止,对于项目中的所有其他ajax,我一直在使用$.post,但我不需要在php脚本中发布任何内容,因为它只需要从数据库中检索所有内容。我需要做的是php脚本从数据库检索信息,并将信息填充到数组中,然后将数组返回给调用该ajax的php脚本 数组是最好的主意吗?如果是的话,我如何从ajax中获取数组的内容,或者如果我没有发布任何内容,是否有更好的方法以及如何
谢谢你能提供的帮助 最简单的方法是在jQuery Ajax请求中使用JSON,如下所示:
// PHP side
<?php
// Do your database query, and then echo out your desired data like so:
echo json_encode(array(
'test' => 'your data',
'test2' => array('you can also use', 'arrays'),
'test3' => array('or' => 'keyed arrays'),
));
die(); // don't echo anything other JSON, or you'll get errors
?>
// Javascript side
$.post("url.com", post_data, function(json)
{
// Do something with your data here:
alert(json.test);
alert(json.test2[1]);
alert(json.test3["or"]);
}, "json");
//PHP端
//Javascript端
$.post(“url.com”,post_数据,函数(json)
{
//在此处对您的数据进行处理:
警报(json.test);
警报(json.test2[1]);
警报(json.test3[”或“]);
}“json”);
好的,谢谢您的帮助。唯一的问题是我不需要发布任何数据,所以我不认为$.post是合适的,因为没有post数据。post是否为空并不重要Tanks我的PHP部分工作正常,但在javascript中它不断出现,说未定义,我已将'platform'=>“myPlatform”在javascript中,我放置了json.platform,但随后它显示为undefined
$.ajax({
url: "your_script.php",
global: false,
type: "POST",
data: {},
dataType: "json",
async:false,
success: function(results){
//append your json results to your DOM
$('#some_div').html('');
for(var i = 0; i < results.length; i ++)
{
$('#some_div').append('<p>' + results[i].html + '</p>');
}
}
});
die(json_encode(array(array('html' => 'first'), array('html' => 'second'))));