PHP to Javascript Array

PHP to Javascript Array

本文关键字:Array Javascript to PHP      更新时间:2023-09-26

我正在尝试使用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方法。