如何在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 和键值对。然后解析它们会更容易。
相关文章:
- 当同一浏览器的两个实例浏览时,Javascript页面如何具有唯一的ID
- JavaScript:当有两个B类元素时,如何在id X中的B类中选择A类
- 为两个ID设置一个变量的正确语法
- 如果两个id具有相同的值,则添加类
- 如何使用js同时更改两个ID的类名
- 如何使用 javascript 在 HTML 元素的两个 ID 之间切换
- 如何在javascript AJAX中从php获取两个id值
- 如何在 JS 中匹配两个 ID 时使用忽略大小写
- JS:如何获得两个ID
- Sequelize-两个ID上的双联接表
- JS Filter函数删除数组中两个ID's在对象中
- jquery针对两个id的单个var
- 如何在一个文档中使用两个id.getElementById类型函数
- 如何在单击事件上传递来自两个ID的两个变量
- 循环遍历两个id内的所有元素
- 需要展开/折叠游戏摘要,并使用交换机连接两个id
- 遍历对象以查找两个ID变量的匹配项
- 对单个Jquery插件使用两个ID
- 我如何在两个html元素之间交换两个id's
- 为self.location设置两个id的值