1

I have been trying to understand how jQuery works while checking the login details of a user. I have created these pages:

  1. db.php (DB Connection)
  2. signin.html (Login Form)
  3. check.php (Login check - MySQL select)
  4. myscript.js (jQuery)

Signin Form:

<script src="js/jquery.min.js"></script>
<script src="js/myscript.js"></script>

<div id="loginForm">
 <label>Please Login</label><br/>
 <form id="myForm" action="check.php" method="post">
 <input name="emailid" id="email" type="text" placeholder="Enter your Email ID"/><br/>
 <input name="password" id="pass" type="password" placeholder="Enter your password"/><br/>
 <button id="submit"> Login</button>
 </form>
</div>
<div id="ack"></div>

myscript.js

 $(document).ready(function (){
   $("#submit").click(function (){
        alert("hi");
        if($("#email").val()==""||$("#pass").val()=="")
            $("div#ack").html("please enter email and pass");
        else
            $.post( $("#myForm").attr("action"), 
                    $("#myForm :input").serializeArray(),
                    function(data){
                        $("div#ack").html(data);
                    });
        $("#myForm").submit( function(){
            return false;
        });
     });
    });

check.php

$email = $_POST['emailid'];
$pass = $_POST['password'];
include "db.php";
$sql = "select count(*)from students where email = '$email' and password = '$pass';";
$result = mysqli_query($con,$sql);
$row=mysqli_fetch_array($result);
if($row[0]>0)
    echo "found result";
else
    echo "no result";

Everything is working fine. The only problem is that the data is not getting posted. $_POST[] is receiving null values even though I'm entering the values.

Please help.

4
  • 2
    Little Bobby Tables says hello! Commented Sep 24, 2013 at 11:56
  • 2
    Check the data you are sending using $("#myForm :input").serializeArray() Is it really what you expect it to be? Commented Sep 24, 2013 at 11:59
  • try to use ajax for this it will be better way to check for login. Commented Sep 24, 2013 at 12:13
  • Learn to walk before running. Commented Sep 24, 2013 at 12:35

4 Answers 4

4

Changed the code and it's working now. Thanks for your inputs.

I was posting the wrong data.

  $(document).ready(function (){
  $("button#submit").click(function (){
        alert("hi");
        e = $("#email").val();
        p = $("#pass").val();
        if($("#email").val()==""||$("#pass").val()=="")
            $("div#ack").html("please enter email and pass");
        else
            $.post( $("#myForm").attr("action"), 
                    //$("#myForm :input").serializeArray(),
                    {emailid: e, password: p},
                    function(data){
                        $("div#ack").html(data);
                    });
        $("#myForm").submit( function(){
            return false;
        });
     });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Change form action

<form id="myForm" action="login.php" method="post">

to

<form id="myForm" action="check.php" method="post">

3 Comments

Oh! I have changed the name of action file only for the sake of simplicity over here. No minor mistakes.
@RiteshA try to simple echo any string at the beginning to whether really it submitted to check.php
Solved the problem. Posted the mistake too. Was sending wrong data from the post.
0

You should be escaping those inputs. You can never trust user input.

<?php
$name = mysqli_real_escape_string($_POST['emailid']);
$password = mysqli_real_escape_string($_POST['password']);
?>

keep in mind, you'll most likely need to include the active database connection.

You should be posting to the correct file, in your form you need to change the action from login.php to check.php

2 Comments

Yes! I'have now included this along with server side validations too.
It is working now. I was posting the wrong content. emailid: e, password: p did the job as explained in the last comment.
0

put action="javascript:void(0)" in form tag and everything works like great

1 Comment

sample: <form action="javascript:void(0)" method="post" name="frmlogin"> <table> <tr> <td>username</td><td><input type="text" name="user" id="user" ></td> </tr> <tr> <td>password</td><td><input type="password" name="password" id="passowrd" ></td> </tr> <tr><td><input type="submit" name="submit" onclick="checklogin();" ></td></tr> </table> </form>

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.