gpt4 book ai didi

javascript - 未获取 ajax-xmlhttp.open 中的字符串值 ("GET","xxx.php?q="+str,true);

转载 作者:太空宇宙 更新时间:2023-11-03 12:04:07 24 4
gpt4 key购买 nike

在下面的例子中,如果值是数字,它工作正常。但如果它是字符串,则值显示为 0 W3 Schools Example

HTML:

<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="abc-01">Peter Griffin</option>
<option value="aaac-02">Lois Griffin</option>
</select>

脚本:

function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax2.php?q="+str,true);
xmlhttp.send();
}
}

PHP:

$q = intval($_GET['q']);        
echo $q ;

我得到 0 作为值。需要帮助

最佳答案

您正试图在 php 中将 String 转换为 int,因为您的 htm 代码中有 String 值,并且您正试图在 php 代码中将这些值转换为 int,因此请相应地更正您的代码。

如果 html 是

<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="abc-01">Peter Griffin</option>
<option value="aaac-02">Lois Griffin</option>
</select>

然后是 php 代码。

$q = $_GET['q'];       
echo $q ;

关于javascript - 未获取 ajax-xmlhttp.open 中的字符串值 ("GET","xxx.php?q="+str,true);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27450130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com