PHP可以通过$_POST或$_GET接收数组数据。
- 使用$_POST接收数组数据:
可以通过设置表单的method属性为post,然后在php文件中使用$_POST接收数据。例如:
HTML表单代码:
<form method="post" action="process.php">
<input type="text" name="name">
<input type="text" name="age">
<input type="submit" value="提交">
</form>
PHP代码:
$name = $_POST["name"];
$age = $_POST["age"];
如果表单中含有数组数据,可以使用以下方式接收:
HTML表单代码:
<form method="post" action="process.php">
<input type="text" name="user[name]">
<input type="text" name="user[age]">
<input type="submit" value="提交">
</form>
PHP代码:
$user = $_POST["user"];
$name = $user["name"];
$age = $user["age"];
- 使用$_GET接收数组数据:
可以通过设置URL中的参数来传递数组数据,然后在php文件中使用$_GET接收数据。例如:
URL代码:
http://example.com/process.php?name=John&age=20
PHP代码:
$name = $_GET["name"];
$age = $_GET["age"];
如果URL中含有数组数据,可以使用以下方式接收:
URL代码:
http://example.com/process.php?user[name]=John&user[age]=20
PHP代码:
$user = $_GET["user"];
$name = $user["name"];
$age = $user["age"];