1

I am trying to get a username and password from an HTML form using $_POST method, but i am getting an undefined index error with given input.

the form portion of my index file is as follows

<form class="form-signin"  role="form"action="" method="post">
        <h2 class="form-signin-heading">title<em class='current'>title</em></h2>
        <input id="username" type="username" class="form-control" placeholder="Username" required autofocus>
        <input id="password" type="password" class="form-control" placeholder="Password" required>
        <input name ="submit" type="submit" value="sign in">
      </form>

i also have my php script (called auth.php) included in the top of my index file.

this is my auth.php script code sample

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
echo "auth is called";
if(isset($_POST['submit'])){
echo "submit is pressed";
//this if statement enters when submit is pressed
if(empty($_POST['username']) || empty($_POST['password'])) {
echo "Username or Password is empty";
//this if statement enters too, even with input given
}
else{
echo "YAY";
}
}
?>

The "username or password is empty" statement keeps getting printed, even when I enter in a username and password into the input fields. What is the problem

1
  • I didn't have input name defined in the form, not sure how I missed that. Thank you for the help everyone! Commented Nov 18, 2014 at 5:26

5 Answers 5

5

Missing "name" attribute in input fields, it should be

EDIT 2 : Missing type="text" in username input

 <input type="text" name='username' id="username" class="form-control" placeholder="Username" required autofocus>
 <input name='password' id="password" type="password" class="form-control" placeholder="Password" required>
Sign up to request clarification or add additional context in comments.

2 Comments

Also, there is no input type username. It should be text.
@CodingAnt you have 2 types attr. in the first input!
0

Correct your form , 1. Name missing, 2. No datatype with the name of username

<form class="form-signin" role="form" action="" name="loginform" method="post">
    <h2 class="form-signin-heading">title<em class='current'>title</em></h2>
    <input id="username" name="username" type="text" class="form-control" placeholder="Username" required autofocus>
    <input id="password" name="password" type="password" class="form-control" placeholder="Password" required>
    <input name="submit" type="submit" value="sign in">
</form>

Comments

0
<input id="username" name="username" type="username" class="form-control" placeholder="Username" required autofocus>
            <input id="password" type="password" name="password" class="form-control" placeholder="Password" required>

    <?php
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    echo "auth is called";
    if(isset($_POST['submit'])){
    echo "submit is pressed";
    //this if statement enters when submit is pressed
    if(empty($_POST['username']) || empty($_POST['password'])) {
    echo "Username or Password is empty";
    //this if statement enters too, even with input given
    }
    else if(empty($_POST['username']){
      echo "Username  is empty";
    }
    else if(empty($_POST['password']){
      echo "Password  is empty";
    }
    else{
    echo "YAY";
    }
    }
    ?>

Comments

0

input type = text and name attr required

 <input name='username' id="username" type="text" class="form-control" placeholder="Username" required autofocus>

Check values using print_r() or var_dump if you are not getting the desired output.

This will help you to check which part of your code malfunctions. If you have done so, you would have found that there's something wrong only in your html form content, as you wont be having any field content printed and you would have easily sorted it out...

Comments

0

Set the username input's type to "text":

<input name='username' id="username" type="text" class="form-control" placeholder="Username" required autofocus>
<input name='password' id="password" type="password" class="form-control" placeholder="Password" required>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.