Reputation: 9855
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?
Upvotes: 20
Views: 145263
Reputation: 515
There is a better way. I'd suggest encoding the source array into json then decoding it on the other side. Instead of hiding many items, just attach the JSON string to this. Presuming this is an HTML page and that JavaScript/JQuery is available:
//if the data is visible only to JavaScript
let json_master = JSON.stringify(itemsMaster);
$('#hidden_data_master').val(json_master);
//or data originated from php and you have access
//create name-key array, not numeric indexed
$hiddenData = json_encode($namedKeyArrayData);
//at the html page, grab it into the JS variable
var hiddenData = <?php echo json_encode($hiddenData); ?>; //it came as string
//next, attach to the hidden field as earlier
$('#hidden_data_master').val(hiddenData);
JQuery is used but even pure JS can do this. The end result is to turn the various hidden inputs into an array, turn the array into a JSON string, attach the string to one hidden field in the form, and submit.
//on your target page
$hiddenDataStr = $_POST['hidden_data_master'];
$hiddenDataArray = json_decode($hiddenDataStr ) //this is what they needed
Upvotes: 0
Reputation: 4778
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.
Upvotes: 10
Reputation: 17638
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
Upvotes: 50
Reputation: 36
You can use the built-in function:
extract($_POST);
it will create a variable for each entry in $_POST
.
Upvotes: -1
Reputation: 2857
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.
Upvotes: 1
Reputation: 27
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]".
Upvotes: -2
Reputation: 713
If you want everything in your post to be as $Variables you can use something like this:
foreach($_POST as $key => $value) {
eval("$" . $key . " = " . $value");
}
Upvotes: -5
Reputation: 730
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.
Upvotes: 5