Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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到JavaScript的值赋值_Php_Javascript_Jquery_Ajax - Fatal编程技术网

从PHP到JavaScript的值赋值

从PHP到JavaScript的值赋值,php,javascript,jquery,ajax,Php,Javascript,Jquery,Ajax,我正在使用jQuery、JavaScript和PHP。我的Ajax.php文件只显示 数据。文件test.php已将值分配给JavaScript函数 我的问题是我无法从getList(data)获取test.php分配的值 我的逻辑有问题吗?如何获取test.php分配的值以显示在getList()函数中 $.ajax({ type: 'POST', url: 'ajax.php', data: 'id=' + id , success: function(da

我正在使用jQuery、JavaScript和PHP。我的
Ajax.php
文件只显示 数据。文件
test.php
已将值分配给JavaScript函数

我的问题是我无法从
getList(data)
获取test.php分配的值

我的逻辑有问题吗?如何获取test.php分配的值以显示在
getList()函数中

$.ajax({
    type: 'POST',
    url: 'ajax.php',
    data: 'id=' + id  ,
    success: function(data){
        $("#response").html(data);
            if(flag != 0){
                flag = 0;
                $.get("test.php", function(data){
                    alert("Data Loaded: " + data);
                    getList(data);
                });
             }
        } //Success
    }); //Ajax
php具有以下特性

<?php
    print "<script language='javascript'>";
    print " temp[temp.length]=new Array('STA-VES','East',4500);";
    print " temp[temp.length]=new Array('STA-CRF','West',5400);";
    print "</script>";
?>

您是否会得到一个具有以下输出的警报框

      <?php

      print "<script language='javascript'>";
      print " temp[temp.length]=new Array('STA-VES','East',4500);"; 
      print " temp[temp.length]=new Array('STA-CRF','West',5400);"; 
      print "</script>";  

      ?>

以下代码将输出所需的值,重建后的值将类似于您的数组:

<?php
    echo json_encode(array(
        array(
            'STA-VES',
            'East',
            4500
        ),
        array(
            'STA-CRF',
            'West',
            5400
        )
    ));
?>

然后,jQuery代码可以将响应解析回JavaScript对象

<?php
    json_encode(array(
        array(
            'STA-VES',
            'East',
            4500
        ),
        array(
            'STA-CRF',
            'West',
            5400
        )
    ));
?>

<script type="text/javascript">
    $.getJSON("test.php", function(json){
        // Access object
        for(var i = 0; i < json.length; i++) {
            alert(json[i][0]);
            alert(json[i][1]);
            alert(json[i][2]);
        }
    });
</script>

$.getJSON(“test.php”,函数(json){
//访问对象
for(var i=0;i
是的,我收到了警报,但getList函数不会显示总长度。您能为这些警报提供JSON示例吗
<?php
    echo json_encode(array(
        array(
            'STA-VES',
            'East',
            4500
        ),
        array(
            'STA-CRF',
            'West',
            5400
        )
    ));
?>
<?php
    json_encode(array(
        array(
            'STA-VES',
            'East',
            4500
        ),
        array(
            'STA-CRF',
            'West',
            5400
        )
    ));
?>

<script type="text/javascript">
    $.getJSON("test.php", function(json){
        // Access object
        for(var i = 0; i < json.length; i++) {
            alert(json[i][0]);
            alert(json[i][1]);
            alert(json[i][2]);
        }
    });
</script>