PHP to Javascript Array
PHP to Javascript Array
我正在尝试使用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($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="
这不会让JSON神高兴。
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'
});
我认为您正在尝试通过执行county_med_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()
}
为了更容易实现,我建议使用jQuery $.get方法。
相关文章:
- Javascript 新的 Array 和 join() 方法
- Javascript setTimeout for an array
- 为什么我可以在Array属性对象中找到Javascript Array for Each方法
- 在Jquery/Javascript中的Array中插入Textbox值
- 根据 Array Javascript 中的字符串值动态创建数组名称
- Multidim json array javascript,如何抓取值
- 将内容插入Array Javascript
- 无法读取 ARRAY Javascript 中未定义的属性“0”
- 如何从 Array Javascript 获取对象
- 在新的 Array Javascript 中对对象中的部分重复对象进行计数和记录
- multi array JavaScript
- 如果数组中的元素在加载之前无法定义,则如何加入Array JavaScript
- ARRAY javascript indexOf
- Select Box PHP Array => Javascript On Click Div
- 使用array (JavaScript)创建一个简单的next / previous链接
- MultiSelect Array (Javascript to MySql)
- 通过Array(JavaScript,D3)按照给定的顺序对JSON进行排序
- getElementById with an array Javascript
- Id's in array javascript
- HTML table to array javascript