Home > Blockchain >  Dynamically populating HTML Form at scale, using SQL values in PHP
Dynamically populating HTML Form at scale, using SQL values in PHP

Time:08-24

This question is more related to process / logic of what I am hoping to achieve in terms of SCALE rather than how to code it.

In Wordpress, I have several forms in html that are loaded when a user creates a custom post (essentially a new database entry for those not familiar with the CMS) for a new "incident" to be logged. I don't want to have to map everything manually as this is using update_post_meta() to set the names and value for the database entry / post - so I am using the php loop foreach ($_POST as $name => $value) { when the form is submitted to populate all database table for this incident.

This works great, but now if the user saves the form and comes back to edit it later, I would like to echo the value if it exists like so:

<label for="reported_by">Reporting individual (full name)</label>
<?php $reported_by = get_post_meta($incident_id, 'reported_by', true); ?>
<input type="text" name="reported_by" value="<?php echo $reported_by; ?>">

Again this works just fine, but I have almost 500 fields across forms on this page so setting the unique variable (in this case $reported_by) for each manually is going to take me forever and will essentially increase the codebase by almost 50%, make this difficult to maintain and will not be efficient .

Any thoughts on how to approach this? Understandably I could construct the forms via php and echo in the HTML, but that also feels like a very manual process. PHP is server side, so I cant easily get the name values from the labels / inputs client side without using AJAX, which I feel is also going to be quiet manual.

So either way I am faced with a lot of monotony unless there is some way of approaching this that would make it easier to scale across all 500 fields without me having to set the variable names manually.

Thank you for your time!

CodePudding user response:

The first thing to notice is that you don't actually need a differently-named local variable for each form input you display. That is, instead of this:

<label for="reported_by">Reporting individual (full name)</label>
<?php $reported_by = get_post_meta($incident_id, 'reported_by', true); ?>
<input type="text" name="reported_by" value="<?php echo $reported_by; ?>">

You can just as easily write this:

<label for="reported_by">Reporting individual (full name)</label>
<?php $current_value = get_post_meta($incident_id, 'reported_by', true); ?>
<input type="text" name="reported_by" value="<?php echo $current_value; ?>">

Why does that help? Because now you only have two things which are specific to this input:

  • The label 'Reporting individual (full name)'
  • The field name 'reported_by', which is used in multiple places

Both of those are just strings, so easy to pull out into PHP variables:

<?php
$field_name = 'reported_by';
$label = 'Reporting individual (full name)';
?>
<label for="<?php echo $field_name; ?>"><?php echo htmlspecialchars($label); ?></label>
<?php $current_value = get_post_meta($incident_id, $field_name, true); ?>
<input type="text" name="<?php echo $field_name; ?>" value="<?php echo $current_value; ?>">

Those look a lot like parameters, so let's make it a function, remembering to also pass in $incident_id:

<?php
function display_input($incident_id, $field_name, $label) {
    ?>
<label for="<?php echo $field_name; ?>"><?php echo htmlspecialchars($label); ?></label>
<?php $current_value = get_post_meta($incident_id, $field_name, true); ?>
<input type="text" name="<?php echo $field_name; ?>" value="<?php echo $current_value; ?>">
    <?php
}
display_input($incident_id, 'reported_by', 'Reporting individual (full name)');

Now all you need to display 500 inputs is 500 calls to display_input. To avoid even that, use an array and a foreach loop:

$fields = [
    'reported_by' => 'Reporting individual (full name)',
];
foreach ( $fields as $field_name => $label ) {
    display_input($incident_id, $field_name, $label);
}

Then, rather than hard-coding the array, you might fetch it from a config file, or even from a table in the database. You've automated the repetitive part (rendering the details of the HTML), and are left with the "interesting" part: defining the actual list of fields to display.

If some of the fields need to be slightly different, you might add some extra options to the function (and therefore the configuration array), but as long as you only have a few variations, the code should remain fairly straight-forward.

  • Related