Viewed   81 times

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?

 Answers

5

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
Sunday, August 7, 2022
2

To pass it in as an array of values, add square brackets to the name attribute of your select element..

<select name="names[]" multiple="multiple"> 

Then you can iterate over the values in php.

foreach ( $_POST['names'] as $selectedOption )
Tuesday, November 29, 2022
 
3

I think you better approach it this way:

<input type="checkbox" name="phonelist[<?=strtoupper($device['id']);?>]" value="<?=strtoupper($device['id']);?>"/>

then

<input type="checkbox" name="phonelist[<?=strtoupper($device['id']);?>][<?=strtoupper($device['another value']);?>]" value="<?=strtoupper($device['id']).strtoupper($device['another value']);?>"/>

So you can still foreach $_POST['phonelist'] but keep a reference value.

Saturday, September 10, 2022
 
5

good and single syntax for creating two dimensional array;

  <input type="text" name="cat[]" value="cat1">
  <input type="text" name="cat[]" value="cat2">
    ...

While receiving cats;

echo $_POST['cat'][0] // echoes cat1
echo $_POST['cat'][1] // echoes cat2

Further example;

According to this definition,

<input type="text" name="cat[$cat][$id]" value="cat1">

foreach ($_POST['cat'] as $a=>$b){
    // $a == $cat
    foreach($b as $c=>$d) {
       // $c == $id
    }
}
Sunday, August 28, 2022
3

No, you can't (although it seems pointless to know it).

Once you set an array-type form field, it yields as much indexes as there are values to this. In that case, it's an empty string, meaning its value was nothing (''), just because it exists. To PHP, having no explicit value means being something null or empty, which counts as valid value. Nothing means not being declared or defined at all, which is not the case of an empty array, or an array of empty value(s).

<form method="POST" id="test" action="test.php">
	<input type='hidden' name='items[]' />
</form>
<input type="submit" value="Send" form="test">

OUTPUT of print_r($_POST)

Array
(
    [items] => Array
        (
            [0] => 
        )

)

If you want $_POST to be an empty array, just don't send anything via POST method. The superglobal is set at runtime as an empty array. Have fun. :)

Saturday, September 24, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :