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

我有一个表单可以通过 POST 提交和发送数据到 2 个页面。


我已经用javascript尝试过代码。一种表单提交有效,但另一种提交无效


<form id="add">

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

    <input type="submit" onclick="return Submit();">

</form>

javascript


function SubmitForm()

{

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

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

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

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

     return true;

}

第一次提交无效,但第二次提交有效。


冉冉说
浏览 147回答 2
2回答

一只名叫tom的猫

由于您似乎向两个不同的处理程序发送了完全相同的数据,因此您可以抛硬币——假设您只提交一个表单,然后在filecreate.php.当您发送表单时,您不能在同一个 HTTP 请求中发送两个单独的表单 - 因此您可以通过异步方法同时执行它们,或者在提交一个表单后在后端处理它们。由于您尚未展示任何 PHP 代码,因此我正在做一些假设并编写一些伪代码,但这应该足以让您入门。所以首先,为你的表单设置一个静态的动作属性。<form id="add" action="filecreate.php">&nbsp; &nbsp;<input type="text" name="test">&nbsp; &nbsp;<input type="submit"></form>如果您通过 POST 发送它,那么您还需要指定方法,<form id="add" action="filecreate.php" method="POST">然后,在 PHP 中,如果将它包含在另一个文件中,则可以同时执行这两个文件。意思是,在您的 中filecreate.php,您包括filecreate.fr.php. 一旦你这样做了,那个文件的内容也将被执行。<?php&nbsp;// Once you require the file, it will be executed in placerequire "filecreate.fr.php";// .. handle the rest of your normal execution here.也就是说,如果你多次做非常相似的事情,只是使用不同的数据,你可能想要为它创建函数 - 遵循 DRY 原则(“不要重复你自己”),你可能可以创建一个函数处理结构和处理,然后通过该函数单独发送数据。

狐的传说

尝试这个 :<form id="add">&nbsp; &nbsp; <input type="text" name="test">&nbsp; &nbsp; <input type="button" onclick="return SubmitForm();"></form>function SubmitForm(){&nbsp;if(document.forms['add'].onsubmit())&nbsp;{&nbsp; &nbsp; &nbsp;document.forms['add'].action='filecreate.php';&nbsp; &nbsp; &nbsp;document.forms['add'].submit();&nbsp; &nbsp; &nbsp;document.forms['add'].action='filecreate.fr.php';&nbsp; &nbsp; &nbsp;document.forms['add'].submit();&nbsp;}&nbsp;return true;}
打开App,查看更多内容
随时随地看视频慕课网APP