Here is an example of using the suggestions listed above -
PHP Code:
<?php
// some 'helper' functions -
// apply html htmlentities to a value
function _ent($val)
{
return htmlentities($val);
}
// return an element from an array
function _element($arr,$index)
{
return isset($arr[$index]) ? $arr[$index] : '';
}
$errors = []; // define an array to hold errors
$data = []; // define an array to hold a working copy of the data being operated on (if editing existing data, this is needed. for commonality, use if just submitting new data too.)
// define a list of option choices - both city/state are using the same list. if they were different lists, create two separate defining arrays.
$options = ['Member','New','one'];
// form processing
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$data = array_map('trim',$_POST); // get a trimmed copy of all 1st level (non-array) data
// validate all the inputs here
// state and city - select/option, consisting of Member, New, one.
// name - text field, consisting of letters and spaces only.
// phone - unknown if used, but using this recommend code layout, adding code for a field is straight forward.
if($data['state'] == '')
{
$errors['state'] = "State is required.";
}
else
{
if(!in_array($data['state'],$options))
{
$errors['state'] = "Invalid choice for State."; // if you see this error, either there is a mistake in the coding or someone is submitting their own values.
}
}
if($data['city'] == '')
{
$errors['city'] = "City is required.";
}
else
{
if(!in_array($data['city'],$options))
{
$errors['city'] = "Invalid choice for City."; // if you see this error, either there is a mistake in the coding or someone is submitting their own values.
}
}
if($data['name'] == '')
{
$errors['name'] = "Name is required.";
}
else
{
if (!preg_match("/^[a-zA-Z ]*$/",$data['name']))
{
$errors['name'] = "Only letters and spaces are allowed in Name."; // allowing actual white space, which the original message stated uses a different value in the regex pattern
}
}
// if no errors, use the submitted data
if(empty($errors))
{
// do 'something' with the submitted form data in $data
// generate a fake error message
$errors[] = 'For testing, stay on this page after the form data has been processed';
}
// if no errors at this point, the form data was successfully processed
if(empty($errors))
{
header('location:success.php'); // note: success was misspelled in the original code
die;
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
// if you want to display all the errors at once, before the start of the form, do that here...
if(!empty($errors))
{
echo implode('<br>',$errors); // add any html markup and css to display them the way you want
}
?>
<form method="post">
<fieldset>
<legend>Please select a state</legend>
<select name="state">
<option value="">-- Please select a state --</option>
<?php
foreach($options as $choice)
{
$sel = isset($data['state']) && $data['state'] == $choice ? ' selected' : '';
echo "<option value='$choice'$sel>$choice</option>\n";
}
?>
</select>
<select name="city">
<option value="">-- Please select a city --</option>
<?php
foreach($options as $choice)
{
$sel = isset($data['city']) && $data['city'] == $choice ? ' selected' : '';
echo "<option value='$choice'$sel>$choice</option>\n";
}
?>
</select>
<input type="text" name="name" value="<?= _ent(_element($data,'name')); ?>"> <?= _element($errors,'name'); ?>
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
</body>
</html>
Bookmarks