php - 使用 php ajax 检查用户名和密码

标签 php mysql ajax

我正在做一个与php相关的作业。在作业中,需要一个登录页面。在此登录页面中,每个用户都需要输入他/她的用户名和密码。这些用户名和密码将与 mysql 数据库中的客户表中的值进行比较。如果密码和用户名与客户表中'cid'和'name'列下的记录一致,则会显示登录成功的提示。

我有3个php文件,分别是loginGUI.php、check_login.php和db_connect.php。

db_connect.php:

  <?php
     $db_name="ozcan_b"; // Database name 
     $tbl_name="customer"; // Table name 
     //connect to db
     $con=mysql_connect("127.0.0.1","xxxxxx","xxxxxx");
     mysql_select_db("ozcan_b")or die("cannot select DB");
    // Check connection
    if (mysqli_connect_errno($con))
    {
    echo "Failed to connect to MySQL: " . mysql_connect_error();
    exit(0);
    }
 ?>

登录GUI.php:

<?php
   session_start();
   require_once ('db_connect.php'); // include the database connection 

?>

<html>
<head>
<title>
Login page
</title>
</head>
<body>
<h1 style="font-family:Comic Sans Ms;text-align="center";font-size:20pt;
color:#00FF00;>
Login Page
</h1>
<form name="myForm" method="POST" >    
Username<input type="text" name="userid"/>
Password<input type="password" name="pswrd"/>
<input type="button" value="Login" id='checklogin' onclick="check()"/>  
<div id='username_availability_result'></div>
</form>

<script language="JavaScript">

var xhr_request = false;
var checking_html = 'Checking...';  

    //when button is clicked  
    function check(){ 

        //Check the fields
        if(document.forms['myForm'].userid.value=="" || document.forms['myForm'].pswrd.value=="")
        {
            alert('Please enter your password and your username!');
        }

        else
        {

            //else show the cheking_text and run the function to check  
             //$('#username_availability_result').html(checking_html);  
            check_availability();  
        }   
    }

    //function to check username availability  
 function check_availability(){  
    //get the username  
    var userid = document.forms['myForm'].userid.value;  
    var pswrd = document.forms['myForm'].pswrd.value;


    //use ajax to run the check 

    var request = $.ajax(
    {
      url:check.php,
      type:POST,
      data:{pswrd:pass, userid:username}
      success:function()
      {

        alert("Success");
      }
    });



   </script>






</body>
</html>

check_login.php:

<?php
    session_start();
    require_once ('db_connect.php');



    if(isset($_POST['pswrd']) && isset($_POST['userid']))
    {
        $sql = "SELECT * FROM users WHERE username='".mysqli_real_escape($dbc,        trim($_POST['userid'])."' AND password= '".mysqli_real_escape($dbc,    trim($_POST['pswrd'])."'");

        $result = mysql_query($dbc, $sql) or die("Could Not make request");
        if(mysql_num_rows($result) == 1)
        {
            //redirect to the welcome page

         }
         else
         {
             echo "User Not Found";

     }


    }

 ?> 

我的问题是,在 ajax 之后我无法得到任何结果,它似乎不起作用。怎么了?

P.S:我已经根据建议编辑了我的 php 文件。谢谢大家,但是ajax又不能用了。帮助我!!

最佳答案

您实际上并未将 useridpswrd 发送到您的 PHP 脚本。您不能只发送 $.ajax 随机属性并希望它起作用。您的数据需要放入 data 参数中。

$.ajax({
    type:"POST",
    url: "check_login.php",
    cache: false,
    data:{
        userid:userid,
        pswrd:pswrd
    },
    success: function(result){
        //if the result is 1  
        if(result == 1){  
            //show that the username is available  
            $('#username_availability_result').html(username + ' is Available');  
        }else{  
            //show that the username is NOT available  
            $('#username_availability_result').html(username + ' is not Available');  
        }  
    }
});

关于php - 使用 php ajax 检查用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20127682/

相关文章:

javascript - 如何使用 Cucumber 测试用 javascript 加载的内容

javascript - Polymer iron-ajax 间隔调用

php - MySQL 数据库中的 IP 地址?

php - MySQL 多对多具有唯一键和更新/选择如果存在否则插入

php - 向数据库添加信息时出现问题

php - 比较 2 列并删除不匹配的

php - 将数组写入文件的最佳方法?

jquery - 使用 jQuery.ajax 和 JSONP 设置 header ?

php - 如何将 j/M/y 日期格式更改为时间戳?

php - imagemagick convert.exe 错误