I've created a signup.php file for html form and signup_form_handler.php for validate the html form. Before create a separate file for validation I tried to validate the html form in the same file (signup.php) and I succeeded. After I separate the validation part into another file I have this problem. How can i get back the validation errors from signup_form_handler.php to the html form file (signup.php) .
signup.php
<div class="container-fluid calbox col-md-4" style="margin-right: 10px;margin-bottom: 10px;">
<h2 style="text-align: center;color:#0275d8;">Sign Up Form</h2>
<form method="POST" action="signup_form_handler.php">
<div class="form-group">
<label style="padding-top: 10px;" class="col-form-label">Name</label>
<input type="text" class="form-control" placeholder="Type your name here" value="<?php if(isset($_POST['name'])){echo $_POST['name'];}?>" name="name" id="name">
<div class="errBox" style="padding-top: 5px;">
<?php echo $nameErr;?>
</div>
</div>
<div class="form-group">
<label class="col-form-label">Email</label>
<input type="email" class="form-control" placeholder="Type your email here" value="<?php if(isset($_POST['email'])){echo $_POST['email'];}?>" name="email" id="email">
<div class="errBox" style="padding-top: 5px;">
<?php echo $emailErr;?>
</div>
</div>
<div class="form-group">
<label class="col-form-label">City</label>
<select class="form-control" id="city" name="city[]">
<option value="Kandy">Kandy</option>
<option value="Colombo">Colombo</option>
<option value="Galle">Galle</option>
</select>
</div>
<fieldset class="form-group row">
<label class="col-form-label col-xs-4">Gender</label>
<div class="col-xs-7 form-check">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="gender" id="genMale" value="Male" checked>
Male
</label>
<label class="form-check-label">
<input type="radio" class="form-check-input" name="gender" id="genFemale" value="Female">
Female
</label>
</div>
</fieldset>
<div class="form-group">
<label class="col-form-label col-xs-4">Interested Areas</label>
<div class="col-xs-7 form-check">
<label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0 offset-xs-1">
<input type="checkbox" name="check[]" id="web" class="custom-control-input" value="Web Design">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Web Design</span>
</label>
<label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0">
<input type="checkbox" name="check[]" id="gd" class="custom-control-input" value="Graphic Design">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Graphic Design</span>
</label>
<label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0">
<input type="checkbox" name="check[]" id="se" class="custom-control-input" value="Software Engineering">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Software Engineering</span>
</label>
</div>
</div>
<div class="errBox" style="padding-top: 5px;">
<?php echo $checkErr;?>
</div>
<div class="form-group">
<label class="col-form-label">Phone Number</label>
<input type="tel" class="form-control" placeholder="Type your phone number here" value="<?php if(isset($_POST['phone'])){echo $_POST['phone'];}?>" name="phone" id="phone">
</div>
<div class="form-group">
<label class="col-form-label">Password</label>
<input type="password" class="form-control" placeholder="Type a password here" name="pass" id="pass">
<div class="errBox" style="padding-top: 5px;">
<?php echo $passErr;?>
</div>
</div>
<div class="form-group">
<label class="col-form-label">Confirm Password</label>
<input type="password" class="form-control" placeholder="Retype the password here" name="cpass" id="cpass">
<div class="errBox" style="padding-top: 5px;">
<?php echo $cpassErr;?>
</div>
</div>
<div class="form-group row offset-sm-8" style="padding-left: 10px;">
<button type="submit" class="btn btn-outline-primary" name="btn" value="signup" id="signupbtn">Register</button>
</div>
</form>
</div>
signup_form_handler.php
<?php
$nameErr = $emailErr = $checkErr = $passErr = $cpassErr = "";
$name = $email = $pass = $check = $checklist = $value = $gender = $city = $phone = $dateTime = "";
if (isset($_POST['btn'])) {
if (empty($_POST["name"])) {
$nameErr = '<div class="alert alert-danger">Name is required !</div>';
} else{
$name = test_validate($_POST['name']);
}
if (empty($_POST["email"])) {
$emailErr = '<div class="alert alert-danger">Email is required !</div>';
}else{
$email = test_validate($_POST['email']);
}
if (isset($_POST['phone'])) {
$phone = test_validate($_POST['phone']);
}
if (empty($_POST["pass"])) {
$passErr = '<div class="alert alert-danger">Password is required !</div>';
} elseif (!empty($_POST["pass"]) < 6) {
$passErr = '<div class="alert alert-danger">Minimum 6 characters required !</div>';
}
if (empty($_POST["cpass"])) {
$cpassErr = '<div class="alert alert-danger">Confirm password is required !</div>';
} elseif ($_POST["pass"] != $_POST["cpass"]) {
$cpassErr = '<div class="alert alert-danger">Password fields do not match !</div>';
}else{
$pass = test_validate($_POST['pass']);
}
if (empty($_POST['check'])) {
$checkErr = '<div class="alert alert-danger">You should at least select 1 area that you are interested !</div>';
}
if (isset($_POST['gender'])) {
$gender = test_validate($_POST['gender']);
}
if (isset($_POST['city'])) {
foreach ($_POST['city'] as $citylist) {
$city = $citylist;
}
}
date_default_timezone_set("Asia/Colombo");
$dateTime = date("Y-m-d h:i:s a");
}
function test_validate($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>