Posting array from form

131,752

Solution 1

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

Solution 2

When you post that data, it is stored as an array in $_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.

Solution 3

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.

Solution 4

Why are you sending it through a post if you already have it on the server (PHP) side?

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.

It will depend upon how you really want to do.

Share:
131,752
Liam
Author by

Liam

Updated on July 09, 2022

Comments

  • Liam
    Liam almost 2 years

    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?

  • Liam
    Liam almost 13 years
    I've been asked to build an order form page for a client and they want the data passing to them in a POST array, as im no php developer I was wondering if my way posted above would suffice or if its frowned upon in any way?
  • David Houde
    David Houde almost 13 years
    There is nothing wrong with the code you posted. If there are only certain hidden form elements they want in this array (and not the rest of the form) then I can see wanting a multidimensional array. This would be accomplished by using the above code. But yeah, nothing wrong with your code.
  • mcgrailm
    mcgrailm almost 13 years
    it may in fact be an array but that array also has other data in it that the he may not want to sift through
  • Liam
    Liam almost 13 years
    Ahh, Is there a way it can be printed out an array then? I've tried using <?php print_r(); ?> only it doesnt print anything out, I have to use my above code, if this makes sense?
  • przemo_li
    przemo_li almost 10 years
    Thats bad design. Really, really, really bad design. 1) "var" can be user submitted 2) val can be user submitted. So someone could send existing app variables to the script and this would override it!
  • przemo_li
    przemo_li almost 10 years
    Security risk. (When $key is equal to already existing variables in the script)
  • mmr
    mmr over 9 years
    will this be a good practice? one thing I saw an importance of this is when you want to insert the values in the database without typing all the fields, you'll just loop through the array?