如何在javascript AJAX中从php获取两个id值


How to get two id values from php in javascript AJAX

如何在javascript AJAX中从php获取两个id值

测试.php

function lc(str) {
    if (str.length == 0) {
        document.getElementById("txtHint").innerHTML = "";
        return;
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "gethint.php?q=" + str, true);
    xmlhttp.send();
} 

<p>Output1: <span id="txtHint"></span></p>
<p>Output2: <span id="wrdHint"></span></p>

得到提示.php

<?php
$q=$_GET["q"];
if (strlen($q) > 0)
  {
  $out1=strlen($q);
  }
if (str_word_count($q) > 0)
  {
  $out2=strrev($q);
  }
echo $out1;
echo $out2;
?>

输出 1 仅工作
输出 2 不工作。

谁能帮帮我....

修改你的 php 代码,并输出两个 id,用逗号分隔前任:

<?php
$q=$_GET["q"];
$out1="";
$out2="";
if (strlen($q) > 0)
  {
  $out1=strlen($q);
  }
if (str_word_count($q) > 0)
  {
  $out2=strrev($q);
  }
echo $out1.",".$out2;
?>

然后在你的JavaScript中

var str = xmlhttp.responseText;
var arr = str.split(',');
// arr[0] will be the first id
// arr[1] will be the second id
document.getElementById("txtHint").innerHTML = arr[0];
document.getElementById("wrdHint").innerHTML = arr[1];

将两个值连接在一起,然后在 jquery 函数中将它们分开。另一种方法是使用 json_encode() 将它们编码为 json 和键值对。然后解析它们会更容易。