Vineet Basantani Vineet Basantani - 6 months ago 13
jQuery Question

Login is not executing

I've made a login page, but it isn't working.
My HTML is:

<form name="form" class="login-form" method="POST">
<input type="text" placeholder="username" name="userID" id="userID"/>
<input type="password" placeholder="password" id="password" name="password"/>
<input type="button" id="login" name="login" value="login" onclick="redirect()"></input>


My JS is:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function redirect(){
$("#login").click(function(){
var user = $('#userID').val();
var passwrd = $('#password').val();
if (user === '' || passwrd === ''){
alert("Please Fill Required Fields");
}
else{
<?php confirm();?>
var userID="<?php echo $username?>";
var pwd="<?php echo $pwd?>";
if(userID===user && pwd===passwrd)
{
window.location.href = 'http://localhost/Annapoorna/Welcome_Page.php';
}
else
{
alert("Please enter correct username and password!");
}
}
});
});
</script>


And PHP is:

<?php
function confirm(){
try{
$username="";
$pwd="";
$pword="";
$servername = "localhost";
$conusername = "root";
$conpassword = "";
$dbname = "annapoorna";
$dsn='mysql:dbname='.$dbname.';host='.$servername;
$conn = new PDO($dsn, $conusername, $conpassword);
$get_pass=$_POST(['password']);
if(!empty($get_pass))
{
$sqlselect='Select * from users where password=:pass';
$sth=$conn->prepare($sqlselect);
$sth->execute(array(':pass'=>$get_pass));
foreach ($sth->fetchAll() as $row) {
$username=$row['username'];
$pwd=$row['password'];
}
}
}
catch(PDOException $e){
echo "Error: " . $e->getMessage();
}}
?>


It works only for a static value of password put in the query against ':pass'. Why is it not working dynamically?
So I think the problem is in fetching data from front-end.
Pray, tell me what evil spirits are hovering over my code.

Answer

You can use like this

<form name="form" class="login-form" method="POST"> 
      <input type="text" placeholder="username" name="userID" id="userID"/>
      <input type="password" placeholder="password" id="password" name="password"/>
      <input type="button" id="login" name="login" value="login" onclick="redirect()"></input>
</form>
<script type="text/javascript">
$(document).ready(function redirect(){
    $("#login").click(function(){
        var user = $('#userID').val(); 
        var passwrd = $('#password').val();
        if (user === '' || passwrd === ''){
            alert("Please Fill Required Fields");
        } 
        else {
            $.ajax({
                url: "validate.php",
                type: "post",
                data : {
                    username: user,
                    password: passwrd
                },
                success: function(data){
                    alert(data);
                    if(data=="ok"){
                        window.location.href="http://localhost/Annapoorna/Welcome_Page.php";
                    }
                }
            });
        }
    });
});
</script>

and validate.php file

<?php
$servername = "localhost";
$conusername = "root";
$conpassword = "";
$dbname = "annapoorna";
$dsn='mysql:dbname='.$dbname.';host='.$servername;
$conn = new PDO($dsn, $conusername, $conpassword);

$username=$_POST['username'];
$password=$_POST['password'];       

$sqlselect="Select * from users where username='".$username."' and password='".$password."'";
$sth=$conn->prepare($sqlselect);
$sth->execute();

$status="";
foreach ($sth->fetchAll() as $row) {
    $status="ok";
}
echo $status;
?>