简体   繁体   English

从表单发布数组

[英]Posting array from form

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,我的页面上有一个包含一堆输入和一些隐藏字段的表单,我被要求通过“后数组”传递这些数据,只是我不确定如何执行此操作,

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...在我的过程中。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?无论如何,我的方法是不好的做法吗?

Give each input a name in array format:给每个输入一个数组格式的名称:

<input type="hidden" name="data[EstPriceInput]" value="" />

Then the in PHP $_POST['data'];然后在 PHP $_POST['data']; will be an array:将是一个数组:

  print_r($_POST);         // print out the whole post
  print_r($_POST['data']); // print out only the data array

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.但我看不到重点。

You're already doing that, as a matter of fact.事实上,你已经在这样做了。 When the form is submitted, the data is passed through a post array ($_POST).提交表单时,数据通过一个 post 数组 ($_POST) 传递。 Your process.php is receiving that array and redistributing its values as individual variables.您的 process.php 正在接收该数组并将其值重新分配为单个变量。

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 really want to do.这将取决于你真正想怎么做。

You can use the built-in function:您可以使用内置的 function:

extract($_POST);

it will create a variable for each entry in $_POST .它将为$_POST中的每个条目创建一个变量。

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.这将获取所有 POST 变量并将它们放入各自的变量中。 So if you have a input field named email and the luser puts in Someone@example.com you will have a var named $email with a value of "Someone@example.com".因此,如果您有一个名为 email 的输入字段,并且 luser 输入 Someone@example.com,您将有一个名为 $email 的变量,其值为“Someone@example.com”。

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"); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM