Php jquery/ajax形成另一个div刷新

Php jquery/ajax形成另一个div刷新,php,jquery,Php,Jquery,我会从我的代码开始 <script type="text/javascript"> $(document).ready(function(){ $("#cForm").validate({ debug: false, rules: { name: "required", email: { required: true, email:

我会从我的代码开始

<script type="text/javascript">
$(document).ready(function(){
    $("#cForm").validate({
        debug: false,
        rules: {
            name: "required",
            email: {
                required: true,
                email: true
            },
            comment: "required"
        },
        messages: {
            name: "Empty.",
            email: "Invalid email.",
            comment: "Empty.",
        },
        submitHandler: function(form) {
            // do other stuff for a valid form
            $.post('process.php', $("#cForm").serialize(), function(data) {
                $('#results').html(data);
            });
        }
    });
});

$(文档).ready(函数(){
$(“#cForm”).validate({
调试:错误,
规则:{
名称:“必需”,
电邮:{
要求:正确,
电子邮件:真的
},
评论:“必需”
},
信息:{
名称:“空。”,
电子邮件:“无效电子邮件。”,
评论:“空。”,
},
submitHandler:函数(表单){
//为有效的表单做其他事情
$.post('process.php',$(“#cForm”).serialize(),函数(数据){
$('#results').html(数据);
});
}
});
});

上面的代码与下面的表单配合得很好,并将my process.php中的成功消息精细地放入带有id结果的div中,但在此之前我还有一个带有id注释的div,它选择并显示来自此ajax/jquery表单存储数据的同一个表中的数据……我想在提交表单时用最新的更改刷新它……先看看我的代码

<div id="comments">
<h2>Comments</h2>
<?php
$query3 = "SELECT name, c_c, c_date FROM blog_comments WHERE art_id='$id'";
$result3 = @mysql_query($query3);
while($rr=mysql_fetch_array($result3))
{
    echo '<div class="comen">';
    echo "<h3>";
    echo $rr['name'];
    echo "</h3>";
    echo "<h4>";
    echo $rr['c_date'];
    echo "</h4>";
    echo "<p>";
    echo $rr['c_c'];
    echo "</p>";
    echo '</div>';
}
mysql_close();
?>
</div>
<p>Post your comments. All fields are obligatory.</p>
<div id="results"></div>
<form action="" id="cForm" name="cForm" method="post" class="cform">
 <table>
<tr><td><label for="name" id="name_label">Name</label></td><td><input type="text"     name="name" id="name" size="30" /></td></tr>
<tr><td><label for="email" id="email_label">Email</label></td><td><input type="text" name="email" id="email" size="30" /></td></tr>
<tr><td><label for="comment" id="comment_label">Comment</label></td><td><textarea name="comment" id="comment" cols="30" rows="5"></textarea></td></tr>
<tr><td><input type="hidden" name="idi" value="<?php echo $id ?>" /></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Submit" /></td></tr>
</table>
</form>

评论
发表你的评论。所有字段都是必填字段

名称 电子邮件 评论
  • 创建一个名为comments.php的新文件(带有comments块的源代码)
  • 最初加载注释
    $(document.ready(function(){$(“#comments_div”).load(“comments.php”);})
  • 然后,当您想要刷新时,只需调用
    $(“#comments_div”).load(“comments.php”)
    从你的第一个给定代码

    submitHandler: function(form) {
        // do other stuff for a valid form
        $.post('process.php', $("#cForm").serialize(), function(data) {
            $('#results').html(data);
            $("#comments_div").load("comments.php");
        });
    }
    
编辑:

如果url中有ID,则替换

            $("#comments_div").load("comments.php");

$.get(“comments.php”,{“id”:“},
功能(响应){
$(“#评论)html(回复);
}
);
其中,您的_id是GET参数的名称

编辑2:

那么,如果您尝试以下方式如何:

            $.get("comments.php", {id: <?php echo (0+$_GET['id']); ?>}, 
              function(response) {
                 $("#comments_div").html(response);
              }
            );
$.get(“comments.php”,{id:},
功能(响应){
$(“#评论)html(回复);
}
);

感谢兄弟的帮助,给了我这么多时间……我的头标签现在看起来就像这样

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $page_title; ?></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js">       </script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function(){
    $("#cForm").validate({
        debug: false,
        rules: {
            name: "required",
            email: {
                required: true,
                email: true
            },
            comment: "required"
        },
        messages: {
            name: "Empty.",
            email: "Invalid email.",
            comment: "Empty.",
        },
        submitHandler: function(form) {
            // do other stuff for a valid form
            $.post('process.php', $("#cForm").serialize(),  function(data) {
                $('#results').html(data);
                 $.get("comment.php", {"id": "<?=(0+$_GET['id'])?>"}, 
          function(response) {
             $("#comment").html(response);
          }
       );
            });
        }
    });
});
</script>
<script type="text/javascript">
$(document).ready(function() {  $.get("comment.php", {"id": "<?=(0+$_GET['id'])?>"}, 
          function(response) {
             $("#comment").html(response);
          }
       ); });
</script>
</head>

$(文档).ready(函数(){
$(“#cForm”).validate({
调试:错误,
规则:{
名称:“必需”,
电邮:{
要求:正确,
电子邮件:真的
},
评论:“必需”
},
信息:{
名称:“空。”,
电子邮件:“无效电子邮件。”,
评论:“空。”,
},
submitHandler:函数(表单){
//为有效的表单做其他事情
$.post('process.php',$(“#cForm”).serialize(),函数(数据){
$('#results').html(数据);
$.get(“comment.php”,{“id”:“},
功能(响应){
$(“#评论”).html(回复);
}
);
});
}
});
});
$(document).ready(函数(){$.get(“comment.php”,{“id”:“},
功能(响应){
$(“#评论”).html(回复);
}
); });

perfect bro感谢您的帮助……但现在还有一个问题……在我的代码中,有一个名为$id的变量,我使用get方法从URL获取,如何将它传递到我的comment.php,您刚才建议我创建user1545078,我为此添加了解决方案。你是个好兄弟,但我真的很笨:(我似乎不明白你在这里的意思。)在我的url中,它看起来像这样,所以你的id应该被id替换。我做了,但它不起作用…我想我需要在我的comment.php页面中用get方法获取id。我尝试了,但没有获取,但不起作用…我可能做了一些非常愚蠢的事情,你认为我n正确的方向。确保将所有的_id更改为id。例如{“id”:“},并确保在两个位置(document.ready和submit处理程序)替换以前的加载。如果它仍然不起作用,我建议您查看代码正在执行的内容。您可以使用Google Chrome,右键单击鼠标使用Inspect元素,移动到NewWork,单击XHR,刷新页面,然后单击comments.php。在选项卡标题中,您应该在“查询字符串参数”中看到您的id。好的,我按照您说的做了ing参数是这样写的:谢谢你的帮助,先生,我使用了你的第一个代码加载方法,我设置了一个cookie来传递变量来解决我的问题……谢谢。。。。
            $.get("comments.php", {id: <?php echo (0+$_GET['id']); ?>}, 
              function(response) {
                 $("#comments_div").html(response);
              }
            );
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $page_title; ?></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js">       </script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function(){
    $("#cForm").validate({
        debug: false,
        rules: {
            name: "required",
            email: {
                required: true,
                email: true
            },
            comment: "required"
        },
        messages: {
            name: "Empty.",
            email: "Invalid email.",
            comment: "Empty.",
        },
        submitHandler: function(form) {
            // do other stuff for a valid form
            $.post('process.php', $("#cForm").serialize(),  function(data) {
                $('#results').html(data);
                 $.get("comment.php", {"id": "<?=(0+$_GET['id'])?>"}, 
          function(response) {
             $("#comment").html(response);
          }
       );
            });
        }
    });
});
</script>
<script type="text/javascript">
$(document).ready(function() {  $.get("comment.php", {"id": "<?=(0+$_GET['id'])?>"}, 
          function(response) {
             $("#comment").html(response);
          }
       ); });
</script>
</head>