I would like my program to check if the user has typed anything into the input box, if not then it would ask the user to do so, otherwise it would submit my form to another page, display.html.
The problem here is that it just submit my form before validating if the user has typed anything into the input box.
How do I get it check the input box first before submit the form?
Here’s my code:
[code=html]<!doctype html>
<html lang=”en”>
<head>
<style>.error {color: #FF0000;}</style>
</head>
<?php
// define variables and set to empty values
$nameErr = $name = “”;
if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
if (empty($_POST[“name”])) {
$nameErr = “Name is required”;
} else {
$name = $_POST[“name”];
}
}
?>
<body>
<form name=”search_form” id=”searchForm” method=”POST” action=”display.html”>
Name: <input type=”text” name=”myName” id=”myName”>
<span class=”error”>* <?php echo $nameErr;?></span><br><br>
<button id=”submit”>Submit</button>
</form>
</body>
</html>