TransWikia.com

Notice Messages Do Not Appear On The Page

Stack Overflow Asked by cifhai on December 24, 2020

I’m working with PHP OOP to develop my custom management system. Basically, I created a page which Admins can add another custom admin. Here is the code:

if (isset($_POST['submit'])){
    $username = $_POST['uname'];
    $email = $_POST['email'];
    $password = $_POST['pass'];
    $groups = $_POST['groups'];
    if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
        $notice['email_validation'] = "The email that you have entered is not a valid one";
    }else{
        $registration = new Admin();
        $notice[] = $registration->CheckUname($username,$email,$password,$groups,$level);   
    }   
    $notice = $registration->getNotice();
}

As you can see at the end, I had specified a variable called $notice which is calling a method getNotice() and it goes like this:

public function getNotice()
{
    return $this->notice; 
}

And the notice variable here is set protected. So the form itself works fine but it just don’t show me any error/success message.

Actually I had added also these codes at the main page that contains the form in order to output the error/success messages:

if(isset($notice['email_validation'])) {
    echo "
    <div class='alert alert-danger'>
        <strong>Hey!</strong> ".$notice['email_validation'].".
    </div>
    ";
}
if(isset($notice['username_exists'])) {
    echo "
    <div class='alert alert-danger'>
        <strong>Hey!</strong> ".$notice['username_exists'].".
    </div>
    ";
}
if(isset($notice['email_exists'])) {
    echo "
    <div class='alert alert-danger'>
        <strong>Hey!</strong> ".$notice['email_exists'].".
    </div>
    ";
}
if(isset($notice['success_message'])) {
    echo "
    <div class='alert alert-success'>
        <strong>Hey!</strong> ".$notice['success_message'].".
    </div>
    ";
}

So if you know what am I doing wrong, please let me know I would really appreciate that.

Also note that the page does not return any PHP errors while the error reporting is on!

One Answer

It seems like the following code is the problem:

if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
    $notice['email_validation'] = "The email that you have entered is not a valid one";
} else {
    $registration = new Admin();
    $notice[] = $registration->CheckUname($username,$email,$password,$groups,$level);   
}   

$notice = $registration->getNotice();

The $notice[] you use inside the if statement is populated with the message you have in the first case or the output of the $registration->CheckUname output.

Then, you replace the contents of the $notice[] with the output of the $registration->getNotice();


Unfortunately, I cannot say 100% this is the problem, as your code could override the value of the $notice in other places, or I don't know what else you could do with the rest of your code, but so far, to me, this is the problem you have.

Try to remove the line $notice = $registration->getNotice(); and see if your code works. If it does, then you should try to re-write your code logic to operate as you desire, either by populating an array with notice values, or use a notice container class to collect all the notices, and return an array later using the $registration->getNotice().

Answered by KodeFor.Me on December 24, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP