更新并显示评论而无需重新加载网页

时间:2011-01-18 04:34:44

标签: php jquery

我有以下jquery代码,它链接到一个链接到php脚本的html表单。表单应该提交,jquery代码应该更新注释并显示注释,就像页面是referesh一样。但是当我点击提交时没有任何反应我似乎无法看到我做错了哪里或者我做错了什么。

<script type="text/javascript">

$(function() {

    //When submit button clicked
    $('#submit').click(function(){

        //Getting data from input fields
        var sentby_val = $('#sent_by').val();
        var message_val = $('#message').val();

        $.post('comment.php', { username: sentby_val, message: message_val }, function(return_data){

            //Response from script when comment inserted to database
            alert(return_data);

            //Clean fields
            $('#sent_by').val('<?php print $_SESSION['email']?>');
            $('#message').val('');

        });

    });
});

</script>

这是html表单

<form  action='comment.php' method='post' style="width: 422px">
    <input type='hidden' id="sent_by" name='sent_by' value="<?php print $_SESSION['email']?>"/>
    <input type='hidden' id="hidden_id"  name='hidden_id' value='<?php print $picid;?>'/>
    <textarea name='message' id="message" value="make your comment" style="width: 450px; height: 70px">make your comment</textarea><br/>
    <input type='submit' name='sub'  value='comment' id="submit" style="border-style:none; float:right;" />
</form>

这里是comment.php //这不应该是问题

<?
$name = $_POST['sent_by'];
$picid= $_POST['hidden_id'];
$message = $_POST['message'];
$sub = $_POST['sub'];

if ($sub){if($name&&$message&&$picid)
{
    $insert = mysql_query("INSERT INTO comment (name,message,picid) VALUES ('$name','$message','$picid')" );
}
else
{ 
    echo "Please enter a comment"; 
}

header("location:../profile.php?pic=$id");
?>

请帮助我缺少或没有看到

1 个答案:

答案 0 :(得分:0)

试试这个:

<script type="text/javascript">

$(function() {

    //When submit button clicked
    $('#submit').click(function(){

        //Getting data from input fields
        var sentby_val = $('#sent_by').val();
        var message_val = $('#message').val();

        $.post('comment.php', { username: sentby_val, message: message_val }, function(return_data){

            //Response from script when comment inserted to database
            alert(return_data);

            //Clean fields
            $('#sent_by').val('<?php print $_SESSION['email']?>');
            $('#message').val('');

        });

        // Overrides default click handler.
        // Without this the form will be submitted
        return false;

    });
});

</script>