Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中使用ajax进行简单登录_Php_Ajax - Fatal编程技术网

在php中使用ajax进行简单登录

在php中使用ajax进行简单登录,php,ajax,Php,Ajax,我试图在PHP中使用AJAX登录,但它不起作用。这是我的代码,请在需要的地方更正 这是输入凭据的起始位置 <?php session_start();?> <html> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <script> $(document).re

我试图在PHP中使用AJAX登录,但它不起作用。这是我的代码,请在需要的地方更正

这是输入凭据的起始位置

<?php session_start();?>
<html>
<head>
  <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
  <script>
    $(document).ready(function(){
        //alert(23424)
        $("button").click(function(){
            alert('hey');
            var email = $("#email").val();
            var pass = $("#pass").val();
            if(email===""|| pass===""){
                alert("All fields are mandatory!!!");
                return false;   
            }
            alert(email);
            $.ajax({
                method: "POST",
                url: "loginDB.php",
                //data: data
                data: { email: email, pass: pass}
            });
        });
    });
  </script>
</head>
<body>
    <form method="post">
        <center><h1>ADMIN LOGIN</h1></center><hr><br>
        <label>Email</label><input type="text" name="email" id= "email" required><br><br>
        <label>Password</label><input type="password" name="pass" id="pass" required><br><br><br>
        <button type="button" id="button">Login</button>
    </form>
</body>
</html>

$(文档).ready(函数(){
//警报(23424)
$(“按钮”)。单击(函数(){
警惕(‘嘿’);
var email=$(“#email”).val();
var pass=$(“#pass”).val();
如果(电子邮件==“”| |通过==“”){
警报(“所有字段均为必填!!!”;
返回false;
}
警报(电子邮件);
$.ajax({
方法:“张贴”,
url:“loginDB.php”,
//数据:数据
数据:{email:email,pass:pass}
});
});
});
管理员登录

电子邮件

密码


登录
这是我的验证码

<?php
session_start();
    $servername = "localhost";
    $username = "root";
    $password = "123456";
    $dbname = "ajax";

    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

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

    $sql = "SELECT name FROM register WHERE email='".$email."' and password='".$pass."'";
    //echo $sql; exit;
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            $_SESSION["name"]= $row["name"];
           // echo $_SESSION["name"]; exit;
            header("Location:welcome.php");
            }
    }
    else {
        include 'admin_ajax.php';
        ?>
        <br><br>
        <?php
        echo "Incorrect email or and password";
    }
exit();
?>




不明白问题出在哪里。请帮助。

您的javascript需要

$.ajax({
 method: "POST",
 url: "loginDB.php",
 dataType: "json",
 data: { email: email, pass: pass},
success:function(data)
{
if(data.type=='success')
    window.location = 'welcome.php';
else
   alert("Incorrect email or and password");
}
});
在php中

<?php
session_start();
    $servername = "localhost";
    $username = "root";
    $password = "123456";
    $dbname = "ajax";

    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

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

    $sql = "SELECT name FROM register WHERE email='".$email."' and password='".$pass."'";
    //echo $sql; exit;
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) 
    {
        while($row = mysqli_fetch_assoc($result)) 
        {
            $_SESSION = $row;
            echo json_encode(array("type" => 'success'));
            exit;
        }
    }
    else 
    {
        include 'admin_ajax.php';        
        echo json_encode(array("type"=>'error'));
        exit;
    }
?>

查看浏览器控制台您的代码易受SQL注入攻击,您需要修复此问题。您正在以明文形式存储密码,还需要修复此问题。您在浏览器控制台中是否遇到任何错误。PHP代码是否已启动?检查Apache日志尝试添加ajax库
https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
您仅在加载
http://code.jquery.com/jquery-latest.min.js
。非常感谢,它可以工作:),但当输入错误的详细信息时,它仍然不会显示错误消息。不管怎么说,这有帮助:)