我正在调用ajax函数,将输入表单文本值更新为mysql数据库,第一次调用ajax函数时它工作正常,但如果我再次调用它,则不会更新任何内容。
HTML和JavaScript:
<html>
<head>
<script type="text/javascript">
function updateTitle() {
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;
}
}
var otsikko = document.getElementById('otsikko').value;
xmlhttp.open("POST","gettable.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
xmlhttp.send('otsikko=' + otsikko);
}
</script>
</head>
<body>
<form name="test" action="javascript:updateTitle()" method="post">
<input type="text" id="otsikko" value="pagetitle">
<input type="Submit" value="Update">
</form>
<div id="txtHint"></div>
</body>
</html>
PHP:
$otsikko=$_POST["otsikko"];
$con=mysql_connect('localhost', 'admin', 'password');
if
(!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
mysql_query("UPDATE site_content SET pagetitle='$otsikko' WHERE longtitle='jee'");
echo "Updated!";
echo $otsikko;
mysql_close($con);