Posting Array from Form
You are currently handling form submissions by explicitly naming every field and retrieving them individually using $_POST['field_name']. While this method functions correctly for small datasets, it quickly becomes unwieldy as the number of inputs grows. The short answer to your question is yes, you can post data as an array, but doing so requires shifting from a flat HTML structure to a nested naming convention in your input fields.
Implementing Nested Arrays with name[] Attributes
To send multiple values for a single logical key (like your render time parameters), standard HTML forms require the use of square brackets in the name attribute. Instead of creating unique names like TimeToRenderHoursInput, TimeToRenderDaysInput, and TimeToRenderYearsInput, you should group them under a shared parent key.
Here is how you would restructure your hidden inputs to represent an array:
<!-- Hidden data as an array -->
<input type="hidden" name="render_times[]" value="<?php echo $RenderHours; ?>" />
<input type="hidden" name="render_times[]" value="<?php echo $RenderDays; ?>" />
<input type="hidden" name="render_times[]" value="<?php echo $RenderYears; ?>" />
<!-- Similarly for content minutes -->
<input type="hidden" name="content_minutes[]" value="<?php echo $ContentMinutes; ?>" />
<input type="hidden" name="content_minutes[]" value="<?php echo $ContentMinutesSelector; ?>" />
In process.php, PHP will automatically populate the corresponding array. You can then access these values using numeric indices or by iterating through them:
$render_times = $_POST['render_times'];
// Access first item
$first_hour = $render_times[0];
// Or loop through all
foreach ($render_times as $time) {
echo "Time: " . $time;
}
This approach reduces code duplication and keeps your logic cleaner. However, you must ensure that the order of elements in the array matches the expected sequence on the server side, or you need to associate each value with a specific key (e.g., render_times[0] is hours, render_times[1] is days).
The Risk of Unnamed Arrays in Production
While grouping fields into arrays works well for simple lists, your current use case involves distinct parameters like "Hours," "Days," and "Years." These are not interchangeable; they represent specific units of time with different validation rules. Sending them as a single unnamed array (render_times[]) forces the backend to assume an order that isn't guaranteed if you ever rearrange the HTML.
A safer pattern for distinct but related fields is to use associative arrays using indexed keys within the name attribute:
<input type="hidden" name="render_times[hours]" value="<?php echo $RenderHours; ?>" />
<input type="hidden" name="render_times[days]" value="<?php echo $RenderDays; ?>" />
<input type="hidden" name="render_times[years]" value="<?php echo $RenderYears; ?>" />
On the backend, you retrieve this as a multidimensional array where keys explicitly define meaning:
$hours = $_POST['render_times']['hours'];
$days = $_POST['render_times']['days'];
This eliminates ambiguity and makes your code resilient to changes in HTML ordering. It also aligns with how most modern frameworks expect data to be structured, facilitating easier migration or refactoring later.
Security and Best Practices
Your current implementation uses enctype="multipart/form-data". This is necessary only if the form contains <input type="file"> elements. If you are not uploading files, keep the attribute as application/x-www-form-urlencoded to reduce payload size. More importantly, never trust that $_POST keys will match exactly what was sent without validation.
Even with arrays, always sanitize and validate input before processing. For example, ensure that numeric fields like $CoresInTest are cast to integers:
$CoresInTest = (int) $_POST['cores_in_test'];
This prevents type coercion errors and potential injection attacks. Additionally, consider using Laravel's request validation if you move this logic into a framework environment. Laravel provides robust rules for arrays and nested structures, allowing you to define validation logic centrally rather than scattering checks across every process.php file.
Final Recommendation
Your current method of accessing variables individually is not "bad practice" in the sense that it breaks the application, but it does create maintenance debt. As forms grow, managing dozens of individual variables becomes error-prone. Transitioning to a structured array approachâpreferably with named keys for distinct data typesâimproves code readability and reduces the risk of logic errors during future development cycles.
