如何从javascript AJAX中的php获取两个id值 [英] How to get two id values from php in javascript AJAX
本文介绍了如何从javascript AJAX中的php获取两个id值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从javascript AJAX中的php获取两个id值
How to get two id values from php in javascript AJAX
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;
?>
Output1仅工作
Output2无法正常工作.
任何人都可以帮助我....
Can anyone please help me....
推荐答案
修改您的php代码,并输出两个ID,并以逗号分隔 例如:
Modify your php code, and output both ids separated by comma 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中
Then in your 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];
这篇关于如何从javascript AJAX中的php获取两个id值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文