-1

I just setup a new server trying to echo my login button to alert (user & pass) just for a test and I'm getting an error message.

I've tried reviewing the undefined index, but I can't seem to find the issue

login.php

<?php include '../connection/db.php'; ?>
<?php

function login(){
    GLOBAL $db;
    if($_GET['login_form'] && $_GET['login_form'] == 'true'){
        echo $email = $_POST['login_email'];
        echo $password = $_POST['login_password'];
    }
}
login();

?>

undefined index: login_email

undefined index: login_password


index.php

<div class="card-body">
                    <form id="login-submit-form">
                        <div class="form-group">
                            <input type="email" name="login_email" id="login-email" class="form-control" placeholder="Enter E-Mail...">
                            <div class="login-email-error error"></div>
                        </div><!-- form group -->
                        <div class="form-group">
                            <input type="password" name="login_password" id="login-password" class="form-control" placeholder="Choose Password..."><div class="login-password-error error"></div>
                        </div><!-- form group -->
                        <div class="form-group">
                            <button type="button" id="login-submit" class="btn btn-success btn-block form-btn">Login</button>
                        </div><!-- form group -->
                        <div class="form-group"><a href="#" id="signup">Create New Account?</a>
                        </div>
                        </form> <!-- form -->

JavaScript

// === Submit the login form === //
$("#login-submit").click(function(){
    if(email.length == ""){
        $("#login-email").addClass("border-red");
        $("#login-email").removeClass("border-green")
        $(".login-email-error").html("Email is required!");
        email = "";
    }if(password.length == ""){
        $("#login-password").addClass("border-red");
        $("#login-password").removeClass("border-green")
        $(".login-password-error").html("Password is required!");
        password = "";
    }
    if(password.length !=  "" && email.length != ""){
        $.ajax({
            type    : 'POST',
            url     : 'ajax/login.php?login_form=true',
            data    : $("login-submit-form").serialize(),
            // dataType : "JSON",
            success     : function(feedback){
                alert(feedback);
            }
        })
    }
})


})

I expect just for it echo the username and password when I hit submit.

Emma
  • 27,428
  • 11
  • 44
  • 69
Ghost
  • 13
  • 6
  • 1
    You are missing a `#` in `$("login-submit-form").serialize()` – bcr666 Apr 03 '19 at 03:34
  • add method="post" in your form – Beginner Apr 03 '19 at 03:35
  • `if(isset($_POST['login_email'],$_POST['login_password'],$_GET['login_form']) && $_GET['login_form'] == 'true)` - and yes you can use multiple arguments with isset. – ArtisticPhoenix Apr 03 '19 at 03:38
  • @Beginner If the OP were using a SUBMIT button, that would be a case, but the OP is using JS and AJAX to do the submission, bypassing the standard form submit. – bcr666 Apr 03 '19 at 03:39
  • This function `function login()` is very ugly to me ... What I mean is it should take arguments instead of directly accessing the request. As it is, it's pointless to even make it a function. But if it was me I would make a User class with methods in it, and then use `login.php` like a controller, with my own proprietary template system. – ArtisticPhoenix Apr 03 '19 at 03:40
  • **WARNING**: Writing your own access control layer is not easy and there are many opportunities to get it severely wrong. Please, do not write your own authentication system when any modern [development framework](http://codegeekz.com/best-php-frameworks-for-developers/) like [Laravel](http://laravel.com/) comes with a robust [authentication system](https://laravel.com/docs/master/authentication) built-in. At the absolute least follow [recommended security best practices](http://www.phptherightway.com/#security) and **never store passwords as plain-text** or a weak hash like **SHA1 or MD5**. – tadman Apr 03 '19 at 03:42

1 Answers1

1

I think you should check $("login-submit-form").serialize() has contain data. I bet it is empty string. if you change the $("login-submit-form").serialize() to $("#login-submit-form").serialize() on ajax setup object it will work.