<?php
$errors=['email'=>'','title'=>'','ingredients'=>''];
$email=$title=$ingridients='';
if(isset($_POST['submit'])){
if(empty($_POST['email'])){
$errors['email']= 'email is empty <br>';
}else{
$email = $_POST['email'];
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$errors['email']= 'email is not valid <br>';
}
}
if(empty($_POST['title'])){
$errors['title']= 'title is empty <br>';
}else{
$title = $_POST['title'];
if(!preg_match('/^[a-zA-Z\s]+$/',$title)){
$errors['title']='title is not valid <br>';
}
}
if(empty($_POST['ingredients'])){
$errors['ingredients']= 'ingredients are empty';
}else{
$ingridients = $_POST['ingredients'];
if(!preg_match('/^([a-zA-Z\s]+)(,\s*[a-zA-Z\s]*)*$/',$ingridients)){
$errors['ingredients']= 'invalid type <br>';
}
}
if(array_filter($errors)){
echo 'there is/are an error/s';
}else{
header('Location: ../index.php');
}
}
?>
<!DOCTYPE html>
<html>
<?php include('../template/header.php'); ?>
<section class="container grey-text">
<h4 class="center">Add a Pizza</h4>
<form class="white" action="add2.php" method="POST">
<label>Your Email</label>
<input type="text" name="email" value="<?php echo htmlspecialchars($email); ?>">
<div class="red-text"><?php echo $errors['email']; ?></div>
<label>Pizza Title</label>
<input type="text" name="title" value="<?php echo htmlspecialchars($title); ?>" >
<div class="red-text"><?php echo $errors['title']; ?></div>
<label>Ingredients (comma separated)</label>
<input type="text" name="ingredients" value="<?php echo htmlspecialchars($ingridients); ?>">
<div class="red-text"><?php echo $errors['ingredients']; ?></div>
<div class="center">
<input type="submit" name="submit" value="Submit" class="btn brand z-depth-0">
</div>
</form>
</section>
<?php include('../template/footer.php'); ?>
</html>