我无法使用PHP(pdo)& AJAX

时间:2017-09-05 12:01:10

标签: php ajax

我正在使用框架7,我正在尝试使用PHP(PDO)和Ajax登录,但是,每次遇到“登录失败”,即使在电子邮件和密码输入中插入了正确的数据,没有错误出现,我能够使用PHP(PDO)注册,一切都很好,但根本无法登录,请告知我的代码有什么问题

index.php

<div class="pages navbar-through" class="login-screen">
<div data-page="index" class="page no-navbar no-toolbar">
<div class="page-content" style="background:url(img/bg1.png) ;background-
size: 100%">
<div class="content-block-title center">
</div>


<span style="float: right;color: white;margin:20px;font-size: 20px">
<b>عربي</b></span>


<div class="list-block" style="margin:100px 70px auto 70px;">

<img src="img/logo2.png" style="height: 55px ;width: 100%;margin-bottom: 
60px;text-align: center;">
<div class="item-input" style="border-bottom: 1px solid gray">
<input type="text"  placeholder="Email" id="email" name="email" style="font-
size: 15px;color: white;" required>
</div>

<div class="item-input" style="border-bottom: 1px solid gray;margin-top: 
10px">
<input type="password" name="password" placeholder="password" id="password" 
style="font-size: 15px;color: white"; required>
</div>



<div style="text-align: center ;margin-top: 30px"> 
<a href="#" style="color:  #00bcd4 ;font-size: 15px">Forgot your password ?
</a>
<span id="login_message"><img src="img/ajax-loader.gif" style="display: 
none;"></span>

<center><button style="margin-top: 20px; background-color:  #00bcd4;border-
radius: 0px;height: 45px; width: 240px; color: white; text-align: center;" 
name="login-btn" class="list-button" id="login-btn" name="login-btn">Login</button></center>



<p style="color: white;font-size: 13px;margin-top: 40px"> You don't have 
account? </p> 
<a href="registeration.php" style="color: #ffeb3b ;font-size: 
13px">REGISTER</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

my-App.js

$(document).ready(function() {
$('#login-btn').click(function () {
var email = $('#email').val();
var password = $('#password').val();

if($.trim(email).length > 0 && $.trim(password).length > 0)
{
$.ajax({
url:"login.php",
method: "POST",
data: {email:email, password:password},
cache: false,
beforeSend:function()
{
$('#login-btn').val("connecting...");
},
success:function(data){
if(data){
myApp.alert('Login successful');
}
else{
myApp.alert('Login failed');
}
}
});
}
});
});

 login.php

<?php
require_once 'config.php';
session_start();
if(isset($_POST['login-btn'])){
$email = $_POST['email'];
$password = $_POST['password'];

$select = $db_con->prepare("SELECT * FROM user WHERE email='$email' and 
password='$password'");
$select->setFetchMode(PDO::FETCH_ASSOC);
$select->execute();
$data=$select->fetch();
if($data['email']!=$email and $data['password']!=$pass)
{
echo "invalid email or password";
}
elseif($data['email']==$email and $data['pass']==$pass)
{
$_SESSION['email']=$data['email'];
echo "successful"; 
}
}
?>

1 个答案:

答案 0 :(得分:2)

在尝试登录之前,您的login.php文件已经过此检查:

if(isset($_POST['login-btn'])){

但是,您的AJAX脚本不会发送名为“login-btn”的值,只会发送名为“email”的值和名为“password”的值:

data: {email:email, password:password},

如果您更改if语句以检查$_POST['email'],则应该有效,假设没有其他问题。

另请注意,您的查询容易受到SQL注入攻击。请使用绑定参数,而不是直接在查询中使用变量:

$select = $db_con->prepare("SELECT * FROM user WHERE email=:email and password=:password");
$select->setFetchMode(PDO::FETCH_ASSOC);
$select->execute(array(':email' => $email, ':password' => $password);