如何在javascript AJAX中从php获取两个id值
test.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>
gethint.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无效。
任何人都可以帮助我....
答案 0 :(得分:1)
修改您的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];
答案 1 :(得分:0)
将两个值连接在一起,然后在jquery函数中将它们分开。 另一种方法是使用json_encode()使用键值对在json中对它们进行编码。然后解析它们会更容易。