Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php/ajax:form get方法不起作用_Php_Ajax_Forms - Fatal编程技术网

php/ajax:form get方法不起作用

php/ajax:form get方法不起作用,php,ajax,forms,Php,Ajax,Forms,我有两个php文件,test.php和test2.php 在“test.php”中,我有一个表单,我想得到它的名称,并用ajax和php对其进行响应。这是我的代码,它有什么问题 test.php <html> <head> <script> function showHint(str) { if (str.length==0) { document.getElementById("txtHint").innerHTML=""; return;

我有两个php文件,test.php和test2.php
在“test.php”中,我有一个表单,我想得到它的名称,并用ajax和php对其进行响应。这是我的代码,它有什么问题

test.php

<html>
<head>
<script>
function showHint(str)
{
if (str.length==0)
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","test2.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<p><b>Start typing a name in the input field below:</b></p>
<form method="get">
First name: <input type="text" name="fname">
<input type="submit" value="click me" onclick="showHint(fname)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>

</body>
</html>

函数showHint(str)
{
如果(str.length==0)
{
document.getElementById(“txtHint”).innerHTML=“”;
回来
}
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”、“test2.php?q=“+str,true”);
xmlhttp.send();
}
开始在下面的输入字段中键入名称:

名字: 建议:

并且test2.php

<?php
$q = intval($_GET['q']);
echo $q;
?>

表单中的按钮是提交输入,将导致页面重新加载。将
return false
添加到
onclick
方法中以防止发生这种情况:

<input type="submit" value="click me" onclick="showHint(fname); return false">

你们应该告诉我们它出了什么问题,你们得到了什么错误,或者发生了什么,和你们期望发生什么,以及什么故障排除步骤被卡住了。并没有错误,只是不起作用,我的意思是,当我点击按钮时,标签不会改变,因为你们并没有通过url返回任何参数。您正在打印$q的值。但是,您可以在成功时使用此值,而不是使用.open()获取url。找到了。检查此链接。这可能对你有帮助。我做了这些更改,我认为它会更好,但并不完全正常,它只打印“0”(不带quet),怎么了?当您使用函数
intval
时,php代码强制输出为整数。您可能想改用
htmlspecialchars
if (typeof str !== 'string' && typeof str.value !== 'undefined') {
    str = str.value;
}