Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自php的jquery ajax数据数组_Php_Jquery_Ajax - Fatal编程技术网

来自php的jquery ajax数据数组

来自php的jquery ajax数据数组,php,jquery,ajax,Php,Jquery,Ajax,这是我的代码,所以您可以看到我正在尝试做的事情,这显然不起作用: $('#selector').change(function() { if ($(this).val().length > 0) { $.ajax({ type: "POST", url: "ajax.php", data: "id=

这是我的代码,所以您可以看到我正在尝试做的事情,这显然不起作用:

        $('#selector').change(function() {  
        if ($(this).val().length > 0)
        {           
            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: "id="+$(this).val(),
                success: function(data){
                    if (data != 'error')
                    {
                        $('#main_url').val(data[2]);
                        $('#main_url_title').val(data[3]);
                    }
                }
            });
        }
    });

我建立了一个alertdata;它只会弹出一个数组。现在,我的2个字段中填充了r和a……正确的方法是什么?

我认为您可以用json数据对php数组进行编码,然后将json与jquery一起使用


在ajax.php中添加类似echo json_encode$your_php_数组的内容

字符串文字数组意味着您在php端对数组进行了字符串化。我们可以看看您的PHP代码吗?$sql=mysql\u queryselect*从id='的表中。mysql\u real\u escape\u string$\u POST['id'.';如果mysql_num_rows$sql==0{echo'error';}否则{while$row=mysql_fetch_assoc$sql{foreach$row as$k=>$v$data[]=$v;}echo$data;}我知道我总是可以通过php对数组进行内爆,然后使用split函数…只是想知道是否有更好的方法来实现这一点…这似乎很好,但现在我需要在javascript中使用json编码的数组。