简体   繁体   中英

PHP login and registration form error handling

I want to make a nice error message inside of PHP and HTML like we make in bootstrap code, instead of showing a message in echo(). but I don't want to use any function to make it.

<?php 
include 'condb.php';


if (isset($_POST['sub'])) {

$name = $_POST['name'];
$lname =$_POST['lname'];
$email =$_POST['email']; 
$pass1 =$_POST['pass1'];
$pass2 =$_POST['pass2'];
if (empty($name) || empty($lname) || empty($email) || empty($pass1) || 
empty($pass2)) {
    echo "<p class='alert alert-danger'> must fill all </p>";
}else{

if (empty($pass1) || empty($pass2)) {
echo "you must fill password";

 }
else{

if ($pass1 == $pass2) {
    $hashpass = md5($pass1);

    $query="insert into users values (null, '$name', '$lname', '$email', 
'$hashpass')";
    $result=mysqli_query($conn,$query);

    echo "success";

}else{
    echo "passwords must be same";
}

}
}
}

?>

Store the message in a session variable like $_SESSION["errormsg"]='you must fill password';

Now you can echo the content of session variable in HTML like

<div id='alert_msg'><?php if(isset($_SESSION["errormsg"])){ echo $_SESSION["errormsg"]; }?></div> 

Don't forget to include session_start() at the beginning of your script and unset the session after printing the content otherwise, it will show you the exact same content over and over.

// remove all session variables
 session_unset(); 

 // destroy the session 
 session_destroy(); 

if you work in different pages one is pure php and the another is embedded with the HTML, you make a session in php page and save your values in session and use the session in html page.

<?php 
include 'condb.php';
session_start();
if (isset($_POST['sub'])) {

$name = $_POST['name'];
$lname =$_POST['lname'];
$email =$_POST['email']; 
$pass1 =$_POST['pass1'];
$pass2 =$_POST['pass2'];
if (empty($name) || empty($lname) || empty($email) || empty($pass1) || 
empty($pass2)) {
    $_SESSION["errormsg"]='you must fill password';
}else{

if (empty($pass1) || empty($pass2)) {
 $_SESSION["errormsg"] = "you must fill password";
 }
else{

if ($pass1 == $pass2) {
    $hashpass = md5($pass1);

    $query="insert into users values (null, '$name', '$lname', '$email', 
'$hashpass')";
    $result=mysqli_query($conn,$query);

    echo "success";

}else{
    $_SESSION["errormsg"] = "passwords must be same";
}

}
}
}

?>

in your html code use the session above.

<div id='alert_msg'><?php if(isset($_SESSION["errormsg"])){ echo $_SESSION["errormsg"]; }?></div> 

create a Separate file like messages.php and include it in your code like in header.php or in the separate page you want.

Now in that message.php file, you can define all the messages you want to, It is easier to change and update them later on. and easier to use them anywhere

Example

define("ADVANCE_ADDED","Advance detail has been added successfully.");
define("ADVANCE_UPDATED","Advance detail has been updated successfully.");
define("ADVANCE_REMOVED","Advance detail has been removed successfully.");
define("APPROVE_LV_MSG","Are you sure you want to approve this leave?");
define("LV_APPROVED","You have approved the leave successfully.");
define("LV_REJECTED","You have rejected the leave successfully.");

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM