Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting array from form

Tags:

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...

$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?

like image 898
Liam Avatar asked May 27 '11 12:05

Liam


People also ask

How can we send arrays in HTML forms?

You can create arrays in form fields using square brackets. That means you can name fields like account[first_name] and account[last_name] and in most server-side languages, you will end up with an 'account' array.

Can you post an array PHP?

Use the POST Method to Send an Array From a Form in PHP. We use the POST method to send data from a form in PHP. The POST method is an HTTP request method that creates or adds resources to the server. We use it when we have to send sensitive information like passwords.

Is $_ POST an array?

The PHP built-in variable $_POST is also an array and it holds the data that we provide along with the post method.

How do you send data to an array?

Arrays can be passed to other methods just like how you pass primitive data type's arguments. To pass an array as an argument to a method, you just have to pass the name of the array without square brackets. The method prototype should match to accept the argument of the array type.


1 Answers

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:

  print_r($_POST);         // print out the whole post
  print_r($_POST['data']); // print out only the data array
like image 92
mcgrailm Avatar answered Sep 17 '22 18:09

mcgrailm