我正在研究與php相關的作業。在作業中,需要登錄頁面。在此登錄頁面中,每個用戶都需要輸入他/她的用戶名和密碼。這些用戶名和密碼將與mysql數據庫中customer表中的值進行比較。如果密碼和用戶名同意客戶表中「cid」和「name」列下的記錄,則會顯示成功登錄的通知。使用php檢查用戶名和密碼ajax
我有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);
}
?>
loginGUI.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";
}
}
?>
我的問題是,我不能阿賈克斯後得到任何結果,它似乎不起作用。哪裏不對?
P.S:我根據建議編輯了我的php文件。謝謝你,但阿賈克斯不能再工作。幫我!!
你在哪裏,包括jQuery庫?你有其他的JS或CSS嗎? – DevlshOne
我沒有包含任何jQuery庫!我應該在哪裏包括?我正在使用Mac上的TextWrangler編寫所有的php文件。 – Begumm
把它放在loginGUI.php的'
'部分:'' – DevlshOne