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

发布于 2024-12-11 08:16:38 字数 1201 浏览 0 评论 0原文

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

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

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脚本

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

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

     }
?>

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?

NB. I hope this makes much sense?

html code

<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 
     if (isset($_POST['submitForm'])) { 

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

     }
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

请止步禁区 2024-12-18 08:16:38

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

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

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

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

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

嗯,看来你想问如何区分这些形式。

添加一个隐藏字段

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

在 PHP 中

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

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 browser will submit only current one.
(and PHP has nothing to do here)

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!!!

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

if ($_POST['step'] == 1) {
  //first form
}
if ($_POST['step'] == 2) {
  //second
}
方圜几里 2024-12-18 08:16:38

这会将多种形式中的一种提交给 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>

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

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

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

<?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:

Array
(
    [A] => e
    [B] => f
    [C] => g
    [D] => h
    [submitForm] => Submit Form
)
眼趣 2024-12-18 08:16:38

@Jay

其实这并不难。

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

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


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

用上面的表格试试这个。

@Jay

Actually its not hard.

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

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.


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.

如果没结果 2024-12-18 08:16:38

我知道这是一篇旧文章,但这就是我解决这个问题的方法。
您所需要做的就是确保每个表单中的提交按钮具有不同的名称。例如:

<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="submitForm1" />
</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="submitForm2" />
</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="submitForm3" />
</form>

然后,您只需检查按下了哪个表单的提交按钮即可。

<?php 
if (isset($_POST['submitForm1'])) { 
  echo('<script>alert("Form 1 Submitted")</script>');
} elseif (isset($_POST['submitForm2'])) { 
  echo('<script>alert("Form 2 Submitted")</script>');
} elseif (isset($_POST['submitForm3'])) { 
  echo('<script>alert("Form 3 Submitted")</script>');
}
?>

I know this is an old post but here's how I solve this very problem.
All you need to do is make sure the submit buttons in each form have different names. Eg:

<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="submitForm1" />
</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="submitForm2" />
</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="submitForm3" />
</form>

Then, you simply check which form's submit button was pressed.

<?php 
if (isset($_POST['submitForm1'])) { 
  echo('<script>alert("Form 1 Submitted")</script>');
} elseif (isset($_POST['submitForm2'])) { 
  echo('<script>alert("Form 2 Submitted")</script>');
} elseif (isset($_POST['submitForm3'])) { 
  echo('<script>alert("Form 3 Submitted")</script>');
}
?>
同展鸳鸯锦 2024-12-18 08:16:38

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

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

这将创建单独的表单名称,例如 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
}
?>

单击第 5 行时的输出示例页面..

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.

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

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

    <?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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文