www.webdeveloper.com
Results 1 to 4 of 4

Thread: Small issue

Hybrid View

  1. #1
    Join Date
    Jan 2014
    Posts
    4

    Small issue

    Hello guys,

    I tried to make a simple login script that prints "Welcome to duty admin" if the username that the user typed is "admin". Otherwise, the message would be "Unauthorized". Nonetheless, I can't figure out why it gives me an error when I first run the script.

    This is the code:

    HTML Code:
    <html>
    <head>
    <title>A BASIC HTML FORM</title>
    <?PHP
    	$user = $_POST['Username'];
    	if ($user == "admin") {
    		print ("Welcome to duty admin");
    	}
    	else {
    		print("You are not allowed here");
    	}
    ?>
    </head>
    <body>
    
    <FORM NAME ="form1" METHOD ="Post" ACTION = "basicForm.php">
    
    <input type="Text" name="Username" value="Username">
    <input type="Submit" name="Submit1" value="Login">
    
    </FORM>
    </body>
    </html>
    The error:

    Notice: Undefined index: Username in C:\wamp\www\basicForm.php on line 5
    Call Stack
    # Time Memory Function Location
    1 0.0000 139824 {main}( ) ..\basicForm.php:0

    P.S: It also says "Unauthorized" at the beginning even though I haven't pressed the Login button.

    Thanks!

  2. #2
    Join Date
    Jan 2014
    Location
    Boston, Massachusetts
    Posts
    66
    Don't know if this is the problem but get rid of the spaces: FORM NAME =, METHOD =, and ACTION = "". Should be NAME="" METHOD="" ACTION="". I don't know if it's a bigger problem but that stuck out to me like a sore thumb.

  3. #3
    Join Date
    Jan 2014
    Posts
    4
    The problem persists :/

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,361
    $_POST['Username'] won't be set when you initially access the page, only when you actually submit the form. So if you are going to do it as a single script, then you need to wrap the code that does the test of the username in an IF block that tests whether the form field exists (either with isset() or !empty()).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles