【发布时间】:2013-11-21 17:18:48
【问题描述】:
我正在做一个与 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);
}
?>
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";
}
}
?>
我的问题是,在 ajax 之后我无法得到任何结果,它似乎不起作用。怎么了?
P.S : 我已经根据建议编辑了我的 php 文件。谢谢大家,但是 ajax 不能再工作了。帮帮我!!
【问题讨论】:
-
你在哪里包含 jQuery 库?你还有其他的 JS 或 CSS 吗?
-
我没有包含任何 jQuery 库!!我应该在哪里包括?我正在使用 Mac 上的 TextWrangler 编写所有 php 文件。
-
把它放在 loginGUI.php 的
<head>部分:<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> -
Okey 我添加了,但不影响结果。我已经根据建议编辑了我的 php 文件。请帮我处理这个编辑过的版本。
-
您需要提供有关您收到的错误的一些详细信息。