将AJAX数据库条目添加到jQuery

时间:2012-06-29 18:35:27

标签: jquery mysql ajax post syntax

如何在此脚本中添加用于数据库输入的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')");
}


?>

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 
        });
    });
});

答案 2 :(得分: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;
}
}
?>