Following is my code. I get an undefined index error when the validation is completed and about to populate the 'Success' message.
I have digged and identified where the problem is (<td><?= $errors['fname'] ?></td>) but what I don't understand is what is the correct method of defining the index which resulted the error. Can someone highlight where I've gone wrong? Thanks.
<?php
function VerifyForm(&$values, &$errors)
{
if($values['fname'] == "")
{$errors['fname'] = 'empty';}
}
function DisplayForm($values, $errors)
{
?>
<html>
<head>
<title>Register</title>
</head>
<body>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
<table border="1">
<tr>
<td>First Name :</td>
<td><input type="text" name="fname" value="<?= htmlentities($values['fname']) ?>"/>
<td><?= $errors['fname'] ?></td>
</tr>
<tr><td colspan="2" align="center"><input type="submit" value="Submit"></td></tr>
</table>
</form>
</body>
</html>
<?php
}
function ProcessForm($values)
{
echo ("<p>Your record has been successfully added!</p>");
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$formValues = $_POST;
$formErrors = array();
if (!VerifyForm($formValues, $formErrors))
DisplayForm($formValues, $formErrors);
else
ProcessForm($formValues);
}
else
DisplayForm(null, null);
?>
$error['fname']is not defined. You should initialize it to get rid of the error message.