0

I am using php based login form with ajax and jquery following is my login form

<form role="form" id="login_Form" action="login.php" method="POST">
            <fieldset>
                <h2>Please Sign In</h2>
                <hr class="colorgraph">
                <div class="form-group">
                    <input type="text" name="login_username" id="username" class="form-control input-lg" placeholder="Username">
                </div>
                <div class="form-group">
                    <input type="password" name="login_password" id="password" class="form-control input-lg" placeholder="Password">
                </div>
                <hr class="colorgraph">

<div id="ack"> </div>

                <hr class="colorgraph">
                <div class="row">
                    <div class="col-xs-6 col-sm-6 col-md-6">
                       <!-- <input type="submit" class="btn btn-lg btn-success btn-block " id="submit" value="Sign In"/> -->
                        <button type="submit" id="submit" class="btn btn-lg btn-success btn-block" name="submit">Login</button>
                    </div>
                    <div class="col-xs-6 col-sm-6 col-md-6">
                        <a href="" class="btn btn-lg btn-primary btn-block">Register</a>
                    </div>
                </div>
            </fieldset>
        </form>

and my ajax code for login in

$("button#submit").click(function () {

if ($("#username").val() == "" || $("#password").val() == "")

    $("div#ack").html("Please Enter Username and Password !");

else 

    $.post($("#login_Form").attr("action"),
    $("#login_Form:input").serializeArray(),
    function (data) 
    {

        $("div#ack").html(data);

    });     

    $("#login_Form").submit(function () {

    return false;
    });




});

and my php code for login is :

<?php

$mysql_id = mysql_connect('localhost', 'root', '');
mysql_select_db('msbte', $mysql_id);



$username = ($_POST['login_username'];
$password = $_POST['login_password'];   

$sql = "select count(*) from login_user where (username='$username' and password='$password')"; 

$result = mysql_query($sql);

$row = mysql_fetch_array($result);

if($row[0] > 0) 

echo "Login Successful !";

else 

echo "Login Failed !";


?>

Whenever i login using my username and password it gives error as

Notice: Undefined index: login_username in /opt/lampp/htdocs/login.php on line 7

Notice: Undefined index: login_password in /opt/lampp/htdocs/login.php on line 8 Login Failed !

Can anbody tell me whats wrong with my code.

3
  • you have done wrong code. First try to search from google. wrong hints in 2 points. 1) form action. 2) check javascript part Commented Jan 27, 2014 at 7:58
  • Please escape the data before using it in a Query, or use prepared statements (PDO or Mysqli). You'r script is open for sql-injections. Commented Jan 27, 2014 at 8:03
  • Also, it looks like you are using plain text passwords. You should never do this, see here for more: sitepoint.com/password-hashing-in-php. Sorry, not the point of your question but just trying to help you out :) Commented Jan 27, 2014 at 8:06

5 Answers 5

1

Try this, You need use isset()

<?php 
 if(isset($_POST['login_username']) && isset($_POST['login_password'])){
    $username = $_POST['login_username'];
    $password = $_POST['login_password'];       
    $sql = "select count(*) from login_user where (username='$username' and password='$password')";     
    $result = mysql_query($sql);        
    $row = mysql_fetch_array($result);      
    if($row[0] > 0) 
        echo "Login Successful !";      
    else        
        echo "Login Failed !";

 }else{
  echo "no pot values";

}

?>  
Sign up to request clarification or add additional context in comments.

Comments

1

$ _POST or $ _GET are two special functions of PHP that are used to get variables from a user-filled form. While using these functions, a user may encounter an error - Notice: Undefined index. This error can be avoided with the help of PHP isset (). This error will be notified, but that depends on the configuration of the server. Notice: Undefined index is a minor error and hence not notified by default. With the help of the error_reporting function, the type of error reported can be changed.

if (isset($_POST['login_username']) && isset($_POST['login_password']))    
{    
          // Instructions if $_POST['login_username'] and $_POST['login_password'] exist 
}    

You should also consider using PHP PDO. (see more: http://www.php.net/manual/en/book.pdo.php)

Comments

1

I suggest you 3 things to change little bit:

1. Move the login form submit function outside of button's context

$("#login_Form").submit(function () {
    return false;
});

The above piece of code should be outside of button's click.

2. change this line:

$("#login_Form:input").serializeArray(),

to this:

$("#login_Form").serializeArray(),

3. Now in your php part you have to to do this:

$username = ($_POST['login_username'];
       //---^-------------------------here is some issue, remove it '('
       // so it should be like below one

$username = $_POST['login_username'];

Comments

1

Seems like you have a ( too much on this line:

$username = ($_POST['login_username'];

Also some changes to your jQuery

$("button#submit").click(function () {
    if ($("#username").val() == "" || $("#password").val() == "") {
        $("div#ack").html("Please Enter Username and Password !");
    } else {
        $.post($("#login_Form").attr("action"),
        $("#login_Form").serializeArray(),

        function (data) {
            $("div#ack").html(data);
        });
    }

    return false;
});

Comments

0
$("#login_Form:input").serializeArray(),

has to be

$("#login_Form input").serializeArray(),

also check network dev tools tab to be sure the right post params are sent to the server

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.