在同一页面PHP上有多个$ _POST

new*_*ter 2 php

我有个问题.

目前,此表单将动态生成.

例,

<form method="POST">
<input type="text" name="location" id="location1" />
<input type="submit" value="Submit!" />

<input type="text" name="location" id="location2" />
<input type="submit" value="Submit!" />

<input type="text" name="location" id="location3" />
<input type="submit" value="Submit!" />

<input type="text" name="location" id="location4" />
<input type="submit" value="Submit!" />
</form>
Run Code Online (Sandbox Code Playgroud)

因此,每当我按提交时,它将仅采用表格的最后一个值.我如何让它花费所有$ _POST?

谢谢.

Pek*_*ica 8

你可以给每个字段一个唯一的名称:location1,location2....

或者,您可以构建一个数组.

为此,请[]为每个元素的名称添加一个:

<input type="text" name="location[]" id="location1" />
Run Code Online (Sandbox Code Playgroud)

这会给你一个数组$_POST["location"].

不要忘记在使用数据之前(例如在数据库查询或页面输出中),您需要单独清理每个数组元素(使用mysql_real_escape_string()htmlspecialchars()根据您的情况需要).