如何使用一个php脚本处理多个表单
我有多个表单,并且有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这不是真的。
一旦您的表单具有正确的格式,您的浏览器将仅提交当前的表单。
(PHP 在这里无关)
但是,如果您是这个意思,整个页面将被重新加载。没关系 - 当您提交表单时,页面会重新加载。如果您需要其他行为,您必须解释您的愿望。
另请注意,您的任何文本字段都不会发送到服务器,因为它们没有名称。
嗯,看来你想问如何区分这些形式。
添加一个隐藏字段
在 PHP 中
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.
well, it seems you want to ask how to distinguish these forms.
add a hidden field into each
and then in PHP
这会将多种形式中的一种提交给 php。复制、粘贴、测试和研究。
对第一个表单使用 a,b,c,d,对第二个表单使用 e,f,g,h,对第三个表单使用 i,j,k,l,并提交第二个表单会产生以下输出:
This submits one form of many to php. Copy, paste, test, and study.
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:
@Jay
其实这并不难。
一旦您提供了表单名称,您的工作就完成了。 DOM 会完成剩下的工作。
编写一个 php 块来执行您的功能(创建/更新/检索/删除)
无论单击哪个按钮,默认情况下它仅提交与其一起包含的元素。
用上面的表格试试这个。
@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.
try this with your form above.
我知道这是一篇旧文章,但这就是我解决这个问题的方法。
您所需要做的就是确保每个表单中的提交按钮具有不同的名称。例如:
然后,您只需检查按下了哪个表单的提交按钮即可。
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:
Then, you simply check which form's submit button was pressed.
如果您需要动态表单,您可以尝试下面的代码。可以将 while 语句更改为从数据库获取数据并使用 foreach 代替。希望你知道这一点。
在这里,我使用 while($n<10) 表示 10 个动态表单。
如果您需要单独的表单名称,您也可以使用如下标签。
这将创建单独的表单名称,例如 form1、form2 等,但此处不必要。
单击第 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.
This will create separate form names such as form1, form2, etc but not necessary here.
Sample page with output when I click row 5..