我正在做与php相关的家庭作业。在分配中,需要登录页面。在此登录页面中,每个用户都应输入他/她的用户名和密码。这些用户名和密码将与mysql数据库中customer表中的值进行比较。如果密码和用户名与customer表中“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不再起作用了。帮帮我!!答案 0 :(得分:1)
您实际上并未将userid
和pswrd
发送到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');
}
}
});
答案 1 :(得分:1)
您可以使用jquery ajax
<input type='text' id='username'/>
<input type='text' id='password'/>
<input type='button' id='submit'/>
<script type='text/javascript'>
$(document).ready(function()
{
var pass = $("#password").val();
var username = $("#username").val();
$().click(function()
{
var request = $.ajax(
{
url:server.php,
type:POST,
data:{password:pass, username:username}
success:function()
{
alert("Success");
}
});
});
});
</script>
这应该对您有用,您可以使用普通$_POST['password']
和$_POST['username']
在服务器端,你可以这样做
<?php
if(isset($_POST['password']) && isset($_POST['username']))
{
$sql = "SELECT * FROM users WHERE username='".mysqli_real_escape($dbc, trim($_POST['username'])."' AND password= '".mysqli_real_escape($dbc, trim($_POST['password'])."'");
$result = mysqli_query($dbc, $sql) or die("Could Not make request");
if(mysqli_num_rows($result) == 1)
{
//redirect
}
else
{
echo "User Not Found";
}
}
?>
答案 2 :(得分:0)
$sql="SELECT * FROM customer WHERE name='" . $myusername . "' and cid='" . $mypassword . "'"; // fix this line
if ($count === 1){ // and fix this line
if (result === 1){ // and this line
在我看来,您正在使用来自不同地方的代码。您应该验证用户名/密码组合是否存在,而不是检查它是否可用。
对不起,没有人会为你做功课。这是一个很好的理由..
<?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";
}
}
?>
您在这里混合mysql
和mysqli
。你应该使用JUST mysqli
!您的userid
和pswrd
变量会在AJAX调用中分配给username
和pass
,但check_login.php
脚本永远不会看到它们。它仍然在寻找userid
和pswrd
。你有太多的错误,你似乎没有很好地回应他们修复它们。此外,我们不是家庭作业网站。
答案 3 :(得分:0)
问题可能是由于脚本标记中缺少type属性。应该是:
<script type="text/javascript" >
这应该可以解决问题。
答案 4 :(得分:0)
$("#frm-signin").validate({
errorClass: "error fail-alert",
validClass: "valid success-alert",
// Specify validation rules
rules: {
signin_email: { email :true, required: true,
regex: /^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i,
remote: {
url: "checkemail.php",
type: "POST" }
},
signin_pass: {
required: true,
remote: {
url: "checkpassword.php",
type: "post",
data: {signin_email: $("#signin_email").val()}
}
}
},
messages: {
signin_email: {
required: "Email is mandatory",
email: "Please enter valid email <h5 class='text-danger'>abc@xyz.com</h5>",
remote: "Please check your email",
regex: "Please enter valid email <h5 class='text-danger'>abc@xyz.com</h5>"
},
signin_pass: {
required: "Password is mandaitory",
remote: "Please check your password"
}
},
tooltip_options: {
signin_email: {
required: {placement:'right',html:true},
remote : {placement:'right',html:true},
email : {placement:'right',html:true},
regex : {placement:'right',html:true}
},
signin_pass: {
required: {placement:'right',html:true},
remote: {placement:'right',html:true}
}
},
submitHandler: function(form) {
form.submit();`enter code here`
}
});