Date Posted: 09/01/2019

In this post we will and validate a simple form using HTML and PHP.

Step 1: Create the html form using following code.

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
 <form  method="POST" enctype="multipart/form-data" id="alumini-form">	

 Name <input type="text" name="name" value="">
Address <input type="text" name="address" value="">
Email <input type="text" name="email" value="">
<input type="radio" name="howMany" value="zero"> 0
<input type="radio" name="howMany" value="one"> 1
<input type="radio" name="howMany" value="two"> 2
<input type="radio" name="howMany" value="twoplus"> More than 2
<select name="favFruit[]" size="4" multiple>
 <option value="apple">Apple</option>
 <option value="banana">Banana</option>
 <option value="plum">Plum</option>
 <option value="pomegranate">Pomegranate</option>
 <option value="strawberry">Strawberry</option>
 <option value="watermelon">Watermelon</option>
</select>
<input type="checkbox" name="brochure" value="Yes">
<input type="submit" name="submit" value="Submit">

</form>



</body>
</html>

Step 2: Please use the following code to validate the php forms. You can add the below code before html code.

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        $nameErr = "Missing";
    }
    else {
        $name = $_POST["name"];
    }

    if (empty($_POST["address"])) {
        $addrErr = "Missing";
    }
    else {
        $address = $_POST["address"];
    }

    if (empty($_POST["email"]))  {
        $emailErr = "Missing";
    }
    else {
        $email = $_POST["email"];
    }

    if (!isset($_POST["howMany"])) {
        $howManyErr = "You must select 1 option";
    }
    else {
        $howMany = $_POST["howMany"];
    }

    if (empty($_POST["favFruit"])) {
        $favFruitErr = "You must select 1 or more";
    }
    else {
        $favFruit = $_POST["favFruit"];
    }
}


Thank you! for using PHEONIX SOLUTIONS.

You find this tutorial helpful? Share with your friends to keep it alive. Be the first to comment, we value your suggestions. For further queries please comment below.

Leave a Reply