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 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型