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 に成功メッセージを出力していますが、その前に別の div があり、この ajax が存在する同じテーブルからデータを選択して表示する id コメントがあります。 /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>

私のプロセス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
  • コメント.phpという名前の新しいファイルを作成します(コメントブロックのソースコードを使用)
  • 最初にコメントをロードします
    $(document).ready(function() { $("#comments_div").load("comments.php"); });
  • 次に、更新する場合は、$("#comments_div").load("comments.php"); ieを呼び出します。最初に与えられたコードから

    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": "<?=(0+$_GET['id'])?>"}, 
              function(response) {
                 $("#comments_div").html(response);
              }
           );

your_idはGETパラメーターの名前です。

EDIT2:

では、次のようなことを試してみたらどうでしょうか。

            $.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 に答える