jquery - 简单的墙贴

标签 jquery database post facebook-wall

我正在制作一个简单的墙贴,但很难对其进行故障排除。我希望它无需重新加载页面即可发布(如 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"&gt;
<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'; }
?>

最佳答案

首先,这不是一个问题。但我想指出的是你应该使用

var message_wall = $('#message_wall').val();

代替

var message_wall = $('#message_wall').attr('value');

除此之外,我没有看到任何可能出错的地方。请更好地解释一下自己

这是“墙贴”部分。您当然必须添加 ajax 调用

http://jsfiddle.net/myuX3/

编辑:

$.ajax({
    type: "POST",
    url: "insert.php",
    data:{'message_wall':message_wall}, /* This is the line i've modified */
    success: function(){
        $("ul#wall").prepend("<li style="display: none;">"+message_wall+"</li>");
        $("ul#wall li:first").fadeIn();
    }
});

关于jquery - 简单的墙贴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11199497/

相关文章:

jquery - CSS3 转换仅在 Firefox 中无法使用 Zepto/jQuery addClass() 触发

jquery - 在哪里正确声明函数 jQuery

javascript - 类型错误 : Cannot read property 'then' of undefined (return promise? )

php - 关于实现简单的 Web 应用程序异地克隆的方法的想法 (php/codeigniter/mysql)

php - 如何使用 file_get_contents ('php://input' 获取文件信息 + 正文)?

jQuery:URL 不带 'page jump' 的选项卡

javascript - 使用 jQuery 函数 .load() 时出现问题

c# - 使用本地数据库发布 Windows 窗体

javascript - 在新窗口中发布会出现空白页面(jquery)

javascript - 在 JavaScript 函数中间暂停