dswg47377 2014-04-30 20:02
浏览 83
已采纳

如何从AJAX获取两个返回变量

I m sending array table id to get the table no of that table id from database. and i need to add all those table id default seats and return.

JAVA SCRIPT :

        function showUser(str)
        {

        if (str=="")
          {
          str="";
          document.getElementById("table_Merge_comb").value = ''; 
          return;
          } 
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {

            document.getElementById("table_Merge_comb").value = xmlhttp.responseText; 
            }
          }
        xmlhttp.open("GET","get_tableno.php?table_id="+str,true);
        xmlhttp.send();
        }

get_table_no.php

<?PHP 
include 'config.php';
static $total_default_seats =0;
    $item = $_GET['table_id'];              
    $table_id = explode(",",$item);             
    $table_count = count($table_id);            

    for($i=0 ; $i<$table_count; ++$i)             
    {
        $qry = mysql_query("SELECT * FROM table_info WHERE table_id = '$table_id[$i]'");
        $row = mysql_fetch_array($qry);
        $table_no[$i] = $row['table_no'];
        $total_default_seats += $row['default_seats'];
    }
    echo implode(",",$table_no);    
?>

in this code echo implode(",",$table_no); and i get it and store it in textbox by document.getElementById("table_Merge_comb").value = xmlhttp.responseText; now i need to get $total_default_seats this value too

  • 写回答

2条回答 默认 最新

  • douchu4048 2014-04-30 20:15
    关注

    Create an array in PHP, then send it back to the browser with json_encode:

    $returnValues = array(
        "value1" => "a string that I want to return",
        "value2" => array( "some", "other", "values" ),
    );
    
    echo json_encode($returnValues);
    

    This will send the following back to the browser:

    {"value1":"a string that I want to return","value2":["some","other","values"]}
    

    In your AJAX success error handler you can then get the value:

    var x = JSON.parse(xmlhttp.responseText);
    
    console.log(x.value1);    // outputs "a string I want to return"
    
    console.log(x.value2[0]);    // outputs "some"
    

    Then you can do whatever you want with the values.

    This method allows you to send back any structured data you like - multiple return values (as an array), arrays, objects, etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何基于Ryu环境下使用scapy包进行数据包构造
  • ¥15 springboot国际化
  • ¥15 搭建QEMU环境运行OP-TEE出现错误
  • ¥15 Minifilter文件保护
  • ¥15 有限元软件终止时间超过设定值
  • ¥15 onvif框架引用一直报错
  • ¥50 C#和C++混合编程,使用CLR托管,报错System.Runtime.InteropServices.SEHException
  • ¥30 .NET使用sqlite发布后报错
  • ¥15 Unity在WebGL平台导出Word报错问题
  • ¥15 ghpython这里总是报错而且rhino视图窗口内不显示怎么办