0

如何在此脚本中添加用于数据库条目的 AJAX?它可以正常工作,并且我有 php 文件可以使用,但我一直遇到括号或语法问题。

这是有效的代码(看看http://jsfiddle.net/myuX3/5/

<HTML>
<HEAD>
<script type="text/javascript" src="js/jquery.js"> </script>
</HEAD>
<BODY>
<label for="message_wall">Share your message on the Wall</label>
<input type="text" id="message_wall" name="message_wall" />
<button id="submitbtn" type="submit">Post to wall</button>
<ul id="wall">
</ul>

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

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

    var message_wall = $('#message_wall').val();
    var $d=$("<li>"+message_wall+"</li>").fadeIn(500,function() {
        $('#message_wall').val('');
    });
    $("ul#wall").prepend($d);

});
});
</script>
</body>
</html>

这是我对 ajax 的尝试。没有任何内容添加到数据库中,但其他一切正常:

$(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() {
                $('#message_wall').val('');
            });
            $("ul#wall").prepend($d);
        }
    });
});​
});

以及随附的 php 文件:

<?php
if(isset($_POST['submitbtn'])){ /*is this the correct syntax?*/
   $message = $_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']);

mysql_query("INSERT INTO wall (message) VALUES ('$message')");
}


?>
4

3 回答 3

1

更改随附的 php 文件

<?php
if(!empty($_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']);

$done = mysql_query("INSERT INTO wall (message) VALUES ('".$message."')");
    if($done){
         echo true;
    }else{
        echo  false;
    }
}
?>

关于萤火虫错误

看到这个;后删除success:function() {}fiddle

于 2012-06-29T18:42:23.603 回答
0
<?php
if(isset($_POST['submit'])){
/* 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']);

$done = mysql_query("INSERT INTO wall (message) VALUES ('test')");
if($done){
     echo true;
}else{
    echo  false;
}
}
?>
于 2012-06-29T19:13:31.300 回答
0

我认为这个调整会解决你的问题(你有一个额外的分号):

$(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() {
                    $('#message_wall').val('');
                });
                $("ul#wall").prepend($d);
            } // ; <-- remove this semicolon 
        });
    });
});
于 2012-06-29T18:54:15.317 回答