获取POST发送的所有变量? [英] Get all variables sent with POST?
问题描述
我需要插入所有使用post发送的变量,它们是每个代表用户的复选框。
I need to insert all variables sent with post, they were checkboxes each representing an user.
如果我使用GET,我会得到这样的结果:
If I use GET I get something like this:
?19=on&25=on&30=on
我需要在数据库中插入变量。
I need to insert the variables in the database.
如何通过POST发送所有变量?作为一个或多个用逗号分隔的数组?
How do I get all variables sent with POST? As an array or values separated with comas or something?
推荐答案
变量 $ _ POST
会自动填充。
尝试 var_dump($ _ POST);
查看内容。
您可以访问以下单个值: echo $ _POST [name];
You can access individual values like this: echo $_POST["name"];
当然,这假设您的表单使用典型的表单编码(即 enctype =multipart / form-data
This, of course, assumes your form is using the typical form encoding (i.e. enctype="multipart/form-data"
如果你的帖子数据是另一种格式(例如JSON或XML,你可以这样做:
If your post data is in another format (e.g. JSON or XML, you can do something like this:
$post = file_get_contents('php://input');
和 $ post
将包含原始数据。
假设您使用的是标准 $ _ POST
变量,您可以测试是否选中了复选框:
Assuming you're using the standard $_POST
variable, you can test if a checkbox is checked like this:
if(isset($_POST['myCheckbox']) && $_POST['myCheckbox'] == 'Yes')
{
...
}
如果您有一系列复选框(例如
If you have an array of checkboxes (e.g.
<form action="myscript.php" method="post">
<input type="checkbox" name="myCheckbox[]" value="A" />val1<br />
<input type="checkbox" name="myCheckbox[]" value="B" />val2<br />
<input type="checkbox" name="myCheckbox[]" value="C" />val3<br />
<input type="checkbox" name="myCheckbox[]" value="D" />val4<br />
<input type="checkbox" name="myCheckbox[]" value="E" />val5
<input type="submit" name="Submit" value="Submit" />
</form>
在复选框名称中使用 []
表示所选值将由PHP脚本作为数组访问。在这种情况下, $ _ POST ['myCheckbox']
将不会返回单个字符串,但会返回一个数组,其中包含已检查的复选框的所有值。
Using [ ]
in the checkbox name indicates that the selected values will be accessed by PHP script as an array. In this case $_POST['myCheckbox']
won't return a single string but will return an array consisting of all the values of the checkboxes that were checked.
例如,如果我选中了所有方框, $ _ POST ['myCheckbox']
将是一个由以下组成的数组: code> {A,B,C,D,E} 。这是一个如何检索值数组并显示它们的示例:
For instance, if I checked all the boxes, $_POST['myCheckbox']
would be an array consisting of: {A, B, C, D, E}
. Here's an example of how to retrieve the array of values and display them:
$myboxes = $_POST['myCheckbox'];
if(empty($myboxes))
{
echo("You didn't select any boxes.");
}
else
{
$i = count($myboxes);
echo("You selected $i box(es): <br>");
for($j = 0; $j < $i; $j++)
{
echo $myboxes[$j] . "<br>";
}
}
这篇关于获取POST发送的所有变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!