Given the following code example, what would I need to add to the check_input function so that it deals with missing / required form fields. Basically, all I am trying to do is to show the end user an error message on the top of my form that says something like “Fields marked with a * are required” if they try to submit the form without filling out all the required fields.
Any help would be greatly appreciated and thank in advance for your time.
// Don't post the form until the submit button is pressed.
if(isset($_POST['submit'])) {
echo(
check_input($_POST['name']) . <br> .
check_input($_POST['city']);
}
// check_input function
function check_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data, ENT_QUOTES);
return $data;
}
The Form
<form action="test.php" method="post">
<input type="text" name="name">
<input type="text" name="city">
<input type="submit" name="submit" value="submit">
</form>
PS: I noticed a quote mismatch in your form HTML. The method should read
method="post", notmethod="post'.