简体   繁体   English

如何将php数组转换为javascript数组

[英]How to cast a php array into javascript array

I run a mysql query and get the results successfully. 我运行一个mysql查询并成功获取结果。 However, I cannot read the elements of the array from javascript side. 但是,我无法从javascript端读取数组的元素。 Can anyone help?? 有人可以帮忙吗?

//JAVASCRIPT makes a request

  function profiles(){

   $.post('dbConn.php', { opType:"getProfileList" }, fillProfileCombo, "text");

  }


  function fillProfileCombo(res) {

   alert(res);

  }

//dbConn.php takes the request , gets the result and passes via echo as it is shown as follows:


  //RETURN PROFILE LIST 
  else if (!strcmp($opType, "getProfileList")){ //no param is coming

   $connect = mysql_connect( $db_host, $db_user, $db_pass ) or die( mysql_error() );
   mysql_select_db( $db_name ) or die( mysql_error() );

   $profiles = mysql_query(" SELECT DISTINCT profileName FROM `map_locations` ");
   $row = mysql_fetch_array($profiles);
   /*while() {
    echo $row['FirstName'] . " " . $row['LastName'];
    echo "<br />";
   } 
   */ 
   //$data = array();
   //$row = mysql_fetch_assoc($profiles)
   /*while($row = mysql_fetch_assoc($profiles))
   {
    $data[] = $row;
   }*/

   if ($row){
    echo $row;
   } else {
    echo "ERROR occured";
   }


  }

//PROBLEM: //问题:

//when I change echo $row; //当我改变echo $ row; into echo $row[0]; 进入echo $ row [0]; , I see the first element in an alert box...query is definitely working.. ,我看到了警报框中的第一个元素...查询绝对有效。

//however when I change res to res[0], it does not show anything - which is normal because I do not know how to cast php array into js array.. //但是,当我将res更改为res [0]时,它什么也没有显示-这是正常现象,因为我不知道如何将php数组转换为js数组。

function fillProfileCombo(res) {
  alert(res[0]); // does not work..
}

I do not want to use json by the way... I am not very good at. 我不想顺便使用json ...我不太擅长。 I do not want to mess it up. 我不想弄乱它。 Any suggestion and help is appreciated. 任何建议和帮助表示赞赏。

// PHP
$res = array();
while ($row = mysql_fetch_array($profiles)) {
    $res[] = $row['profileName'];
}

header('Content-type: application/json');
echo json_encode($res);

// JavaScript
$.post('dbConn.php', { opType:"getProfileList" }, function(data) {
    alert(data.length + " profiles returned");
}, "json");

Thanks Phil..This works now.. I followed your way by changing sth.. Maybe it was working but I couldnt run it. 谢谢菲尔..这现在可以工作..我按照您的方式更改了某件事..也许它正在工作,但我无法运行它。 Very similar except a couple of changes. 非常相似,除了一些更改。 I changed it as like this: 我将其更改为:

//PHP // PHP

        $data = array();
        while($row = mysql_fetch_assoc($profiles))
        {
            $data[] = $row;
        }

        if ($data){
            echo json_encode($data);

        } else {
            echo $data;
        }

//JS // JS

    function profiles(){

        //$.post('dbConn.php', { opType:"getProfileList" }, fillProfileCombo, "json");
         $.post('dbConn.php', { opType:"getProfileList" }, fillProfileCombo, "json");

    }


    function fillProfileCombo(data) {
        alert(data[1].profileName);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM