单击后,我的html文件中有一个按钮,我需要做两件事。 (将数据插入数据库并重新加载页面)所以我在onclick事件中调用了javascript函数。
这是javascript文件中的函数:
function grandFinale()
{
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","insert.php?q=",true);
xmlhttp.send();
document.location.reload();
}
这是上面提到的insert.php文件
<?php
$con = mysql_connect("localhost","uname","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("SnakeDB", $con);
$sql="INSERT INTO masterTable (Name, Score) VALUES ('$_GET[name]','$_POST[txtScore1]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?>
这是HTML文件的一部分,其中包含我要更新到数据库的位置
<div class="modal-body">
<p>You scored :: </p><p id="txtScore1"></p>
<input type="text" placeholder="Your Name" id="name"> </div>
问题是数据库未更新!仅重装发生。请给我建议一个解决方案
最佳答案
据我所知,您是否没有将参数发送到脚本?
还将名称字段追加到
xmlhttp.open("GET","insert.php?q=&name=" + encodeURIComponent(document.getElementById('name').value),true);
给定的代码未经测试,但应以这种方式工作。