如何使用JQuery和PHP为多个表单设置唯一ID

时间:2018-06-17 05:56:28

标签: javascript php jquery ajax

我的页面中有多个表单。我使用ajax将此表单提交给数据库。问题是这个表单中的每一个都应该有唯一的id以便用ajax(JQuery id选择器)提交,并且第一个表单总是使用ajax和其他表单提交用php提交。如何为每个表单提供唯一ID并使用AJAX提交所有表单?

<form action="inc/new.comment.php" method="post" id="new_comment_answer_form">
    <textarea name="new_answer" id="new_answer" cols="30" rows="10" placeholder="enter your new answer"></textarea>
    <!-- <input type="text" name="comment_author" id="comment_author" placeholder="author name"> -->
    <input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id ?>">
    <input type="hidden" name="comment_id" id="comment_id" value="<?php echo $comment_id ?>">
    <input type="submit" name="submit_answer" value="send" id="submit_answer">
</form>
<p id="new_answer_result"></p>

JQUERY:

 $("#new_comment_answer_form").submit(function (e) {
        e.preventDefault();
        var new_answer = $("#new_answer").val();
        var submit_answer = $("#submit_answer").val();
        var post_id = $("#post_id").val();
        var comment_id = $("#comment_id").val();
        $(document).ajaxStart(function () {});
        $(document).ajaxComplete(function () {});
        $.post("./inc/new.comment.php", {
            new_answer: new_answer,
            submit_answer: submit_answer,
            post_id: post_id,
            comment_id: comment_id
        }, function (data, status) {
            $("#new_answer_result").html(data);
            $("#new_answer").val("");
        });
    });

使用此代码是对博客网站中其他评论的评论。(如果有帮助的话)

3 个答案:

答案 0 :(得分:2)

您可以为每个表单提供唯一ID,但设置相同的类,例如class =&#34; submit_form&#34;。接下来,您应该通过此类调用该函数。您可以按姓名呼叫的其他字段。

$(".submit_class").submit(function (e) {
    e.preventDefault();
    var new_answer = $(this).find("[name=new_answer]").val();
    var submit_answer = $(this).find("[name=submit_answer]").val();
    var post_id = $(this).find("[name=post_id]").val();
    var comment_id = $(this).find("[name=comment_id]").val();
    $(document).ajaxStart(function () {});
    $(document).ajaxComplete(function () {});
    $.post("./inc/new.comment.php", {
        new_answer: new_answer,
        submit_answer: submit_answer,
        post_id: post_id,
        comment_id: comment_id
    }, function (data, status) {
        $("#new_answer_result").html(data);
        $(this).find("[name=new_answer]").val("");
    });
});

答案 1 :(得分:0)

我猜你在循环中有表单创建代码。

试试这个..

<?php i=0; // take post_id, if you want ?>

<form action="inc/new.comment.php" method="post" id="new_comment_answer_form_<?php echo ++i; ?>">
    <textarea name="new_answer" id="new_answer" cols="30" rows="10" placeholder="enter your new answer"></textarea>
    <!-- <input type="text" name="comment_author" id="comment_author" placeholder="author name"> -->
    <input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id ?>">
    <input type="hidden" name="comment_id" id="comment_id" value="<?php echo $comment_id ?>">
    <input type="submit" name="submit_answer" value="send" id="submit_answer">
</form>
<p id="new_answer_result"></p>

您将使用不同的ID作为&#34; new_comment_answer_form_1&#34;,&#34; new_comment_answer_form_2&#34;。

答案 2 :(得分:0)

您也可以使用php提交多个表单。

位于file.php的顶部

if(isset($_POST['btnName1'])){

  ... manipulate form's content

}elseif(isset($_POST['btnName2'])){

  ... manipulate another forms content
}

然后在你的文件中有表格,他们甚至不需要ID,php会通过按钮名称识别表格

<form method="POST">
<input type="text" name="inputField">
<input type="submit" name="btnName1">
</form>

<form method="POST">
<input type="text" name="inputField">
<input type="submit" name="btnName2">
</form>