2

我将从我的代码开始......

<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);
            });
        }
    });
});

上面的代码在下面的表单中运行良好,并从我的 process.php 中将成功消息发送到带有 id 结果的 div 中,但在此之前我有另一个带有 id 注释的 div,它选择并显示来自这个 ajax 的同一个表中的数据/jquery form 存储数据...我想在提交表单时用最新的更改刷新它..先看看我的代码。

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

我的进程 php 看起来像这样

if (isset($_POST['submit']))
{
include ('databas/conektion.php');
function escape_data ($data)
        {
            global $con;
            if (ini_get('magic_quotes_gpc'))
            {
                $data = stripslashes($data);
            }
        return mysql_real_escape_string($data,$con);
        }
        if (empty($_POST['name']))
        {
            $n = FALSE;
            echo '<p>Invalid name</p>';
        }
        else
        {
            $n = escape_data($_POST['name']);
        }
        if (empty($_POST['email']))
        {
            $e = FALSE;
            echo '<p>Invalid Email</p>';
        }
        else
        {
            $e = escape_data($_POST['email']);
        }
        if (empty($_POST['comment']))
        {
            $c = FALSE;
            echo '<p>Invalid Comments</p>';
        }
        else
        {
            $c = escape_data($_POST['comment']);
        }
        $id = $_POST['idi'];
        if($n && $e && $c)
        {
            $query2 = "INSERT INTO blog_comments (name, c_email, c_c, art_id, c_date) VALUES ('$n', '$e', '$c', '$id', NOW())";
            $result2 = @mysql_query($query2);
            if($result2)
            {
                echo '<p>Your comments have been posted successfully.</p>';
            }
            else
            {
                echo '<p>System Error</p><p>' . mysql_error() . '</p>';
            }
        mysql_close();
        }
        else
        {
            echo '<p style="color:red">Please try again.</p>';
        }
 }
?>

我想要做的是刷新带有 id 注释的 div,一旦提交表单。我应该在我的代码中进行哪些更改...谢谢!

4

2 回答 2

1
  • Create a new file called comments.php (with source code of your comments block)
  • Initially load comments
    $(document).ready(function() { $("#comments_div").load("comments.php"); });
  • Then when you want to refresh, just call $("#comments_div").load("comments.php"); ie. from your first given code

    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");
        });
    }
    

EDIT:

If you have an ID in your url, then replace

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

with

            $.get("comments.php", {"id": "<?=(0+$_GET['id'])?>"}, 
              function(response) {
                 $("#comments_div").html(response);
              }
           );

Where your_id is the name of GET parameter.

EDIT2:

So how about if you try something like:

            $.get("comments.php", {id: <?php echo (0+$_GET['id']); ?>}, 
              function(response) {
                 $("#comments_div").html(response);
              }
            );
于 2012-07-23T08:46:48.670 回答
0

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

<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>
于 2012-07-24T01:16:00.200 回答