0

こんにちは、私はこれらの HTML フォームを持っています:

<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="./comments.js"></script>
</head>
<div id="addCommentContainer">
<form class="add-comment-form" id="addCommentForm3" method="post" action="">
<input type="hidden" value="3" name="comentonpost" id="comentonpost" />
<textarea class="commentinput" name="body" id="body" cols="20" rows="5"></textarea>
<input type="button" class="add-comment-submit" value="Submit" />
</form>
</div>
<div id="addCommentContainer">
<form class="add-comment-form" id="addCommentForm3" method="post" action="">
<input type="hidden" value="3" name="comentonpost" id="comentonpost" />
<textarea class="commentinput" name="body" id="body" cols="20" rows="5"></textarea>
<input type="button" class="add-comment-submit" value="Submit" />
</form>
</div>



これが私のJSです:

     $(document).ready(function () {
// $(".add-comment-submit") selects all elements with the class "add-comment-submit", so the "submit" buttons
// .on will handle event binding for the click event on any of those buttons
$(".add-comment-submit").on("click", function (e) {
    e.preventDefault();
    // 'this' is the element that fired the event, so the submit button
    var submit = $(this);

    // check if the submit button has the "working" class added. this will tell us that someone already clicked it
    // and we shouldn't continue
    if (submit.hasClass("working")) return false;

    // add "working" class to the submit button to prevent double click
    submit.addClass("working");
    submit.val("Working...");

    /* Sending the form fileds to submit.php: */
    // do the post, and on callback (simulated with setTimeout) set everythign back
    $.post('submit.php',$(this).serialize(),function(msg){

    // simulate post 
    setTimeout(function () {   
        submit.removeClass("working");
        submit.val("Submit");

        //$(msg.html).hide().insertBefore(submit.parent()).slideDown();

        // simulate getting results back from server
        $("<div>Here are some results or something</div>").hide().insertBefore(submit.parent($(".addCommentContainer"))).slideDown();

    }, 1000);
    });
});
});

この JS を使用すると、すべてのフォームで SUBMIT ボタンを押すと、その前に「結果か何かがあります」というメッセージが表示されます。

ここに私のsubmit.phpがあります:

<?PHP 
$message = "Hello world";

echo $message;
?>



私の質問は$message、「ここにいくつかの結果などがあります」の代わりに、送信時にJSを印刷するにはどうすればよいですか?

スクリプトが機能するように、スクリプトで何を変更する必要があるか教えてもらえますか? 前もって感謝します!

4

2 に答える 2