views:

41

answers:

3

I have the following error:

Notice: Undefined index: submit in C:\wamp\www\registration\register.php on line 6

Can't seem to work out whats wrong??? Here's the code::

    <?php
    //Create registration form (register.php)
    include "../includes/db_connect.php";

    if(!$_POST['submit']) ///Line 6
    {
    ?>
    <html>
    <head><link rel="stylesheet" href="style.css"></head>

    <div class="divider">
    <strong>Register</strong><br/><br/>
    <form method="post" action="register.php">

    <div class="formElm">
    <label for="first">First Name</label>
    <input id="first" type="text" name="first">
    </div>

    <div class="formElm">
    <label for="last">Last Name</label>
    <input id="last" type="text" name="last">
    </div>

    <div class="formElm">
    <label for="username">Desired Username</label>
    <input id="username" type="text" name="username">
    </div>

    <div class="formElm">
    <label for="password">Password</label>
    <input id="password" type="password" name="password">
    </div>

    <div class="formElm">
    <label for="pass_conf">Confirm Password</label>
    <input id="pass_conf" type="password" name="pass_conf">
    </div>

    <div class="formElm">
    <label for="email">Email</label>
    <input id="email" type="text" name="email">
    </div>

    <div class="formElm">
    <label for="about">About</label>
    <textarea id="about" cols="30" rows="5" name="about">Tell us about yourself</textarea>
    </div>

    <input type="submit" name="submit" value="Register">
    </form>

    or <a href="index.php">Login</a>
    </div>
    </html>
    <?php
    }
    else
    {
    $first = protect($_POST['first']);
    $last = protect($_POST['last']);
    $username = protect($_POST['username']);
    $password = protect($_POST['password']);
    $pass_conf = protect($_POST['pass_conf']);
    $email = protect($_POST['email']);
    $about = protect($_POST['about']);
    $errors = array();
    $regex = "/^[a-z0-9]+([_\.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\.[a-z]{2,}$/i";
    if(!preg_match($regex, $email))
    {
      $errors[] = "E-mail is not in name@domain format!";
    }

    if(!$first || !$last || !$username || !$password || !$pass_conf || !$email || !$about)
    {
       $errors[] = "You did not fill out the required fields";
    }

    $sql = "SELECT * FROM `users` WHERE `username`='{$username}'";
    $query = mysql_query($sql) or die(mysql_error());

    if(mysql_num_rows($query) == 1)
    {
      $errors[] = "Username already taken, please try another";
    }
    if(count($errors) > 0)
    {
      echo "The following errors occured with your registration";
      echo "<font color=\"red\">";
      foreach($errors AS $error)
      {
        echo "<p>" . $error . "\n";
      }
      echo "</font>";
      echo "<a href=\"javascript:history.go(-1)\">Try again</a>";
      //we use javascript to go back rather than reloading the page 
      // so the user doesn't have to type in all that info again.
    }
    else
    {
      $sql = "INSERT into `users`(`first`,`last`,`username`,`password`,`email`,`about`)
      VALUES ('$first','$last','$username','".md5($password)."','$email','$about');";

     $query = mysql_query($sql) or die(mysql_error());
     echo "Thank You for registering {$first}! Your username is {$username}";
     echo "<a href=\"index.php\"> Click here </a> to Login";
    }
    }

?>
+2  A: 

If there is no POST parameter at all or if there is no parameter named submit then you're trying to access an array index that does not exists, hence the warning. You can simply test if there is such an index/element in the _POST array.

if( isset($_POST['submit']) )

It doesn't check the value (like you original script, which tests if the value of _POST['submit'] equals false, see type juggling), but the mere existence of the index/element should suffice in this case.

see http://docs.php.net/isset

VolkerK
Just for completeness: "It doesn't check the value" isn't completely true. `$v=null; isset($v)`=>false, but that doesn't matter in this case, the elements in _POST are strings, maybe empty strings but still strings.
VolkerK
A: 

To get rid of this error, it should be:

if(!isset($_POST['submit']))

However, your code is already OK.

What you are getting is not an error, it is a warning, which is caused by having strict warnings enabld. PHP is a dynamic language which does not usually require to define variables and array keys, and most documentation and code will skip this part. So you should consider turning this feature off, as it clutters code and has few additional benefits. Or, switch to a statically compiled language (say asp.net) which will really benefit from defined variables and static typing.

Palantir
Turning off E_NOTICE is usually a bad idea.
Yorirou
A: 

Your $_POST does not exist when you first load your page. Change your check to something like:

if(!isset($_POST["submit"]))

Because you did not post anything yet, there will be no "submit" key in your $_POST array. That's what causes the warning.

Stegeman