I normally only have one or two inputs on a form, and editing such is no problem. However, I’d like to find a way to do this so that my form memorizes what has been input. I’m pretty certain this can probably be best done using Ajax or Javascript, but I’m not on the best of terms with either.
The plan is to have some error checking above the form, with relevant error messages, but not sure how to get the form to display what has been input already.
Code begins:
You would use the submitted form data, if any (the null coalescing operator ?? comes in handy for the initial case where there is no submitted form data), to populate form field values, to preselect options, or to precheck checkbox/radio fields. Note: any dynamic value you output in a html context needs to have htmlentities() applied to it to help prevent cross site scripting.
You should use an intermediate array variable to hold a trimmed working copy of the form data, that gets used throughout the rest of the code. There are two reasons for this - 1. to leave the original $_POST data as is, in case it is needed, 2. when you get to the point of editing existing data, you can simply query for and fetch that data into the same intermediate array variable.
If you have more than 2-3 form fields, you should dynamically validate and process the form data, and dynamically build the form, by defining the expected fields in an array, along with a display label, field type, validation rules, processing rules, … for each field. You would then loop over this defining array to control what general purpose code does, instead of writing out bespoke code and markup for every field.
Here’s a, non-dynamic, example for a single form field -
// initialization
session_start();
$post = []; // array to hold a trimmed working copy of the form data
$errors = []; // array to hold user/validation errors
// post method form processing
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
// inputs - fname (start with a single field)
// trim all the data at once
$post = array_map('trim',$_POST); // if any input is an array, use a recursive trim call-back function here instead of php's trim
// validate the inputs
// first name
if($post['fname'] === '')
{
$errors['fname'] = "First Name is required.";
}
// validate the rest of the inputs here...
// if no errors, use the form data
if(!$errors)
{
}
// if no errors, success
if(!$errors)
{
$_SESSION['success_message'] = 'Your data has been processed.';
die(header("Refresh:0"));
}
}
// get method business logic - get/produce data needed to display the web page,
// html document - this is an incomplete document and only shows the parts necessary for this demonstration
?>
<?php
// display and clear any success message
if(!empty($_SESSION['success_message']))
{
echo "<p>{$_SESSION['success_message']}</p>";
unset($_SESSION['success_message']);
}
?>
<?php
// display any user/validation errors
if($errors)
{
echo '<p>'.implode('<br>',$errors).'</p>';
}
?>
<?php
// display the form
?>
<form method='post'>
<label>First Name:<br><input type='text' name='fname' value='<?=htmlentities($post['fname']??'')?>'></label><br>
<br>
<input type='submit'>
</form>
Thanks for a very clear and explicit reply. One remaining question. How do you memorize the selected value in a dropdown list, i.e. when the form is redisplayed, how do you set this from your $post array?
Thanks.