with PHP I'm trying to check if the input fields are valid or not, while doing this I'm putting my codes into different php files and include them in the main php file(registerFunction.php), the reason I'm doing this is to keep things organised that will run on the click of a button but I get an error that the variable I declared in the registerFunction.php is undeclared in the regValidation.php file. Following is the error message I get: Undefined variable: regErrors in
my Code for registerFunction.php is:
<?php
//Create Error array
$regErrors = array();
//Add Date Functions
include "includes/functions/dateFunction.php";
//Checks if Register Screen Inputs are valid or not and pushes the errors into an array.
include "includes/functions/checkRegFields.php";
//
include "includes/functions/registryValidation.php";
if(regDecision()){
//register
}
else{
foreach ($regErrors as $errrorMessage) {
?>
<script>
$("#errors").append('<div class="alert alert-danger" role="alert"><?php echo $errrorMessage; ?></div>');
</script>
<?php
}
}
echo postDate();
?>
checkRegFields.php :
<?php
if(isset($_POST['registerButton'])){
if(($_POST['regEmail'])){
$regEmail = $_POST['regEmail'];
}
else{
array_push($regError,"Please Fill in the Email field");
}
if(($_POST['regUsername'])){
$regUsername = $_POST['regUsername'];
}
else{
array_push($regErrors,"Please Fill in the Username field");
}
if ((['regPassword']) and ($_POST['regPassword2'])) {
if( ($_POST['regPassword']) == ($_POST['regPassword2']) ){
$regPassword = $_POST['regPassword'];
}
else{
array_push($regErrors,"Passwords does not match!");
}
}
else{
array_push($regErrors,"Please Fill in the Password fields");
}
}
?>
and Finally I have registryValidation.php(where I'm having an error):
<?php
function regDecision(){
if(sizeof($regErrors)==0){
return true;
}
else{
return false;
}
}
?>
checkRegFields.php is fine with the $regError variable but registryValidation.php tells me that $regError is undeclared.
why is this?