0

I have a login form and script on index.php. The problem is that the PHP code doesn't seem to pick up the fields I put in.

Here is the PHP code:

<?php
    if(isset($_POST['user'])||isset($_POST['pass'])){
        ?>
            <script>
                $('#login-drop').addClass('open');
                console.log('Opening');
            </script>
        <?php
        if(isset($_POST['user'])){
            if(isset($_POST['pass'])){
                login();
            } else{
                ?>
            <script>
                $('#pass-group').addClass('has-error');
                console.log('Password has error.');
            </script>

                <?php
            }
        } else {
            ?>
            <script>
                $('#user-group').addClass('has-error');
                console.log('User has error');
            </script>
            <?php
        }
    } else {
        echo 'Nothing was entered'; 
    }

    function login(){
        echo 'Both username and password was entered.';
    }
?>

Here is my HTML form:

        <form class="form-horizontal" method="post" action="?login">
              <fieldset>
                <legend>Log in</legend>
                <div class="form-group" id="user-group">
                  <label for="user" class="col-lg-2 control-label">Username</label>
                  <div class="col-lg-10">
                    <input class="form-control" id="user" placeholder="Username or Email" type="text">
                  </div>
                </div>
                <div class="form-group" id="pass-group">
                  <label for="pass" class="col-lg-2 control-label">Password</label>
                  <div class="col-lg-10">
                    <input class="form-control" id="pass" placeholder="Password" type="password">
                    <div class="checkbox">
                      <label>
                        <input type="checkbox"> Remember me
                      </label>
                    </div>
                  </div>
                 </div>
                 <div class="form-group">
                    <div class="col-lg-10 col-lg-offset-2">
                    <button class="btn btn-primary" type="submit">Log in</button>
                    </div>
                 </div>
                </fieldset>
        </form>

Even if I enter the fields it still says Nothing was entered. I can't see the problem but I tend to overlook things a lot.

PS. I'm using Bootstrap so the classes should work. I've tested them.

user2864740
  • 60,010
  • 15
  • 145
  • 220
Brandon Nguyen
  • 345
  • 2
  • 3
  • 15
  • 3
    It picks up only those fields which have a `name` attribute. `id` isn't used when the field is submitted. –  Jun 21 '14 at 19:16
  • what's the output of `var_dump($_POST)`? – arielnmz Jun 21 '14 at 19:16
  • 1
    You are missing the input names and hence the POST cant read your data. – Abhik Chakraborty Jun 21 '14 at 19:17
  • (This problem could have been isolated if using the browser developer tools to inspect transmitted data; this in turn would have led to a more useful title/description..) – user2864740 Jun 21 '14 at 19:17
  • http://stackoverflow.com/questions/854205/php-post-not-working?lq=1 (good duplicate) , http://stackoverflow.com/questions/14220473/post-not-passing-any-variables?rq=1 , http://stackoverflow.com/questions/23940291/php-not-able-to-access-post-values?rq=1 – user2864740 Jun 21 '14 at 19:19

2 Answers2

4

You need to give your input element names, otherwise their values do not get submitted. ids may also be useful for your JavaScript code, but they don't solve that problem.

So what you want to do is

<input name="user" id="user" ... >
Jon
  • 428,835
  • 81
  • 738
  • 806
0

You haven't added "name" attribute to your <input> tag ie

<input class="form-control" id="user" placeholder="Username or Email" type="text" name="user">
DWX
  • 2,282
  • 1
  • 14
  • 15