PHP到Javascript数组
我正在尝试使用PHP创建一个Javascript数组。当我运行下面的PHP代码时,它返回必要的数组。PHP到Javascript数组,javascript,php,arrays,referenceerror,Javascript,Php,Arrays,Referenceerror,我正在尝试使用PHP创建一个Javascript数组。当我运行下面的PHP代码时,它返回必要的数组。 $counties = 'SELECT * FROM house_price_data_db.county_median_months where county=$1 or county=$2 or county=$3 order by county'; $result_counties = pg_query_params($dbconn,$counties,array(
$counties = 'SELECT * FROM house_price_data_db.county_median_months where county=$1 or county=$2 or county=$3 order by county';
$result_counties = pg_query_params($dbconn,$counties,array($county1,$county2,$county3)) or die('Query failed: ' . pg_last_error());
$county_meds_php = array();
while($r = pg_fetch_assoc($result_counties)) {
$county_meds_php[] = $r;
}
echo("county_med_month=".json_encode(array_values($county_meds_php), JSON_NUMERIC_CHECK).";");
pg_close($dbconn);?>
下面是我在Javascript中调用信息的一个小片段:
county_1="Dublin";
county_2="Galway";
county_meds(county_1,county_2);
function county_meds(a,b){
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","county_med_month.php?c_1="+a+"&c_2="+b,false);
xmlhttp.send()
}
然后当我试着打电话时:
if(county_med_month.length > 0)
console.log("The array was created!, the problem was with my JavaScript code!");
我得到这个错误:
ReferenceError: county_med_month is not defined
建议/建议?可能缺少某些内容,但: 1您的php响应必须是有效的JSON字符串
echo("county_med_month="
这不会让上帝高兴的
2您的php应该发送一个标题
header( "Content-type: application/json" );
我相信您已经过多地考虑了解决方案,请在PHP中尝试这个方法
$stuff = ''; // work on stuff later...
$return = array(); // declare variables
$return['county_med_month'] = $stuff // stuff is your content
header( "Content-type: application/json" );
die( json_encode( $return ) );
在Javascript中,使用jQuery安全地抽象ajax调用
$.get( 'php_url.php' , function( response ){
console.log( response.county_med_month ); // contains you 'stuff'
});
我认为您试图通过使用country\u med\u month=x从php设置javascript变量。那是不可能的 在PHP代码中,只需返回json格式的数组:
$counties = 'SELECT * FROM house_price_data_db.county_median_months where county=$1 or county=$2 or county=$3 order by county';
$result_counties = pg_query_params($dbconn,$counties,array($county1,$county2,$county3)) or die('Query failed: ' . pg_last_error());
$county_meds_php = array();
while($r = pg_fetch_assoc($result_counties)) {
$county_meds_php[] = $r;
}
header( "Content-type: application/json" ); // set the header to json
echo(json_encode($county_meds_php, JSON_NUMERIC_CHECK)); // this will return json
pg_close($dbconn);?>
与javascript相比,当http请求处于就绪状态时,您应该将内容解析为json数组:
county_1="Dublin";
county_2="Galway";
var county_med_month = [];
county_meds(county_1,county_2);
function county_meds(a,b){
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
// this method is used to capture the response of the http request
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
county_med_month = JSON.parse(xmlhttp.responseText);
// and now you can use the array
if(county_med_month.length > 0)
console.log("The array was created!, the problem was with my JavaScript code!");
}
}
xmlhttp.open("GET","county_med_month.php?c_1="+a+"&c_2="+b,false);
xmlhttp.send()
}
为了更容易实现,我建议使用方法。在ajax代码中,您实际上没有从脚本中捕获响应,因此正如错误所说,国家/地区/月份从未定义。在客户端,数据以文本形式接收,您可以在AJAX请求中指定响应数据类型,也可以手动解析它将我们粘贴到另一个代码框中PHP执行后的最终javascript代码使用浏览器内置的Inspector工具您可以进一步了解如何将上面的内容转换为AJAX以满足我的需要吗,只是我不太明白?我已经尝试实现了建议的解决方案,并得到以下错误:codeSyntaxError:JSON.parse:JSON数据编码后出现意外的非空白字符尝试更改echojson\u encodearray\u值$country\u meds\u php,JSON\u NUMERIC\u CHECK;进入echojson_编码$country_meds_php,JSON_NUMERIC_CHECK;好的,这是固定的,唯一的问题是,当我尝试在Country\u meds函数ReferenceError之外使用数组时,我得到了这个结果:Country\u med\u month没有定义。是否有一种方法可以在函数外部访问它?是的,您可以在函数外部声明变量,而不仅仅是在函数内部设置变量。我更新了答案。