Log in

View Full Version : Undefined variable:



mrudul
10-16-2011, 12:11 PM
Hi,

I am a beginner on PHP.

I am writing a script to check if the Login button is pressed and calling another PHP script to perform this task, for that I have written 2 script i.e. basicForm2.php and submitForm.php.

While executing the script it is giving an error:

"( ! ) Notice: Undefined variable: username in C:\wamp\www\basicForm2.php on line 11 Call Stack #TimeMemoryFunctionLocation 10.0013363320{main}( )..\basicForm2.php:0 "> "

The script is as follows:

1. basicForm2.php:

<html>
<head>
<title>A BASIC HTML FORM</title>

</head>

<body>

<FORM NAME ="form1" METHOD ="POST" ACTION="submitForm.php">

<INPUT TYPE = "TEXT" Name = 'username' VALUE="<?PHP print $username ; ?>">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Login">

</FORM>
</body>
</html>


2. submitForm.php:


<?PHP

if (isset($_POST['Submit1'])) {

$username = $_POST['username'];

if ($username = = "letmein") {
print ("Welcome back, friend!");
}
else {
print ("You're not a member of this site");
}
}
else {
$username ="";
}

?>


Please help me to resolve this issue.

Thanks in advance,

Mrudul

traq
10-16-2011, 03:49 PM
Please make your code more readable by using the forum's bbcode tags: [code] [html] [php]


<html>
<head>
<title>A BASIC HTML FORM</title>

</head>

<body>

<FORM NAME ="form1" METHOD ="POST" ACTION="submitForm.php">

<INPUT TYPE = "TEXT" Name = 'username' VALUE="<?PHP print $username ; ?>">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Login">

</FORM>
</body>
</html>it means exactly what it says - you're trying to print $username, but you never defined it. it doesn't exist.

mrudul
10-17-2011, 05:22 AM
Hi Adrian,

Where should we define the $username?

Thks,

Mrudul

djr33
10-17-2011, 01:01 PM
If you want to use a variable, it must be defined. So you need to define it before you use it.

What you're doing now is similar to asking the computer "What is the value of X?" without giving any sort of information about a formula. Of course if you tell the computer that "X=5" then the computer can easily tell you that, but only if you have done this first.

So, for example, at the very beginning of your script you could set $username to some value, if you know what to use at that point.


If you don't know what to use on this page or it may not be set sometimes, then you can give it a default value at the beginning of the page:
$username = "";

That will set it to a blank value (an empty string). You can always change it later, but by having that default value it will avoid the error.

Still, logically I don't see why you'd want that if you don't intend to define it: if you're using a blank value it won't help anything. So maybe you don't need that variable in this part of your script? Or maybe you just need to add more code related to the username to this page and it will all fit together.


Technically the undefined variable error is just a warning; PHP can go on without it, but it's helpful to find problems in the code and it's a good thing to fix.

traq
10-17-2011, 02:35 PM
since this is a form field, aren't you wanting the user to provide the username? I'd just leave it out, personally.

djr33
10-18-2011, 12:25 AM
Usually that method is used for forms that may have already been submitted: if there was some error (for example, the password was wrong) the form will be displayed again but you don't need to retype all of the info.

But to do that you'd need to somehow get the submitted value, such as
$username = "";
if (isset($_POST['username'])) { $username = $_POST['username']; }

mrudul
10-24-2011, 04:33 PM
Thanks Daniel

It is resolved.

Mrudul