多种形式和一个PHP接收器 [英] Multiple forms and one PHP receiver

查看:112
本文介绍了多种形式和一个PHP接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用相同的动作和提交按钮在一个页面上生成MySQL表单。表格的数量各不相同。提交时它们都调用相同的PHP文件。另外,我有一个PHP文件,在提交时收集数据。看到下面的例子。

问题是当一个表单被提交时,值会与来自不同表单的不同字段混淆。示例:当提交form1时,PHP接收form6的值。



如何确保每个表单都提交自己的值?



HTML代码示例

 < form method =POSTaction = index.php?action = newhistoryname =history_1> 
< input type =hiddenname =idvalue =1>
< input type =textname =history>
< input type =submitname =add_historyvalue =Submit>
< / form>

< form method =POSTaction =index.php?action = newhistoryname =history_6>
< input type =hiddenname =idvalue =6>
< input type =textname =history>
< input type =submitname =add_historyvalue =Submit>
< / form>

PHP代码示例:

  case'newhistory':

$ id = $ _POST ['id'];
$ history = $ _POST ['history'];

$ sql = mysql_query(INSERT INTO history(id,history)VALUES('。$ id。','。。$ history。'),$ link);

break;

任何解决方案?

解决方案

它会提交由< form>< / form> 标签封装的内容。将多个表单的 action 属性指向同一页面不应该产生您描述的问题。



您编写的代码这里看起来很好。检查您的HTML代码,并确保每个表单的元素周围都有< form>< / form> 标签。


I have MySQL generating forms on one page with the same action and submit button. The number of forms vary. They all call the same PHP file when submitted. Also, I have one PHP file which collects the data upon submission. See the example below.

The problem is when one of the forms is submitted, values get confused with different fields from different forms. Example: When form1 is submitted, PHP receives the form6's values.

How can I make sure each form submits its own values?

HTML Code Example:

<form method="POST" action="index.php?action=newhistory" name="history_1">
    <input type="hidden" name="id" value="1">
    <input type="text" name="history">
    <input type="submit" name="add_history" value="Submit">
</form>

<form method="POST" action="index.php?action=newhistory" name="history_6">
    <input type="hidden" name="id" value="6">
    <input type="text" name="history">
    <input type="submit" name="add_history" value="Submit">
</form>

PHP Code Example:

case 'newhistory':

    $id = $_POST['id'];
    $history = $_POST['history'];

    $sql = mysql_query("INSERT INTO history (id, history) VALUES('".$id."', '".$history."')", $link);

    break;

Any solutions?

解决方案

It will submit the content encapsulated by the <form></form> tags. Having several form's action attribute point to the same page should not create the problem you describe.

The code you wrote here looks fine. Check your HTML code, and ensure you have the corrent <form></form> tags surrounding the elements of each form.

这篇关于多种形式和一个PHP接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆