Dynamically populating HTML forms at scale: via SQL values ​​in PHP
P粉765570115
P粉765570115 2023-09-10 17:28:20
0
1
614

This question has more to do with the process/logic I'm hoping to implement in terms of "SCALE" than how to write the code.

In WordPress, I have several forms that load as HTML and when the user creates a custom post (actually a new database entry for those not familiar with CMS) to record a new " event". I don't want to manually map everything as this is using update_post_meta() to set the name and value of the database entry/post - so when the form is submitted I use a php loop foreach ($_POST as $ name => $value) { to populate all database tables for this event.

This works fine, but now, if the user saves the form and comes back to edit it later, I want the value to be echoed back like this if it exists:

<label for="reported_by">报告人(全名)</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 approach works fine, but I have almost 500 fields on this page, so manually setting a unique variable for each field (in this case $reported_by) would cost I would go a long way and would basically increase the codebase by almost 50%, making it unmaintainable and inefficient.

Any ideas on how to solve this problem? Understandably, I can build the form via php and echo it in HTML, but this also feels like a very manual process. PHP is server side, so I can't easily get the name value of the tag/input on the client side, unless using AJAX, but I feel like that would also become quite manual.

So no matter what, I'm faced with a lot of duplication of effort, unless there's a way to make this process easier to scale to all 500 fields without requiring me to manually set the variable names.

Thank you for your time!

P粉765570115
P粉765570115

reply all(1)
P粉308089080

The first thing to note is that you don't actually need to create a differently named local variable for each form input. In other words, there is no need to write like this:

<label for="reported_by">报告人(全名)</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 write like this:

<label for="reported_by">报告人(全名)</label>
<?php $current_value = get_post_meta($incident_id, 'reported_by', true); ?>
<input type="text" name="reported_by" value="<?php echo $current_value; ?>">

Why is this helpful? Because there are only two contents related to this input item:

  • Tag "Reporter (full name)"
  • Field name "reported_by", used in multiple places

Both of these are just strings, so they are easy to extract into PHP variables:

<?php
$field_name = 'reported_by';
$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; ?>">

These look a lot like parameters, so let’s turn this into a function, remembering to pass in $incident_id too:

<?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', '报告人(全名)');

Now, you only need to call the display_input function 500 times to display 500 input items. To avoid this, use an array and a foreach loop:

$fields = [
    'reported_by' => '报告人(全名)',
];
foreach ( $fields as $field_name => $label ) {
    display_input($incident_id, $field_name, $label);
}

You can then get the array from a configuration file or database table instead of hardcoding it. You've automated the repetitive part (the details of rendering HTML), leaving only the "fun" part: defining the list of fields to display.

If some fields need to be slightly different, you can add some extra options to the function (and configuration array), but as long as there aren't too many changes, the code should stay fairly simple and straightforward.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template