Jump to content

Can not use isset to fix undefined index


Go to solution Solved by benanamen,

Recommended Posts

I have a form to submit a email address and password. I get an undefined index error and if I try to use isset to fix the undefined index error I get the following error Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in and I have tried to use the suggestions to solve the problem  but than another error pops up. 

 

Here is my php code . All help greatly appreciated. 

 

if (isset($_POST['submit']=="Sign Up")) {
  
if (!$_POST['email']) $error.="<br />Please enter your email";
else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) $error.="<br />Please enter a valid email"; 
 
 
  if (!$_POST['password']) $error.="<br />Please enter your password";
 
and my html
 
<form class="marginTop" method="post"> 
 
  <div class="form-group">
 
  <label for="email">Email Address</label>
 
  <input type="email" name="email" class="form-control" placeholder="Your Email" value="<?php echo addslashes($_POST['email']); ?>" />
    
</div>
 
<div class="form-group">
 
  <label for="password">Password</label>
 
  <input type="password" name="password" class="form-control" placeholder="Password" value="<?php echo addslashes($_POST['password']); ?>" />
 
</div>
 
  <input type="submit" name="submit" value="Sign Up" class="btn btn-success btn-lg marginTop"/> 
 
  </form>

 

  • Solution

Change 

if (isset($_POST['submit']=="Sign Up"))

To

if ($_SERVER['REQUEST_METHOD'] == 'POST')

You are also trying to use variables in your form without checking if those variables exist.

Edited by benanamen
  • Like 1

As to the original problem: The code makes no sense, because you can only check if a variable is set. You're trying to apply isset() to the logical expression $_POST["submit"] == "Sign Up", which cannot be interpreted in any meaningful way.

 

What you probably meant to write is a combination of two checks:

// is the submit parameter set, and does its value say "Sign Up"?
if (isset($_POST['submit']) && $_POST['submit'] == 'Sign Up')
{
    ...
}

However, this is very poor practice, because now your entire processing script relies on one stupid submit button and its exact label. Simply checking for the HTTP POST method as demonstrated by benanamen makes a lot more sense.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.