我正在制作一个简单的墙柱,并且很难对其进行故障排除。我希望它无需重新加载页面即可发布(如 Facebook 帖子)。当我点击提交时,什么都没有发生。页面重新加载,没有帖子出现,并且没有任何内容插入数据库。我不知道为什么。有什么建议么?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<HTML>
<HEAD>
<script type="text/javascript" src="js/jquery.js"> </script>
</HEAD>
<BODY>
<form id="submit_wall">
<label for="message_wall">Share your message on the Wall</label>
<input type="text" id="message_wall" />
<button type="submit">Post to wall</button>
</form>
<ul id="wall">
</ul>
<script type="text/javascript">
$(document).ready(function(){
$("form#submit_wall").submit(function() {
var message_wall = $('#message_wall').attr('value');
$.ajax({
type: "POST",
url: "insert.php",
data:"message_wall="+ message_wall,
success: function(){
$("ul#wall").prepend("<li style="display: none;">"+message_wall+"</li>");
$("ul#wall li:first").fadeIn();
}
});
return false;
});
});
</script>
</body>
insert.php 只是一个简单的数据库输入:
<?php
if(isset($_POST['message_wall'])){
/* Connection to Database */
$link = mysql_connect($host, $un, $pw) or die("Couldn't make connection.");
@mysql_select_db($db, $link) or die("Couldn't select database");
/* Remove HTML tag to prevent query injection */
$message = strip_tags($_POST['message_wall']);
$sql = 'INSERT INTO wall ("message") VALUES( "'.$message.'")';
mysql_query($sql);
echo $message;
} else { echo '0'; }
?>