【问题标题】:Multiple forms using PHP engine on the same page are submitted all in one go在同一页面上使用PHP引擎的多个表单一次提交
【发布时间】:2019-12-27 15:57:09
【问题描述】:

我有多个查询表单,它们都调用用于电子邮件转发的同一个文件,所以它的标题是 emailForwarding.php。我显然设法在前端使用 jQuery 将表单分开,但是 emailForwarding.php 中的脚本被处理的次数与查询表单的数量相同。我只希望 php 脚本适用于我提交的表单。

我尝试使用 .eq() 和 .index() 隔离脚本的效果,并传递一个名为 $arg 的参数以仅触发包含所选表单的 div.vendor-wrapper 的表单提交事件。

single.php:

echo
 '<div class="vendor-wrapper"><form method="post" action="" name="form" class="commentForm">
<textarea name="comment" placeholder="Please enter your message in the space of 300 characters and hit the Confirm button." value="" class="message" maxlength="300"></textarea>
<input name="confirm" type="button" value="Confirm">
<input class="send" name="send'.$i++.'" type="submit" value="Send">
<input type="hidden" name="position" val="">
</form></div>;

<script>
$('.confirm').click(function(){
$('.vendor-wrapper').find('.position').val('');
var index = $(this).parents('.vendor-wrapper').index()-1;
if($('.vendor-wrapper').eq(index).find('.message').val()){
$('.vendor-wrapper').eq(index).find('.confScreen').show();
$('.vendor-wrapper').eq(index).find('.position').val(index);
}
});
</script>

emailForwarding.php:

if(isset($_POST['position'])):
$arg = 'send';
$arg .= $_POST['position'];
echo "<script>console.log('PHP: ".$arg."');</script>";
if(isset($_POST[$arg])):
 if(isset($_POST['comment'])):
   $EmailCustomer = $_POST['email'] = $current_user->user_email;
//The rest of the script for email processing omitted.

表单的提交次数与页面上表单的数量相同。

【问题讨论】:

    标签: php post form-submit


    【解决方案1】:

    在single.php标签前插入include()禁用重复提交。

    【讨论】:

      【解决方案2】:

      您能提供更多代码吗?因为我试图重现该问题,但无法使用提供的代码。因为,从代码中不清楚 $_POST['position'] 代表什么。

      回显语句是用户任何循环。你可以给 FORM 取一个不同的名字吗?

      <form method="post" action="" name="form-$i" class="commentForm">
      

      【讨论】:

      • $_POST['position'] 应该获取我不小心遗漏并稍后插入的隐藏输入元素的值。它旨在显示表单是从哪个 div.vendor-wrapper 提交的。我从未尝试过更改表格的名称。我明天会试试这个。谢谢。
      • 我将相同的变量 i 分配给
        元素,现在每个表单都以“form-[number]”的形式显示为唯一的名称值。但是,问题仍然存在于如何分别识别表单。我发现了一个建议使用隐藏输入元素或具有唯一名称的提交类型输入的问题:stackoverflow.com/questions/846020/… 但是我已经在这篇文章中显示的代码中实现了这两种方法,所以页面没有帮助。跨度>
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多