从2维PHP数组与AJAX创建一个二维阵列的jQuery [英] Creating a 2-dimensional jQuery array from a 2-dimensional PHP array with AJAX

查看:191
本文介绍了从2维PHP数组与AJAX创建一个二维阵列的jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery,AJAX和PHP更新一个下拉框,对事件的内容。我的code目前触发事件,并使用AJAX来调用PHP函数都到数据库并获取与下拉的每个成员相关联的记录。

I am using jQuery, AJAX and PHP to update the contents of a drop down box on an event. My code currently triggers the event and uses AJAX to call a PHP function which goes to the database and gets the records associated with each member of the drop down.

我目前可以返回该2维数组(记录与列的每一个数组的数组)回到我的jQuery的功能,但我很茫然,如何给数组转换成的东西,我可以使用

I can currently return this 2-dimensional array (an array of records with an array of columns in each one) back to my jQuery function but I am at a loss as to how to convert the array into something which I can use.

jQuery的code调用AJAX:

jQuery code to call AJAX:

    var element= $('select[name=elementName]');
    var data = 'inData=' + element.val();

    // Call AJAX to get the info we need to fill the drop downs by passing in the new ID
    $.ajax(
    {
        type: "POST",
        url: "ops.php",
        data: "op=getInfo&" + data,
        success: 
        function(outData) 
        {   
            // WHAT DO I DO HERE TO CONVERT 'outData' INTO A 2-DIMENSIONAL jQUERY ARRAY??
        },
        error:
        function()
        {

        }
    }); 

PHP code:

PHP code:

$sqlResults= mysql_query("SELECT data FROM table WHERE id='".$_POST['inData']."'"); 

$outData = array();

// Fill the data array with the results
while ($outData[]= mysql_fetch_array($sqlResults));

// echo the data to return it for use in the jQuery file
echo $outData;

在code发布工作正常 - 我只是不知道怎么读outDatajQuery中

The code posted is working fine - I just don't know how to read 'outData' in jQuery.

在此先感谢您的帮助!

推荐答案

JSON可以做的伎俩,但为什么不看它从另一个角度?

JSON can do the trick, but why not look at it from another angle?

如果你侦测PHP获取更新信息,只是有PHP输出所需的选项值的选择框。然后使用jQuery AJAX的HTML返回为.html()的结果到您选择的元素。

If you're pinging PHP to get updated info, just have PHP output the option values you want in your select box. Then use the HTML return of jQuery AJAX to .html() the result into your select element.

有几个不同的方法对皮肤一只猫,我会提交这更简单的方法是要争取你额外的时间做更多的jQuery的魔力。

There's a couple of different ways to skin a cat, and I would submit that this much easier approach is going to gain you extra time to do more jQuery wizardry.

这篇关于从2维PHP数组与AJAX创建一个二维阵列的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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