简体   繁体   中英

Notice: Undefined index: register (error trying to make registration form?)

Notice: Undefined index: register on line 12.

This is some old website code that I'm going through to fix - I'm a noob at this.

Originally I was using isset() , but was forced to use null!==

if (isset($_SESSION['id'])) {    
    echo "<fieldset><legend><b>Error!</b></legend>";
    echo "You are already logged in, therefore you can not register a new account!";
    echo "</fieldset>";
} else {
    if (null !== (!$_POST['register'])) {
        echo "<fieldset><legend><b>Registration</b></legend><br />";
        echo "Fill out all of these fields below in order to create a new account. This account will be used to enter, so you are advised not to share the information with anyone at all!<br /><br />";
        echo "<form method=\"POST\">";

I expect to get a working registration form, but get this. I get the same undefined index error in other files as well in similar scenarios.

Replace

if (null !== (!$_POST['register']))

with

if (isset($_POST['register']) && $_POST['register'] != '') // to check not empty

or if (isset($_POST['register']))

Checking isset() will check if the index is defined. If needed to check if any value exists in the index check $_POST['register'] != ''

While using such scenarios it is always best to check if the index is present and the value in the index is defined. This will save a lot of headaches.

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