This chapter will introduce how to ensure that all fields are entered correctly before the user clicks the "Submit" button to submit data.
After the user clicks the submit button, to ensure that the field values are entered correctly, we insert a PHP script into the HTML input element. Each field is named: name, email, and website. In the textarea field in the note, we place the script between the <textarea> and </textarea> tags.
The PHP script output values are: $name, $email, $website, and $comment variables.
Then, we also need to check the selected radio button. For this, we must set the checked attribute (not the value attribute of the radio button):
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> Name: <input type="text" name="name" value="<?php echo $name;?>"> <span>* <?php echo $nameErr;?></span> <br><br> E-mail: <input type="text" name="email" value="< ?php echo $email;?>"> <span>* <?php echo $emailErr;?></span> <br><br> Website: <input type="text" name="website" value="<?php echo $website;?>"> <span><?php echo $websiteErr;?></span> <br><br> Note: <textarea name="comment" rows="5" cols="40"> <?php echo $comment;?></textarea> <br><br> Gender: <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo " checked";?> value="female">female<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male<span>* <?php echo $genderErr;?></span> <br><br> <input type="submit" name="submit" value="Submit"> </ form>