I'm adding a html form to my website. I have an issue with the form validation. I'm trying to display error message when the user does not writte is email address and still submit the form. The thing is: I'm trying to do it on the same page: so my code, as it is now, directly display the error message when the page is loaded (which is normal because the user did not enter is email address yet).
I would like this message to display on the same page (so here: on registerForm.php) only if the user clicked on the submit btn but did not enter his email address. Could someone help me please?
I put the code bellow:
registerForm.php
`
<?php
session_start();
//include a javascript file
echo "<script type='text/javascript' src='registerForm.js'></script>";
?>
<div class="divCenter">
<div class="registerform-container">
<form action="successForm.php" class="registerform-form" method="POST">
<div>
<label id="emailInput" for="email" class="registerform-form-label">Email</label>
<div>
<input type="email" class="registerform-form-control" id="email" name="email" aria-describedby="email-subscribe" placeholder="you@exemple.com">
<div id="email-help" class="registerform-form-text">L'email utilisé lors de la création de compte.</div>
</div>
</div>
<button id="button" class="registerform-btn disabled" type="submit">Send</button>
</form>
<?php
if(isset($_SESSION['statut'])){
?>
<div class="success">
<p><?php echo htmlspecialchars($_SESSION['statut']) ?></p>
</div>';
<?php
//une fois afficher il faut détruire le session
unset($_SESSION['statut']);
}
?>
</div>
</div>
successForm.php
<?php
//include CSS Style Sheet
echo "<link rel='stylesheet' type='text/css' href='registerForm.css' />";
//include a javascript file
echo "<script type='text/javascript' src='rfffff.js'></script>";
?>
<?php
if(!isset($_SESSION))
{
session_start();
}
?>
<?php
if(!isset($_POST['email']))
{
echo 'you must enter your email address'; return;
}
try
{
$db = new PDO('mysql:host=localhost;dbname=mariawebsite;charset=utf8', 'root', 'root');
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
$email = $_POST['email'];
// Préparation
$insertEmail = $db->prepare("INSERT INTO users_addresses(email) VALUES (:email)");
// ajouter la ligne ci-dessous pour être sûr d'afficher toutes les erreurs.
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// Exécution ! La recette est maintenant en base de données
$insertEmail->execute([
'email' => $email,
]);
$_SESSION['statut'] = "$email submited successfully";
header('Location: home.php');
?>
`
I would like this message to display on the same page (so here: on registerForm.php) only if the user clicked on the submit btn but did not enter his email address. Could someone help me please?
Try this: On successForm.php
if(!isset($_POST['email']) || empty($_POST['email']))
{
$msg = 'you must enter your email address';
header('Location: registerForm.php?msg='.$msg); // this will redirect to register form with message
} else {
//try catch and other code here
}
On registerForm.php After and before form starts. Add this:
<?php if(isset($_GET['msg']) && $_GET['msg'] != "") {?>
<div class="msg_error">
<p><?php echo $_GET['msg']?></p>
</div>
<?php } ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.