为了账号安全,请及时绑定邮箱和手机立即绑定

通过两个操作提交相同的表单?

通过两个操作提交相同的表单?

PHP
冉冉说 2021-09-18 21:07:01
我有一个表单可以通过 POST 提交和发送数据到 2 个页面。我已经用javascript尝试过代码。一种表单提交有效,但另一种提交无效<form id="add">    <input type="text" name="test">    <input type="submit" onclick="return Submit();"></form>javascriptfunction SubmitForm(){     document.forms['add'].action='filecreate.php';     document.forms['add'].submit();     document.forms['add'].action='filecreate.fr.php';     document.forms['add'].submit();     return true;}第一次提交无效,但第二次提交有效。
查看完整描述

2 回答

?
一只名叫tom的猫

TA贡献1906条经验 获得超3个赞

由于您似乎向两个不同的处理程序发送了完全相同的数据,因此您可以抛硬币——假设您只提交一个表单,然后在filecreate.php.


当您发送表单时,您不能在同一个 HTTP 请求中发送两个单独的表单 - 因此您可以通过异步方法同时执行它们,或者在提交一个表单后在后端处理它们。


由于您尚未展示任何 PHP 代码,因此我正在做一些假设并编写一些伪代码,但这应该足以让您入门。


所以首先,为你的表单设置一个静态的动作属性。


<form id="add" action="filecreate.php">

   <input type="text" name="test">

   <input type="submit">

</form>

如果您通过 POST 发送它,那么您还需要指定方法,


<form id="add" action="filecreate.php" method="POST">

然后,在 PHP 中,如果将它包含在另一个文件中,则可以同时执行这两个文件。意思是,在您的 中filecreate.php,您包括filecreate.fr.php. 一旦你这样做了,那个文件的内容也将被执行。


<?php 

// Once you require the file, it will be executed in place

require "filecreate.fr.php";


// .. handle the rest of your normal execution here.

也就是说,如果你多次做非常相似的事情,只是使用不同的数据,你可能想要为它创建函数 - 遵循 DRY 原则(“不要重复你自己”),你可能可以创建一个函数处理结构和处理,然后通过该函数单独发送数据。


查看完整回答
反对 回复 2021-09-18
?
狐的传说

TA贡献1804条经验 获得超3个赞

尝试这个 :


<form id="add">

    <input type="text" name="test">

    <input type="button" onclick="return SubmitForm();">

</form>


function SubmitForm()

{

 if(document.forms['add'].onsubmit())

 {

     document.forms['add'].action='filecreate.php';

     document.forms['add'].submit();

     document.forms['add'].action='filecreate.fr.php';

     document.forms['add'].submit();

 }

 return true;

}


查看完整回答
反对 回复 2021-09-18
  • 2 回答
  • 0 关注
  • 146 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信