php 如何使用一个php脚本处理多个表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7849478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:29:20  来源:igfitidea点击:

How to process multiple forms using one php script

phphtmlformssubmit

提问by Jay Smoke

I have multiple forms and I have one php script that I want to use to process these forms but when I click on submit for any of the forms...the script is processed by the number of forms with the submit button named 'submitForm' in this case, the alert will show 3 times instead of once! What am I not doing right?

我有多个表单,我有一个 php 脚本,我想用它来处理这些表单,但是当我单击任何表单的提交时……该脚本由带有名为“submitForm”的提交按钮的表单数量处理在这种情况下,警报将显示 3 次而不是一次!我做错了什么?

NB. I hope this makes much sense?

注意。我希望这很有意义吗?

html code

html代码

<form action="" name="form1" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>

<form action="" name="form2" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>

<form action="" name="form3" method="post">
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="text" value="" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>

php script

php脚本

<?php 
     if (isset($_POST['submitForm'])) { 

     echo('<script>alert("Form Submitted")</script>');

     }
?>

回答by Your Common Sense

when I click on submit for any particular form, it submits all the forms.

当我单击任何特定表单的提交时,它会提交所有表单。

this is not true.
Once your forms have proper formatting, your browserwill submit only current one.
(and PHP has nothing to do here)

这不是真的。
一旦您的表单具有正确的格式,您的浏览器将仅提交当前的表单。
(和 PHP 在这里无关)

however, whole page will be reloaded, if you mean that. That is okay - when you submit a form, a page is intended to reload. If you need another behavior, you have to explain your wishes.

但是,如果您是这个意思,整个页面将被重新加载。没关系 - 当您提交表单时,页面会重新加载。如果你需要另一种行为,你必须解释你的愿望。

Also note that none of your text fields being sent to the server as they have no names.

另请注意,您的任何文本字段都没有发送到服务器,因为它们没有名称。

I guess the question I should be asking is, how do I pass a particular form to php instead of writing multiple php scripts to handle each form!!!

我想我应该问的问题是,如何将特定表单传递给 php 而不是编写多个 php 脚本来处理每个表单!!!

well, it seems you want to ask how to distinguish these forms.

嗯,看来你是想问怎么区分这些形式。

add a hidden field into each

在每个字段中添加一个隐藏字段

<input type="hidden" name="step" value="1" />

and then in PHP

然后在 PHP 中

if ($_POST['step'] == 1) {
  //first form
}
if ($_POST['step'] == 2) {
  //second
}

回答by Herbert

This submits one form of many to php. Copy, paste, test, and study.

这将许多形式中的一种提交给 php。复制、粘贴、测试和学习。

<?php
     if (isset($_POST['submitForm'])) { 

    print_r($_POST);

     }

?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>

<form action="" name="form2" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>

<form action="" name="form3" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>

Using a,b,c,d for the first form, e,f,g,h for the second form and i,j,k,l for the third form and submitting the second form yields the following output:

第一个表单使用 a,b,c,d,第二个表单使用 e,f,g,h,第三个表单使用 i,j,k,l 并提交第二个表单会产生以下输出:

Array
(
    [A] => e
    [B] => f
    [C] => g
    [D] => h
    [submitForm] => Submit Form
)

回答by gondwe

@Jay

@杰伊

Actually its not hard.

其实不难。

Once you supply form names, your work is done. the DOM does the rest.

一旦您提供表单名称,您的工作就完成了。DOM 完成剩下的工作。

write one php block to do your functions (create/update/retrieve/delete) Whichever button is clicked, by default it submits only the elements enclosed together with it.

编写一个 php 块来执行您的功能(创建/更新/检索/删除) 无论单击哪个按钮,默认情况下它只提交与其一起包含的元素。



if(!empty($_POST)){
    if(isset($_POST['submit'])){
        print "<pre>";
        var_dump($_POST); // write your code here as you would 
        print "<pre>";
    }
}

try this with your form above.

用上面的表格试试这个。

回答by Pramod

If you need dynamic forms, you may try below code. While statement can be changed to fetch data from DB and use foreach instead. Hope you know this. Here, I used while($n<10) for 10 dynamic forms. You can also use tag as below if you need separate form names.

如果您需要动态表单,您可以尝试以下代码。While 语句可以更改为从 DB 获取数据并使用 foreach 代替。希望你知道这一点。在这里,我将 while($n<10) 用于 10 个动态表单。如果您需要单独的表单名称,您也可以使用如下标签。

<form action="" name="form<?=$n?>" method="post">

This will create separate form names such as form1, form2, etc but not necessary here.

这将创建单独的表单名称,例如 form1、form2 等,但这里不是必需的。

    <?php
if (isset($_POST['submitForm'])) { 
    echo '<pre>';
    print_r($_POST);
    echo '</pre>';
}
$n=0;
while($n<10) {
    $n++;
    ?>
    <form action="" name="form1" method="post">
      <input type="text" value="" name="A" />
      <input type="text" value="" name="B" />
      <input type="text" value="" name="C" />
      <input type="text" value="" name="D" />
      <input type="Submit" value="Submit Form" name="submitForm" />
    </form>
    <?php
}
?>

Sample page with output when I click row 5..

单击第 5 行时带有输出的示例页面。