Undefined Index in simple form

Hi, I’m a newbie and struck at this point, please help me.

here is the code:

<?php

$submit =$_POST[“submit”];
$fname =strip_tags($_POST[“fullname”]);
$uname =strip_tags($_POST[“username”]);
$pwd =strip_tags($_POST[“password”]);
$cpwd = strip_tags($_POST[“cpassword”]);
$date = date(“Y-m-d”);

?>

<html>
<head>
<title>Registration Form</title>
</head>

<body>
<h1>Register New User</h1>

<form method=“post” action=“register.php” id=“register”>
<table>
<tr>
<td>
Your Full Name:
</td>
<td>
<input type=“text” name=“fullname” >
</td>
</tr>
<tr>
<td>
Choose Username:
</td>
<td>
<input type=“text” name=“username” >
</td>
</tr>
<tr>
<td>
Choose Password:
</td>
<td>
<input type=“password” name=“password” >
</td>
</tr>
<tr>
<td>
Confirm Password:
</td>
<td>
<input type=“password” name=“cpassword” >
</td>
</tr>
</table>
<div>
<input type=“submit” name=“submit” value=“Proceed!”>
</div>

</form>
</body>
</html>

And here are the warning messages:

Notice: Undefined index: submit in D:\wamp\www\Learning\Own\register.php on line 5

Notice: Undefined index: fullname in D:\wamp\www\Learning\Own\register.php on line 6

Notice: Undefined index: username in D:\wamp\www\Learning\Own\register.php on line 7

Notice: Undefined index: password in D:\wamp\www\Learning\Own\register.php on line 8

Notice: Undefined index: cpassword in D:\wamp\www\Learning\Own\register.php on line 9

“error_reporting(E_ALL ^ E_NOTICE);” suppresses it, but I want a solution of it, how can I get rid of it ?

You’re trying to address values which don’t exist in an array.

Take a quick peek at [fphp]isset/fphp. :wink:

Thanx I tried that too, and got this error message:

Here are the full code with error message:

Code:

$submit = isset($_POST[‘submit’]);
$fname = isset($_POST[‘fullname’]));
$uname = isset($_POST[‘username’]));
$pwd = isset($_POST[‘password’]));
$cpwd = isset($_POST[‘cpassword’]));

if ($submit){
echo “$fname $uname $pwd $cpwd”;
}

Error Message:

Parse error: parse error in D:\wamp\www\Learning\Own\register.php on line 17

And if I code like :

<?php

if(isset($_POST[‘submit’])){
$submit = $_POST[‘submit’];
}

if(isset($_POST[‘fullname’])){
$fname = $_POST[‘fullname’];
}

if(isset($_POST[‘username’])){
$uname = $_POST[‘username’];
}

if(isset($_POST[‘password’])){
$pwd = $_POST[‘password’];
}

if(isset($_POST[‘cpassword’])){
$cpwd = $_POST[‘cpassword’];
}

/$submit = isset($_POST[‘submit’]);
$fname = isset($_POST[‘fullname’]));
$uname = isset($_POST[‘username’]));
$pwd = isset($_POST[‘password’]));
$cpwd = isset($_POST[‘cpassword’]));
/

if ($submit){ ==========>This is the line 33
echo “$fname $uname $pwd $cpwd”;
}
?>

Then getting again the same warning message:
Notice: Undefined variable: submit in D:\wamp\www\Learning\Own\register.php on line 33

The script is working fine. but this notice is not going away.

Try this and we’ll go from there. :slight_smile:


<?php
if('POST' === $_SERVER['REQUEST_METHOD']){
    $post_to_variable_mapping = array(
        'fullname'  => 'fname',
        'username'  => 'uname',
        'password'  => 'pwd',
        'cpassword' => 'cpwd'
    );
    foreach($post_to_variable_mapping as $post_var => $local_var){
        #if post has the var
        if(true === isset($_POST[$post_var])){
            #assigning the var
            ${$local_var} = $_POST[$post_var];
            #debug
            printf('$&#37;s = %s<br />', $local_var, $_POST[$post_var]);
        }
    }
}
?>

$submit will never be set as it will never have any data as it’s the submit button, which the user clicks to submit the data.

Thanx buddy but this is going over my head. Ok here is my simple script and I’m getting the same problem.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?php
session_start()

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
</head>
<body>

<h2>Login and Validate yourself</h2>
<?php
//error_reporting(E_ALL ^ E_NOTICE);

$user = $_POST['user']; ------------------------>Line 19
$pass = $_POST['pass'];------------------------> Line 20
$links = "<a href='main.php'>Click here</a> to proceed to main page.<br /> <a href='logout.php'>Click here</a> to logout.";

if ($user && $pass){

	if ($logged_in_user == $user){ ----------->Line 25
	  echo "U are already logged in";
	  echo $links;
	  exit;
	}
	$connect = mysql_connect("localhost", "root", "");
	mysql_select_db("forum");
	$query = mysql_query("SELECT * FROM users WHERE username = '$user' AND password = '$pass'");
	if (!$query){
		echo "Sorry there is some error.";
		exit;
	}
	if (mysql_num_rows($query)>0){
		$logged_in_user = $user;
		session_register("logged_in_user");
		echo "Welcome $logged_in_user <br />";
		echo $links;
		exit;
	} else {
		echo "Invalid login, please try again";
		}
} else if ($user || $pass){
	echo "Please fill both fields <br />";
}
?>

<form method="post" action="login.php">
<p>
Username: 
<input type="text" name="user"/><br />
</p>
<p>Password:
 <input type="password" name="pass" /><br />
</p>
<input type="submit" value="login" />

</form>

</body>
</html>

Here are the errors:

Notice: Undefined index: user in D:\wamp\www\Learning\Forum\login.php on line 19

Notice: Undefined index: pass in D:\wamp\www\Learning\Forum\login.php on line 20

And if I’m applying wrong username or password then getting this error message:

Notice: Undefined variable: logged_in_user in D:\wamp\www\Learning\Forum\login.php on line 25

You’re getting

Notice: Undefined index: user in D:\wamp\www\Learning\Forum\login.php on line 19

Notice: Undefined index: pass in D:\wamp\www\Learning\Forum\login.php on line 20
because when you load the page, there is nothing in the $_POST array as no form data has been submitted.

Note:

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

Anthony I tried that in a test script and it gives GET when POST is used. This should work:

if (isset($_POST['user'])) {
    $user = $_POST['user'];
} else {
    $user = '';
}

if (isset($_POST['pass'])) {
    $pass = $_POST['pass'];
} else {
    $pass = '';
}

Intriguing!

How did you test it? What platform?

Versions:

Apache: 2.2.11
PHP: 5.9.9-2
MySQL: 5.1.33

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<?php

session_start()



?>



<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Login</title>

</head>

<body>



<h2>Login and Validate yourself</h2>

<form method="post" action="test30.php">

<p>

Username: 

<input type="text" name="user"/><br />

</p>

<p>Password:

 <input type="password" name="pass" /><br />

</p>

<input type="submit" value="login" />



</form>

<?php

var_dump($_SERVER['REQUEST_METHOD']);

/*
if('POST' === $_SERVER['REQUEST_METHOD']){
    echo 'It is POST';
} */


//error_reporting(E_ALL ^ E_NOTICE);


if (isset($_POST['user'])) {
    $user = $_POST['user'];
} else {
    $user = '';
}

if (isset($_POST['pass'])) {
    $pass = $_POST['pass'];
} else {
    $pass = '';
}



$links = "<a href='main.php'>Click here</a> to proceed to main page.<br /> <a href='logout.php'>Click here</a> to logout.";



if ($user && $pass){



    if ($logged_in_user == $user){ 

      echo "U are already logged in";

      echo $links;

      exit;

    }

    $connect = mysql_connect("localhost", "root", "");

    mysql_select_db("forum");

    $query = mysql_query("SELECT * FROM users WHERE username = '$user' AND password = '$pass'");

    if (!$query){

        echo "Sorry there is some error.";

        exit;

    }

    if (mysql_num_rows($query)>0){

        $logged_in_user = $user;

        session_register("logged_in_user");

        echo "Welcome $logged_in_user <br />";

        echo $links;

        exit;

    } else {

        echo "Invalid login, please try again";

        }

} else if ($user || $pass){

    echo "Please fill both fields <br />";

}

?>







</body>

</html>

var_dump() returns

string(3) “GET”

Well, you’re making a GET request though… :wink:

Try this buddy.


<html>
    <head>
        <title>Request Method Test</title>
    </head>
    <body>
        <h3>Current request method is defined as <span style="color: blue"><?php echo $_SERVER['REQUEST_METHOD']; ?></span></h3>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
        <input type="submit" name="submit" value="submit" />
        </form>
    </body>
</html>

Same result as the code in #11

Current request method is defined as GET

Click submit.

Thanx Anthony & Phoenix

For ur valuable posts but these things are bouncing over my head. Phoenix, ur code works for me, but it gives a message on the page, and as I’m a newbie dunno how to tackle that, while I tried a lot but could not be able to find a good solution to it. So I separated the html and php part and put those in two different files, and this is working fine for me as of now.

Since I’m in initial phase of learning, so it would be helpful if any of u can point out the reason, why I’m getting this warning notice, while I’ve seen lots of tutorials online, and none of them have got such error messages.

U both have also coded this kind of simple form. This is just a simple login and registration form, Can u point out why I’m getting these warning notices ?

You’re getting them because you’re trying to access variables in an array which aren’t present.

Accessing the $_POST superglobal when the form has not been submitted will do this. Check the request method as I described above, if it is POST, then you can access the elements defined in your form without these notices.

You can easily fix this with a simple if block. :slight_smile:


<?php
if('POST' === $_SERVER['REQUEST_METHOD']){
    #rest of php code.
}
?>


$fname =strip_tags($_POST["fullname"]);

The problem is that $_POST[“fullname”] does not exist until the form is posted.

Now try this.


if (isset($_POST["fullname"]) {
// now we know the form was posted and declare some vars
$submit = isset($_POST['submit']); 
$fname = isset($_POST['fullname']));
$uname = isset($_POST['username']));
$pwd = isset($_POST['password'])); 
$cpwd = isset($_POST['cpassword'])); 
}

If you want to work with both POST and GET you can use $_REQUEST[“fullname”]

Oh yea, you will not need the isset when declaring the vars.

Thanx guys really very much.

However Anthony ur code works for me here, thanx really for that.

Meanwhile lorenw I’d tried ur code and I got the same warning notice.

See the code on which I’ve tried. I tried altering the variables too, but still got the same error.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php

if (isset($_POST["fullname"])) { 
// now we know the form was posted and declare some vars 
$submit = $_POST['submit']; 
$fname = $_POST['fullname']; 
$uname = $_POST['username']; 
$pwd = $_POST['password']; 
$cpwd = $_POST['cpassword']; 
} 			

if ($submit){ //--------------------------------> Line 20.
	
	echo "$fname $uname $pwd $cpwd";
	
}
	

?>

<h1>Register New User</h1>

<form method="post" action="form.php" id="register">
  <table>
    <tr>
      <td>
      Your Full Name:
      </td>
      <td>
      <input type="text" name="fullname" >
      </td>
    </tr>
    <tr>
      <td>
      Choose Username:
      </td>
      <td>
      <input type="text" name="username" >
      </td>
    </tr>
    <tr>
      <td>
      Choose Password:
      </td>
      <td>
      <input type="password" name="password" >
      </td>
    </tr>
    <tr>
      <td>
      Confirm Password:
      </td>
      <td>
      <input type="password" name="cpassword" >
      </td>
    </tr>
  </table>
<div>
<input type="submit" name="submit" value="Proceed!">
</div>

</form>
</body>
</html>


And also they are not echo-ing the value, they are only returning only boolean values viz 1 only if I’m supplying any value to any of the form above.

I tried altering the code too like:

if (isset($_POST["fullname"])) { 
// now we know the form was posted and declare some vars 
$submit = isset($_POST['submit']); 
$fname = isset($_POST['fullname']); 
$uname = isset($_POST['username']); 
$pwd = isset($_POST['password']); 
$cpwd = isset($_POST['cpassword']); 
} 

or

if (isset($_POST["submit"])) { 
// now we know the form was posted and declare some vars 
$submit = isset($_POST['submit']); 
$fname = isset($_POST['fullname']); 
$uname = isset($_POST['username']); 
$pwd = isset($_POST['password']); 
$cpwd = isset($_POST['cpassword']); 
} 

But the problem still remains there.

and I’m getting the error message on line 20

Error message
Notice: Undefined variable: submit in D:\wamp\www\Learning\form.php on line 20

on line 20

if ($submit){ //--------------------------> This is line 20.
	
	echo "$fname $uname $pwd $cpwd";
	
}

If the form isn’t submitted then $submit on line 20 won’t exist as it is only set if (isset($_POST[“submit”])) {

To get around that you can either define it

$submit = false;
if (isset($_POST["submit"])) { 
//...
}
if($submit) // will always be set so no error

or use empty()

if(empty($submit)) // will be false if not yet set, no error

thanx hash…

and thanx anthony, ur tip really helped me.:slight_smile: