Results 1 to 7 of 7

Thread: Undefined variable:

  1. #1
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Undefined variable:

    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:
    Code:
    <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:

    Code:
    <?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
    Last edited by djr33; 10-16-2011 at 09:45 PM.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Please make your code more readable by using the forum's bbcode tags:
    • [code]
    • [html]
    • [php]


    PHP Code:
    <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.

  3. #3
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi Adrian,

    Where should we define the $username?

    Thks,

    Mrudul

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    since this is a form field, aren't you wanting the user to provide the username? I'd just leave it out, personally.

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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']; }
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. The Following User Says Thank You to djr33 For This Useful Post:

    mrudul (10-24-2011)

  8. #7
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks Daniel

    It is resolved.

    Mrudul

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •