访问PHP中的数组从Javascript / jQuery的 [英] Accessing an array in PHP from Javascript/jQuery

查看:106
本文介绍了访问PHP中的数组从Javascript / jQuery的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个单一的文件中的所有我的HTML,PHP和JavaScript / jQuery的code。我有PHP中的数组$ ARR(json_en code($ ARR)),它在打印时显示在PHP中的数据。如何访问它的JavaScript。该数组包含了所有来自查询执行的结果集的行。我已经看过了jsonParse和VAR json_obj =,但没有得到任何结果。我是新手,所以任何帮助AP preciated。我的code到目前为止在PHP中:

  $ result_again = $ conn->查询($ sql_again);
如果($ result_again-> NUM_ROWS大于0)
{
    $ resultarray =阵列();
    而($ row_again = $ result_again-> FETCH_ASSOC())
    {
        $ resultarray [] = $ row_again;
    }
}
回声json_en code($ resultarray);

我的code。在.js文件:

  $(文件)。就绪(函数(){
 $阿贾克斯({
      键入:GET,
      数据类型:JSON
      网址:secondform.php
      成功:功能(数据){
        警报(结果:+数据);
      }
    });
});


解决方案

第1步:

渲染json_en code($ ARR)成JavaScript字符串变量。

  VAR JSON ='< = json_en code($ ARR)>?;

第二步:

解析JSON字符串成JavaScript对象。

  VAR OBJ = JSON.parse(JSON);

或者,如果你使用jQuery的:

  VAR OBJ = jQuery.parseJSON(JSON);

您现在有一个JavaScript对象,你可以像下面的访问属性:)

 警报(obj.title); //显示对象标题
警报(OBJ [0] .ID); //取得数组,并显示ID列的第一行

编辑 - 在回答slugspeeds更新

好了,所以看起来像你这样做是使用jQuery的AJAX方法。因为你的PHP脚本使用json_en code()jQuery的$。阿贾克斯()应该返回一个JavaScript数组对象。

  $(文件)。就绪(函数(){
    $阿贾克斯({
      键入:GET,
      数据类型:JSON
      网址:secondform.php
      成功:函数(ARR){
        的console.log(ARR); //显示在控制台阵列(查看右键单击检查某个元素在屏幕上,然后单击控制台选项卡)        $。每个(ARR,功能(索引,行){//通过我们与jQuery数组循环
            的console.log('阵列行#+指数,行); //显示矩阵行,我们都达到
            //你可以做你想做的与'行'在这里
        });      }
    });
});

有关参考:
https://developer.chrome.com/devtools/docs/console

I have all my html, php and javascript/jquery code in a single file. I have an array $arr in php (json_encode($arr)) which when printed shows the data in php. How do I access it in javascript. The array contains all the rows from the resultset of a query execution. I have looked up jsonParse and var json_obj = but have not got any results. I am newbie so any help is appreciated. My code so far in php :

$result_again = $conn->query($sql_again);
if ($result_again->num_rows > 0) 
{
    $resultarray = array();
    while($row_again = $result_again->fetch_assoc()) 
    {
        $resultarray[] = $row_again;
    }
}
echo json_encode($resultarray);

My code in the .js file :

 $( document ).ready(function() {
 $.ajax({
      type: "GET",
      dataType: "json",
      url: "secondform.php",
      success: function(data) {
        alert("Result: " + data);
      }
    });
});

解决方案

Step 1:

Render json_encode($arr) into javascript string variable.

var json = '<?= json_encode($arr) ?>';

Step 2:

Parse JSON string into javascript object.

var obj = JSON.parse(json);

Or if you're using jQuery:

var obj = jQuery.parseJSON(json);

You now have a javascript object which you can access properties of like below :)

alert(obj.title); // show object title
alert(obj[0].id); // grab first row of array and show 'id' column

Edit -- in reply to slugspeeds update

Ok, so looks like you're doing this the AJAX way using jQuery. Since your PHP script is using json_encode() the jQuery $.ajax() should return an javascript array object.

$( document ).ready(function() {
    $.ajax({
      type: "GET",
      dataType: "json",
      url: "secondform.php",
      success: function(arr) {
        console.log(arr); // show array in console (to view right-click inspect element somewhere on screen, then click console tab)

        $.each(arr, function( index, row ) { // loop through our array with jQuery
            console.log('array row #'+index, row); // show the array row we are up to
            // you can do what you want with 'row' here
        });

      }
    });
});

For reference: https://developer.chrome.com/devtools/docs/console

这篇关于访问PHP中的数组从Javascript / jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆