[英]How to get two id values from php in javascript AJAX
如何从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仅工作
Output2无法正常工作。
谁能帮帮我吗....
修改您的php代码,并输出两个ID,以逗号ex分隔:
<?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中对其进行编码。 这样一来,解析它们就会更加容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.