I'm coding a website and I have a form. For example, when the user is writing an email in the email input and he writes "benjamin" there's automatically a window saying it's not valid (even before submitting) same if he writes "benjamin@". However if he inserts "benjamin@gmail" no window is shown and the user can submit the form. The problem os that "benjamin@gmail" is not a valid mail adress so the controller does not accept it and the user is redirected to a blank page showing "invalid e-mail" because of the echo function of the controller. How can I find an alternative for the user not to be redirected to this page?
Here how it looks like in the html file:
<div>
<label for="email">Adresse mail :</label>
<input type="email" name="email" id="email" placeholder="josephine.dupont@gmail.com" required/>
</div>
and here is the controller part:
function pageInscription2() {
if ($_POST["nom"] && $_POST["prenom"] && $_POST["email"] && $_POST["telephone"]
&& $_POST["cle"] && $_POST["adresse"]
&& $_POST["code_postal"] && $_POST["pays"]
&& $_POST["mdp"] && $_POST["mdp2"])
{
$nom = htmlspecialchars($_POST["nom"]);
$prenom = htmlspecialchars($_POST["prenom"]);
$email = htmlspecialchars($_POST["email"]);
$telephone = htmlspecialchars($_POST["telephone"]);
$cle = htmlspecialchars($_POST["cle"]);
$adresse = htmlspecialchars($_POST["adresse"]);
$code_postal = htmlspecialchars($_POST["code_postal"]);
$pays = htmlspecialchars($_POST["pays"]);
$mdp = htmlspecialchars($_POST["mdp"]);
$mdp2 = htmlspecialchars($_POST["mdp2"]);
if (filter_var($email, FILTER_VALIDATE_EMAIL))
{
if ($mdp==$mdp2)
{
insererUtilisateur($nom, $prenom, $email, $telephone, $cle, $adresse, $code_postal, $pays, $mdp);
require "vues/inscription2.php";
}
else {
echo "Vous n'avez pas saisi les mêmes mot de passe";
} }
else {
echo "Email invalide";
} }
else {
$erreur = "Tous les champs doivent être complétés"; }
}