I have made an html form and validate it with php or more precisely part of validation (just check rquired field) Here is the html codes:
<div class="container">
<div class="row">
<div class="col-md-6">
<h3>Registration Form</h3><br>
<form action="#" method="post">
<div class="form-group">
<label for="name">Name : * </label><span class="error"><?php echo $nameErr; ?></span>
<input id="name" type="text" name="name" class="form-control" placeholder="Type your name here">
</div>
<div class="form-group">
<label for="username">Username : * </label><span class="error"><?php echo $usernameErr; ?></span>
<input id="username" type="text" name="username" class="form-control" placeholder="Type your username here">
</div>
<div class="form-group">
<label for="password">Password : * </label><span class="error"><?php echo $passwordErr; ?></span>
<input id="password" type="password" name="password" class="form-control" placeholder="Type password here">
</div>
<div class="form-group">
<label for="rpassword">Retype Password : * </label><span class="error"><?php echo $rpasswordErr; ?></span>
<input id="rpassword" type="password" name="rpassword" class="form-control" placeholder="Retype password here">
</div>
<div class="form-group">
<label for="email">Email : * </label><span class="error"><?php echo $emailErr; ?></span>
<input id="email" type="email" name="email" class="form-control" placeholder="Type your email here">
</div>
<div class="form-group">
<label for="phone">Phone No : * </label><span class="error"><?php echo $phoneErr; ?></span>
<input id="phone" type="text" name="phone" class="form-control" placeholder="Type your phone here">
</div>
<div class="form-group">
<label for="dob">Date of Birth : * </label><span class="error"><?php echo $dobErr; ?></span>
<input id="dob" type="date" name="dob" class="form-control">
</div>
<div class="form-group">
<label>Select A Country : * </label><span class="error"><?php echo $countryErr; ?></span>
<input id="country" type="text" name="country" class="form-control not-me">
</div>
<div class="form-group">
<label for="gender">Please Select Your Gender : *</label><span class="error"><?php echo $genderErr; ?></span>
<div class="form-control gender">
<input type="radio" id="male" name="gender">
<label for="male" class="exclude">Male</label>
<input type="radio" id="female" name="gender">
<label for="female" class="exclude">Female</label>
</div>
</div>
<input type="submit" name="submit" class="custom-btn" value="Submit" name="reg-form">
</form>
</div>
</div>
</div>
And here is the php code to check if any of the field is empty:
<?php
$nameErr = $usernameErr = $passwordErr = $rpasswordErr = $emailErr = $phoneErr = $dobErr = $countryErr = $genderErr = "";
$name = $username = $password = $rpassword = $email = $phone = $dob = $country = $gender = "";
if(isset($_POST["submit"])) {
if (empty($_POST["name"])) {
$nameErr = " This field is required";
}
else {
$name = $_POST["name"];
}
if (empty($_POST["username"])) {
$usernameErr = " You must enter a username";
}
else {
$username = $_POST["username"];
}
if (empty($_POST["password"])) {
$passwordErr = " You must type a strong password";
}
else {
$password = $_POST["password"];
}
if (empty($_POST["rpassword"])) {
$rpasswordErr = " Your password didn't match";
}
else {
$rpassword = $_POST["rpassword"];
}
if (empty($_POST["email"])) {
$emailErr = " This is not a valid email address";
}
else {
$email = $_POST["email"];
}
if (empty($_POST["phone"])) {
$phoneErr = " Please enter your contact number";
}
else {
$phone = $_POST["phone"];
}
if (empty($_POST["dob"])) {
$dobErr = " You must fill your Date of Birth";
}
else {
$dob = $_POST["dob"];
}
if (empty($_POST["country"])) {
$countryErr = " You didn't select a country";
}
else {
$country = $_POST["country"];
}
if (empty($_POST["gender"])) {
$genderErr = " You can not leave this field empty";
}
else {
$gender = $_POST["gender"];
}
}
?>
Now I simply want to show a success message after submission the form if there is no empty field.
N.B: This is my first php work.
Thanks in advance!