Home > database >  I have make a php page but this error is not going event he syntax is correct
I have make a php page but this error is not going event he syntax is correct

Time:11-09

function message()
{
    if (isset($_SESSION['message'])) {
        if ($_SESSION['message'] == "signup_err_password") {
            echo "   <div class='alert alert-danger' role='alert'>
        please enter the password in correct form !!!
      </div>";
            unset($_SESSION['message']);
        } elseif ($_SESSION['message'] == "loginErr") {
            echo "   <div class='alert alert-danger' role='alert'>
        The email or the password is incorrect !!!
      </div>";
            unset($_SESSION['message']);
        } elseif ($_SESSION['message'] == "usedEmail") {
            echo "   <div class='alert alert-danger' role='alert'>
        This email is already used !!!
      </div>";
            unset($_SESSION['message']);
        } elseif ($_SESSION['message'] == "wentWrong") {
            echo "   <div class='alert alert-danger' role='alert'>
        Something went wrong !!!
      </div>";
            unset($_SESSION['message']);
        } elseif ($_SESSION['message'] == "empty_err") {
            echo "   <div class='alert alert-danger' role='alert'>
        please don't leave anything empty !!!
      </div>";
            unset($_SESSION['message']);
        } elseif ($_SESSION['message'] == "signup_err_email") {
            echo "   <div class='alert alert-danger' role='alert'>
        please enter the email in the correct form !!!
      </div>";
            unset($_SESSION['message']);
        }
    }
}

enter image description here

This kind of error is displayed were ever i use this message function the same message is displayed wen eve i tried to import the message function in the program

CodePudding user response:

I believe next code snippet will help you. First of all you have saw an error because you don't ensure element with that index exists. Secondary, I have refactore the code to make it easily to read and extend, please take a look

<?php

function message()
{
    $messageMap = [
        "signup_err_password" => "lease enter the password in correct form !!!",
        "loginErr"            => "The email or the password is incorrect !!!",
        "usedEmail"           => "This email is already used !!!",
        "wentWrong"           => "Something went wrong !!!",
        "empty_err"           => "please don't leave anything empty !!!",
        "signup_err_email"    => "please enter the email in the correct form !!!"
    ];
    
    if (array_key_exists('message', $_SESSION)) {
        if(array_key_exists((string)$_SESSION['message'], $messageMap)) {
            printf("<div class='alert alert-danger' role='alert'>%s</div>", $messageMap[$_SESSION['message']]);
            unset($_SESSION['message']);
        }
    }
}

CodePudding user response:

there are no errors in the code you provided. most likely you copied it from the wrong file. Or maybe you didn't have this check before

if (isset($_SESSION['message']))

then you added it and forgot to upload it to your server.

CodePudding user response:

<?php session_start();
//for checking this session
$_SESSION['message']="wentWrong";

function message($message)
{
    $res="";$msg="";
    switch($message)
    {
        case "signup_err_password":
            $msg="please enter the password in correct form !!!";   
        break;
        case "loginErr":
            $msg=" The email or the password is incorrect !!!";  
        break;
        case "usedEmail":
            $msg="This email is already used !!!";  
        break;
        case "wentWrong":
            $msg=" Something went wrong !!!";  
        break;
        case "empty_err":
            $msg="please don't leave anything empty !!!";  
        break;
        case "signup_err_email":
            $msg="please enter the email in the correct form !!!";  
        break;
        default:
            $msg="";
        break;
    }
    $res="<div class='alert alert-danger' role='alert'>".$msg."</div>";
    return $res;   
}
$msg = message($_SESSION['message']);
echo $msg;
?>
  • Related