Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 更新并显示评论,无需重新加载网页_Php_Jquery - Fatal编程技术网

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

Php 更新并显示评论,无需重新加载网页,php,jquery,Php,Jquery,我有下面的jquery代码,它链接到一个html表单,它链接到一个php脚本。表单应该提交,jquery代码应该更新注释并显示注释,就像页面是referesh一样。但是当我点击submit时什么也没发生,我似乎看不出我做错了什么 <script type="text/javascript"> $(function() { //When submit button clicked $('#submit').click(function(){ //G

我有下面的jquery代码,它链接到一个html表单,它链接到一个php脚本。表单应该提交,jquery代码应该更新注释并显示注释,就像页面是referesh一样。但是当我点击submit时什么也没发生,我似乎看不出我做错了什么

<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");
?>
请帮助我,我错过了什么或没有看到什么

请尝试以下方法:

<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>

请确保您的代码格式正确。如果你没有,阅读起来会很困难,我们也不知道是否所有的东西都在那里。你应该使用firebug来检查你的ajax响应。它比警报更干净。不是答案,但在这里您应该非常小心,因为您将自己完全暴露在SQL注入式攻击面前。