i am new in php and i am working on a login and registration form . I created a function to check if the email address is valid or not even though i did't get any errors but it don't seem to work. It would be a great help if anyone here could take a look at my code.
<?php
$firstname="";
$lastname="";
$email="";
$password="";
$confirm_password="";
$error= array("firstname"=>"",
"lastname" =>"",
"email" =>"",
"password" =>"",
"confirm_password"=>"" );
function correct_email(){
if(!empty($email)){
if(filter_var($email,FILTER_VALIDATE_EMAIL)){
return true;
}else{
echo"your email address is not valid";
}
}
return false;
}
function correct_password(){
global $error;
if(empty($error['password'])){
if(strcmp($_POST['password'],$_POST['confirm_password'])==0){
return true;
}else{
$error['password']="Password didnot match";
$error['confirm_password']="Password didnot match";
}
}
return false;
}
function validate(){
global $error;
$valid= true;
foreach($_POST as $key=>$value){
if(empty($value)){
$valid=false;
$error[$key]="This field is blank";
}
}
return correct_password() && correct_email() && $valid;
}
if($_SERVER['REQUEST_METHOD']=="POST"){
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$password=$_POST['password'];
$confirm_password=$_POST['confirm_password'];
if(validate()){
echo "registration succesful";
die;
}
else{
echo "couldnot register";
}
}
?>
<html>
<head>
<title>Registration</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<h1>New Memebers Registration</h1>
<form method="post" action="register.php">
<table>
<tr>
<td>Firstname:</td>
<td><input type="text" name="firstname" value="<?= $firstname ?>">
<?= $error['firstname'] ?> </td>
</tr>
<tr>
<td>Lastname:</td>
<td><input type="text" name="lastname" value="<?= $lastname ?>">
<?= $ error['lastname'] ?> </td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" value="<?= $email ?>">
<?= $error['email'] ?> </td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" value="<?= $password ?>">
<?= $error['password'] ?> </td>
</tr>
<tr>
<td>Confirm Password:</td>
<td><input type="password" name="confirm_password"
value="<?= $confirm_password ?>">
<?= $error['confirm_password'] ?> </td>
</tr>
<tr>
<td colspan="2" class="pullright">
<input type="submit" value="Register"> </td>
</tr>
</table>
</form>
</body>
</html>