Results 1 to 4 of 4

Thread: Small issue

  1. #1
    Join Date
    Jan 2014

    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:
    <title>A BASIC HTML FORM</title>
    	$user = $_POST['Username'];
    	if ($user == "admin") {
    		print ("Welcome to duty admin");
    	else {
    		print("You are not allowed here");
    <FORM NAME ="form1" METHOD ="Post" ACTION = "basicForm.php">
    <input type="Text" name="Username" value="Username">
    <input type="Submit" name="Submit1" value="Login">
    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.


  2. #2
    Join Date
    Jan 2014
    Boston, Massachusetts
    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
    The problem persists :/

  4. #4
    Join Date
    Aug 2004
    $_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()).
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

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