【问题标题】:how to distinguish data-handling from 2 forms with 1 ajax to php如何区分数据处理与 2 种表单与 1 个 ajax 到 php
【发布时间】:2020-04-29 11:56:18
【问题描述】:

我有 2 个不同的表单,我想用 1 个 ajax 将这些表单的数据处理到 php 文件中。 为了区分它是main-comment-form 还是nested-comment-form,我在第一个表单字段中添加了一个额外的隐藏输入,其值为main_form

主表单(用于普通评论):

<form class="commentform" action="" method="POST" role="form">
    <!-- MAIN_COMMENT; need to distinguish main comment or nested comment -->
    <input type="hidden" class="main_comment" value="true" />
    <!-- NAME -->
    <input class="form-control comment_name" name="comment_name" type="text" placeholder="NAME">    
    <button type="submit" class="btn btn-primary" name="submit_comment">Submit</button>
</form>

第二种形式(用于发布嵌套评论)

<form class="commentform" action="" method="POST" role="form">
    <!-- NESTED NAME -->
    <input class="form-control nested_comment_name" name="nested_comment_name" type="text" placeholder="NAME">  
    <button type="submit" class="btn btn-primary" name="submit_nested_comment">Submit</button>
</form> 

我的 ajax js 是这样的:

$('.commentform').on('submit', function(e){
    e.preventDefault();

    var main_comment = $(".main_comment").val();    
    var comment_name = $(".comment_name").val();        
    var nested_comment_name = $(".nested_comment_name").val();


    $.ajax({
        url: 'comment.php',
        type: 'POST',
        data: {
                main_comment:main_comment,          
                comment_name:comment_name,                              
                nested_comment_name:nested_comment_name,

                },

PHP 文件,comment.php:


// variables
$main_comment = $_POST['main_comment'];
echo $main_comment;

当我从第二种形式(嵌套评论)发帖时,它也回显了值true。 我不明白这个,因为在第二种形式中,我不发送$_POST['main_comment']

【问题讨论】:

  • 但是在commentform这个表单类里面有一个叫main_comment的表单域,因为你有同一个类的两个表单,你不区分它们。因此,它可以找到您正在使用的字段。如果您在第二个表单中放入一个名为main_comment 的字段并将值设置为false,会发生什么情况?或者也许使用this 来识别您正在使用的表单? (我对 JS 不是很了解,但是你需要以某种方式区分这两种形式)
  • 当我将值为“false”的隐藏字段放入第二种形式时,两种形式都提交它回显的true
  • 好的,所以它必须找到第一个条目并使用它。这可能是有道理的。因此,您需要对其进行编码以引用实际调用 Ajax 代码的表单。我不知道怎么做,但也许是$(this).$(".main_comment").val() 或类似的东西?
  • 我用 2 个不同的 ajax 解决了这个问题
  • 嗯,没关系,但它是不需要的地方的重复。另一种方法是使用var postdata = $(this).serializeArray(); 之类的东西并将postdata 提交到您的表单。这应该从调用提交函数的表单中获取所有表单字段。

标签: php ajax forms


【解决方案1】:

你用隐藏字段解决这个问题的想法很好。 但是您还必须在第二个表单上放置一个隐藏字段,其值为false

第一种形式:

<form class="commentform" action="" method="POST" role="form">
    <!-- CHECK_MAIN_COMMENT; need to distinguish main comment or nested comment -->
    <input type="hidden" class="main_comment" value="true" />
    <!-- NAME -->
    <input class="form-control comment_name" name="comment_name" type="text" placeholder="NAME">    
    <button type="submit" class="btn btn-primary" name="submit_comment">Submit</button>
</form>

第二种形式:

<form class="commentform" action="" method="POST" role="form">
    <!-- CHECK_MAIN_COMMENT; need to distinguish main comment or nested comment -->
    <input type="hidden" class="main_comment" value="false" />
    <!-- NESTED NAME -->
    <input class="form-control nested_comment_name" name="nested_comment_name" type="text" placeholder="NAME">  
    <button type="submit" class="btn btn-primary" name="submit_nested_comment">Submit</button>
</form>

你的 ajax:

$('.comment_form').on('submit', function(e){
    e.preventDefault();

    // below;this will find the value of class main_comment from the CLOSEST form!
    var main_comment = $(this).closest('.comment_form').find('.main_comment').val(); 

    var comment_name = $(".comment_name").val();        
    var nested_comment_name = $(".nested_comment_name").val();

    $.ajax({
        url: 'comment.php',
        type: 'POST',
        data: {
                main_comment:main_comment,          
                comment_name:comment_name,                              
                nested_comment_name:nested_comment_name,

                },

comment.php

// variables
$main_comment = $_POST['main_comment'];

if($main_comment == 'true') {
    // handle data that comes from 1st form
}
else {
    // handle data that comes from 2nd form
}

【讨论】:

  • 这种方式效果很好!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 2012-02-29
  • 1970-01-01
相关资源
最近更新 更多