1

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

3 回答 3

1

索引.php:

<!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>
<label for="message_wall">Share your message on the Wall</label>
<form name="post" id="post" name="post">
<input type="text" id="message_wall" name="message_wall"/>
<input type="submit" id="submitbtn" value="Post to wall"/>
</form>
<ul id="wall">
</ul>

<script type="text/javascript">
$(document).ready(function(){

$('#submitbtn').click(function() {

    var message_wall = $('#message_wall').val();
    $.ajax({
        type: "POST",
        url: "insert.php",
        data:"message_wall="+ message_wall,
        success: function(){      
            var $d=$("<li>"+message_wall+"</li>").fadeIn(500,function() {
                document.post.reset(); 
            });
            $("ul#wall").prepend($d);
        }
    });
    return false;
});
});

</script>

insert.php:(名称、密码和数据库都正确。我已经三重检查了它们)

<?php
if(isset($_POST['message_wall'])){
/* Connection to Database */
$link = mysql_connect("localhost", "******", "******") or die("Couldn't make connection.");
@mysql_select_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 'fail'; }
?>
于 2012-06-28T21:22:30.107 回答
1

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

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();
    }
});
于 2012-06-26T01:04:15.773 回答
0
<input type="text" id="message_wall" name='message_wall' /> // you can use "name" attribute and "serialize()" method

js:

data: $(this).serialize(); 
success: function(data){ // pass the data to your success callback
$("ul#wall").prepend("<li style='display: none;'>"+data+"</li>"); // correct the quotation marks
于 2012-06-26T01:13:58.957 回答