1

I'm not able to connect with database using jquery ajax.

The below is the login1.html page

    <form method='POST'>
                <div class="username">
                    <input type="text" id='name' name="name" class="form-control" placeholder="Enter your username">
                    <i class="glyphicon glyphicon-user"></i>
                </div>
                <div class="password">
                    <input type="password" id='password' name='password' class="form-control" placeholder="Enter your password">
                    <i class="glyphicon glyphicon-lock"></i>
                </div>
                <div id="error"></div>
                <div class="custom-radio-checkbox">
                    <input tabindex="1" type="checkbox" class="bluecheckradios">
                    <label>Remember me</label>
                </div>
                <script>
                    $(document).ready(function(){
                      $('.bluecheckradios').iCheck({
                        checkboxClass: 'icheckbox_flat-blue',
                        radioClass: 'iradio_flat-blue',
                        increaseArea: '20%' // optional
                      });
                    });     
                </script>
                <input type="submit" class="btn btn-primary style2" name='submit' id="submit" value="Sign In">
            </form>

The form value is transferred through jquery ajax

<script>

$(document).ready(function(){

   // jQuery methods go here...


$('#submit').click(function(){

    var username = $('#name').val();
    var pass = $('#password').val();

    var datastring = {username:username,pass:pass};
    if($.trim(username).length>0 && $.trim(pass).length>0){
        $.ajax({
        type: "POST",
        url : "process/login.php",
        data : datastring,
        cache: false,
        beforeSend : function(){
            $("#submit").val('Connectiing...');
        },
        success:function(data){
            if(data){
                window.location.href('http://www.google.com');
            }
            else{
                $('#loginone').shake();
                $('#submit').val('Login')
                $("#error").html("<span style='color:red'>Error:</span>Invalid Username or Password");
            }
        }

        });
    }
return false;

});

});

</script>

** This is the data connection page process/login.php**

<?php

    include_once('config.php');

    if(isset($_POST['submit'])){
        $username = $_POST['username'];
        $pass = $_POST['pass'];
        $sql = "select id from login where login_name = '".$username."' and login_password = '".$pass."' ";
        $result = $conn->query($sql);
        if($result->num_row > 0){

            echo "connected";
        }



    }//EOF SUBMIT

?>

When ever i pass the correct credentials i get an error for wrong username and password.

8
  • 2
    maybe your ajax not having submit input and u are using if(isset($_POST['submit'])){ in php and sending just var datastring = {username:username,pass:pass}; Commented Nov 8, 2016 at 10:40
  • change if(isset($_POST['submit'])){ as if(count($_POST) > 0) will work Commented Nov 8, 2016 at 10:42
  • What's your XHR response content ? Check what's you are getting in data in success:function(data){ Commented Nov 8, 2016 at 10:44
  • now u have few solutions try them Commented Nov 8, 2016 at 10:57
  • strange but none of them is working :-( Commented Nov 8, 2016 at 11:12

3 Answers 3

1

You are not sending the value of the submit over the ajax change your if to

 if(isset($_POST['username']) && isset($_POST['pass']) ){ 

or add the value to the ajax:

datastring = {username:username,pass:pass,submit:"true"};
Sign up to request clarification or add additional context in comments.

Comments

1

You are getting wrong username and password because your PHP not returning anything in ajax success.

Whats wrong here:

You are sending username and password in ajax request but checking by using submit input which is wrong because submit is not the part of ajax data.

You can also check what are you getting in ajax request in php as:

print_r($_POST);

Here, you can solve the problem as:

if(count($_POST) > 0) // check $_POST array > 0 or not..

instead of:

if(isset($_POST['submit'])){ // submit not available in $_POST

Comments

0

The object data you are sending through Ajax to the PHP script does not have a key called "submit" and hence this part of the code will fail

if(isset($_POST['submit'])){

Just remove that if statement and you will be fine... You can equally check whether you have data within the $_POST super global like devpro suggested in the comment below your question or just check for only the fields you sent within the $_POST variable.

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.