Ajax call not working(Phonegap, Mysql, PHP)

I am developing an app using Phonegap and right now I am on making the login page. Whenever I try to click the submit button, nothing happens. Here's my code.

index.html

    <link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
<script src="js/jquery.min.js"></script>
<script src="js/login.js"></script>
<link href='//fonts.googleapis.com/css?family=Droid+Serif:400,400italic,700,700italic' rel='stylesheet' type='text/css'>
<!--<script>

</script>-->
</head>

<body>
    <div class="main-info2">
        <h3>Sign In</h3>
            <div class="in-form">
                <form id="login_form" method="post">
                    <input type="text" placeholder="Username" required=" " id="email" />
                    <input type="password" placeholder="Password" required=" " id="password" />
                </form>
                <div class="check-sub">
                    <input type="submit" value="Login" id="login">
                </div>
            </div>
        </div>
        <div class="copy-right">
            <p>Design by <a href="http://w3layouts.com">W3layouts</a></p>
        </div>
    </div>
    <!-- //main -->

login.js

$(document).ready(function(){
    do_login();
});

function do_login() {
    //$("#login").click(function () {
        var email = $('#email').val();
        var password = $('#password').val();
        var data =  $("#login_form").serialize();

        if(email != "" && password != ""){
            $.ajax({
                type: 'post',
                url: 'https://localhost:1234/cleverpro/login.php',
                data: data,
                success: function(response){
                    if(response == "success"){
                        console.log("yehey!");
                        window.location.href = "welcome.html";
                    }else{
                        alert("sayup uy");
                    }
                }
            });
        }else{
            alert("Please fill in ");
        }
    //});
    return false;
}

login.php

<?php

    session_start();

    if(isset($_POST['login'])){
        $host = "localhost";
        $username = "root";
        $password = "";
        $dbname = "cleverpro";
        $connect = mysql_connect($host,$username,$password);
        $db = mysql_select_db($dbname);


        $email = $_POST['email'];
        $pass = $_POST['password'];

        $sql = "SELECT * FROM user WHERE email='$email'";

        $recordset=mysqli_query($connect, $sql) or die("database error:".mysqli_error($connect));

        /*if($row=mysql_fetch_array($select_data)){
            $_SESSION['email']=$row['email'];
            echo "success";
        }else{
            echo "fail";
        }
        exit();*/

        $row = mysqli_fetch_assoc($recordset);

        if($row['password'] == $pass) {
            echo "success";
            $_SESSION['user_session'] = $row['id']
        }else{
            echo "fail";
        }
    }

?>

I don't know which part is wrong. Can you help me with this? Thanks in advance.

Answers 1

  • Your submit button is out of scope of <form> tag.

    Pls change the form tag as follows

    <form id="login_form" method="post">
          <input type="text" placeholder="Username" required=" " id="email" />
          <input type="password" placeholder="Password" required=" " id="password" />
          <div class="check-sub">
           <input type="submit" value="Login" id="login">
          </div>
     </form>
    

Related Articles