我和一个朋友正在尝试为我们的网站制作评论系统,我们有一些简单的代码可以将值插入 mysql 数据库,然后读取并显示为评论,但是数据没有正确发送到表在这一刻。我们都是 ajax、php 和 mysql 的新手,所以这可能只是一个愚蠢的错误!:P
的HTML:
<form id="postComment" action="Comments.js" method="post">
<input type="email" name="email" onchange="checkEmail();" id="email" /> </br>
<div id ="emailerror">
<p id="emailerror"> </p>
</div> </br>
<input type="text" name="username" id="username" /> </br>
<input type="text" name="content" id="content" /> </br>
<input type="button" value="submit" onclick="commentUpload();" />
</form>
Javascript 文件:
function commentUpload() //uploads comment to sQl table
{
var email = document.getElementById("email").value //gets the user's email
var username = document.getElementById("username").value //gets the user's username
var content = document.getElementById("content").value //gets the comment content
// var articleName = document.getElementById("articleName") gets the article name
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","commentUpload.php?email=" + email + "&username=" +username + "&content="+content,true);
xmlhttp.send();
}
和PHP:
<?php
$email=mysql_real_escape_string($_GET['email']);
$username=mysql_real_escape_string($_GET['username']);
$content=mysql_real_escape_string($_GET['content']);
$query="INSERT INTO Comments
VALUES (1, email, username, content)";
mysql_query($query);
mysql_close();
?>
提前感谢您的帮助!