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.
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.
<?php
if('POST' === $_SERVER['REQUEST_METHOD']){
#rest of php code.
}
?>
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.
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";
}