Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.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 javascript代码只工作一次_Javascript_Php_Html_Mysql_Sql - Fatal编程技术网

Php javascript代码只工作一次

Php javascript代码只工作一次,javascript,php,html,mysql,sql,Javascript,Php,Html,Mysql,Sql,我有一个简单的代码,可以在不使用javascript刷新页面的情况下将值插入数据库。 问题是,当我在方法中使用“onchange”属性调用函数时,代码工作正常,并且插入了值,但当我删除“onchange”表单并使用按钮“onclick”属性调用它工作过一次的同一方法,然后停止工作 我的comment.html文件的代码是 <html> <head> <script> function showUser(str) { if (str=="") { do

我有一个简单的代码,可以在不使用javascript刷新页面的情况下将值插入数据库。 问题是,当我在方法中使用“onchange”属性调用函数时,代码工作正常,并且插入了值,但当我删除“onchange”表单并使用按钮“onclick”属性调用它工作过一次的同一方法,然后停止工作

我的comment.html文件的代码是

 <html>
<head>
<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
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","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form method="GET">
<input type="text" name="q">
<button method="GET" onclick="showUser(q.value)">Submit</button>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

函数showUser(str)
{
如果(str==“”)
{
document.getElementById(“txtHint”).innerHTML=“”;
返回;
} 
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”、“getuser.php?q=“+str,true”);
xmlhttp.send();
}
提交

此处将列出人员信息。
我的getuser.php文件的代码是:

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

$con = mysqli_connect('localhost','root','','login');
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }

mysqli_select_db($con,"ajax_demo");
$sql2= "INSERT INTO `users` ( `FirstName`) VALUES( '{$_GET['q']}') "; 
$result = mysqli_query($con,$sql2);
while($row = $result)
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>

除了您创建的按钮之外,代码是正确的

使用:

而不是:
Submit


您所做的实际上是提交表单。因此,onclick事件被覆盖。

@Lavneet方向正确,但并没有解决问题

如果坚持该设置,则必须在
onclick
中返回
false

<button onclick="showUser(q.value);return false;">Submit</button>
提交

这样,表单就不会在
showUser()

之后重新提交。您应该使用
新的mysqli(/*args*/)
mysqli
连接保存在一个单独的安全页面上,并使用面向对象的PHP样式。它让生活变得更简单。如果你在
showUser()
中放置
警报
,它会触发吗?是的,警报会被触发
str=''
?如果输入为空或数字为0,则函数会在执行AJAX之前返回。为什么人们坚持同时使用
表单
AJAX
?不,即使删除method=“GET”也是一样的。您必须删除..它可以工作,但不知道为什么现在URL中没有附加任何内容。删除方法=“GET”不会解决问题。你甚至可以试试那东西。Prince:URL中没有附加任何内容,因为您是通过Ajax调用PHP文件的。实际上,它是附加的,但您看不到它……)