php 从表单发布数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6152436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Posting array from form
提问by Liam
I have a form on my page with a bunch of inputs and some hidden fields, I've been asked to pass this data through a "post array" only im unsure on how to do this,
我的页面上有一个表单,其中包含一堆输入和一些隐藏字段,我被要求通过“post array”传递这些数据,但我不确定如何执行此操作,
Heres a snippet of what im doing at the moment
这是我目前正在做的事情的片段
<form enctype="multipart/form-data" action="process.php" method="POST">
...
more inputs
...
<!-- Hidden data -->
<input type="hidden" name="TimeToRenderHoursInput" value="<?php echo $RenderHours; ?>" />
<input type="hidden" name="TimeToRenderDaysInput" value="<?php echo $RenderDays; ?>" />
<input type="hidden" name="TimeToRenderYearsInput" value="<?php echo $RenderYears; ?>" />
<input type="hidden" name="ContentMinutesInput" value="<?php echo $ContentMinutes; ?>" />
<input type="hidden" name="ContentMinutesSelector" value="<?php echo $ContentMinutesSelector; ?>" />
<input type="hidden" name="PriorityInput" value="<?php echo $Priority; ?>" />
<input type="hidden" name="AvgFrameRenderTimeInput" value="<?php echo $AverageFrameRenderTime; ?>" />
<input type="hidden" name="AvgFrameRenderTimeSelector" value="<?php echo $AverageFrameRenderSelector; ?>" />
<input type="hidden" name="CoresInTestInput" value="<?php echo $CoresInTest; ?>" />
<input type="hidden" name="EstPriceInput" value="<?php echo $EstPrice; ?>" />
<!-- End hidden -->
<input type="image" src="http://www.venndigital.co.uk/testsite/renderbutton/_includes/images/button/submit.jpg" alt="Submit" value="Submit" style="border:0!important;" />
In my process.php im then calling the data as such...
在我的 process.php 中,我然后调用数据...
$first_name = $_POST['first_name'];
$company_name = $_POST['company_name'];
$email_from = $_POST['email'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$RenderHours = $_POST['TimeToRenderHoursInput'];
$RenderDays = $_POST['TimeToRenderDaysInput'];
$RenderYears = $_POST['TimeToRenderYearsInput'];
$ContentMinutes = $_POST['ContentMinutesInput'];
$ContentMinutesSelector = $_POST['ContentMinutesSelector'];
$Priority = $_POST['PriorityInput'];
$AverageFrameRenderTime = $_POST['AvgFrameRenderTimeInput'];
$AverageFrameRenderSelector = $_POST['AvgFrameRenderTimeSelector'];
$CoresInTest = $_POST['CoresInTestInput'];
$EstPrice = $_POST['EstPriceInput'];
Is there a way to post it as an array? Is my method bad practice in anyway?
有没有办法将它作为数组发布?无论如何,我的方法是不好的做法吗?
回答by mcgrailm
Give each input a name in array format:
以数组格式为每个输入指定一个名称:
<input type="hidden" name="data[EstPriceInput]" value="" />
Then the in PHP $_POST['data'];
will be an array:
然后在 PHP$_POST['data'];
中将是一个数组:
print_r($_POST); // print out the whole post
print_r($_POST['data']); // print out only the data array
回答by David Houde
When you post that data, it is stored as an array in $_POST
.
当您发布该数据时,它会以数组的形式存储在$_POST
.
You could optionally do something like:
您可以选择执行以下操作:
<input name="arrayname[item1]">
<input name="arrayname[item2]">
<input name="arrayname[item3]">
Then:
然后:
$item1 = $_POST['arrayname']['item1'];
$item2 = $_POST['arrayname']['item2'];
$item3 = $_POST['arrayname']['item3'];
But I fail to see the point.
但我没有看到这一点。
回答by Allen
You're already doing that, as a matter of fact. When the form is submitted, the data is passed through a post array ($_POST). Your process.php is receiving that array and redistributing its values as individual variables.
事实上,你已经在这样做了。当表单被提交时,数据通过一个post数组($_POST)传递。您的 process.php 正在接收该数组并将其值重新分配为单个变量。
回答by NJInamdar
Why are you sending it through a post if you already have it on the server (PHP) side?
如果您已经在服务器(PHP)端拥有它,为什么还要通过帖子发送它?
Why not just save the array to the $_SESSION
variable so you can use it when the form gets submitted, that might make it more "secure" since then the client cannot change the variables by editing the source.
为什么不将数组保存到$_SESSION
变量中,以便在提交表单时使用它,这可能会使其更加“安全”,因为客户端无法通过编辑源来更改变量。
It will depend upon how you reallywant to do.
这将取决于您真正想做的事情。
回答by Manunich
You can use the built-in function:
您可以使用内置函数:
extract($_POST);
it will create a variable for each entry in $_POST
.
它将为中的每个条目创建一个变量$_POST
。
回答by user2558999
What you are doing is not necessarily bad practice but it does however require an extraordinary amount of typing. I would accomplish what you are trying to do like this.
你所做的不一定是不好的做法,但它确实需要大量的打字。我会像这样完成你想要做的事情。
foreach($_POST as $var => $val){
$$var = $val;
}
This will take all the POST variables and put them in their own individual variables. So if you have a input field named email and the luser puts in [email protected] you will have a var named $email with a value of "[email protected]".
这将获取所有 POST 变量并将它们放入各自的变量中。因此,如果您有一个名为 email 的输入字段,并且 luser 输入了[email protected],那么您将拥有一个名为 $email 且值为“[email protected]”的变量。
回答by Guus Geurkink
If you want everything in your post to be as $Variables you can use something like this:
如果您希望帖子中的所有内容都作为 $Variables,您可以使用以下内容:
foreach($_POST as $key => $value) {
eval("$" . $key . " = " . $value");
}
foreach($_POST as $key => $value) {
eval("$" . $key . " = " . $value");
}