我最近尝试将ajax实现到我网站的登录功能中。 它在普通PHP中工作良好,但当我发出ajax请求时,响应是一个HTML代码块,这是我一生中从未见过的。 下面是我的代码:
$(document).ready(function(){
function Message(color, text){
$("#errorMSG span").text(text);
$("#errorMSG").slideDown();
$("#errorMSG").css("background-color",color);
}
function DissmissMessage(){
$("#errorMSG").slideUp();
}
$("#loginSubmit").on('click', function(){
var email = $("#loginEmail").val();
var password = $("#loginPassword").val();
if(email == ""){
Message('red', 'Please enter your email');
}else if(password == ""){
Message('red', 'Please enter your password');
}else{
DissmissMessage();
$.ajax({
url: './includes/functions.php',
method: 'POST',
data:{
signup : 1,
email : email,
password : password
},
success: function(data){
if(data){
Message('green', 'Login Success.');
console.log(data);
}else{
Message('red', 'Wrong password or email.');
console.log(data);
}
},
});
}
});
});
PHP部分:
if(isset($_POST['login'])){
if(!$_POST['email']){
$loginError = "Please enter your email.";
}else{
$email = ValidatedForm($_POST['email']);
if(!$_POST['password']){
$loginError ="Please enter your password.";
}else{
$password = ValidatedForm($_POST['password']);
}
}
$query = "SELECT email, password, fullname, permission FROM users WHERE email = '$email'";
$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
$hashedPassword = $row['password'];
$email = $row['email'];
$name = $row['fullname'];
$permission = $row['permission'];
}
if(password_verify($password, $hashedPassword)){
//session_start();
//setcookie('email', $email, time() + (86400 * 30), "/"); // 86400 = 1 day
$_SESSION['email']=$email;
$_SESSION['name']=$name;
$_SESSION['permission']=$permission;
echo 1;
}
}else{
$loginError ="Wrong email or password.";
echo 0;
}
}
这是我的HTML表单:
<h1>Log in</h1>
<form action = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method = 'post' id="regform">
<span>Email:</span><br>
<input type="text" name="email" id="loginEmail"><br>
<span>Password:</span><br>
<input type="password" name="password" id="loginPassword"><br>
<span style="position:relative; background-color: red; display: inline-block; border-radius: 100px;"> <?php echo $loginError ?></span>
<br><br>
<input type="button" name="login" value="Log in" id="loginSubmit">
</form>
而且,我得到的响应是这个HTML块:
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: fullname in E:\Programs\wamp64\www\CountDown\includes\functions.php on line <i>32</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0008</td><td bgcolor='#eeeeec' align='right'>263816</td><td bgcolor='#eeeeec'>{main}( )</td><td title='E:\Programs\wamp64\www\CountDown\includes\functions.php' bgcolor='#eeeeec'>...\functions.php<b>:</b>0</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: conn in E:\Programs\wamp64\www\CountDown\includes\functions.php on line <i>40</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0008</td><td bgcolor='#eeeeec' align='right'>263816</td><td bgcolor='#eeeeec'>{main}( )</td><td title='E:\Programs\wamp64\www\CountDown\includes\functions.php' bgcolor='#eeeeec'>...\functions.php<b>:</b>0</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysqli_query() expects parameter 1 to be mysqli, null given in E:\Programs\wamp64\www\CountDown\includes\functions.php on line <i>40</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0008</td><td bgcolor='#eeeeec' align='right'>263816</td><td bgcolor='#eeeeec'>{main}( )</td><td title='E:\Programs\wamp64\www\CountDown\includes\functions.php' bgcolor='#eeeeec'>...\functions.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0387</td><td bgcolor='#eeeeec' align='right'>266272</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysqli-query' target='_new'>mysqli_query</a>
( )</td><td title='E:\Programs\wamp64\www\CountDown\includes\functions.php' bgcolor='#eeeeec'>...\functions.php<b>:</b>40</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: SELECT email FROM users WHERE email ='' in E:\Programs\wamp64\www\CountDown\includes\functions.php on line <i>40</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0008</td><td bgcolor='#eeeeec' align='right'>263816</td><td bgcolor='#eeeeec'>{main}( )</td><td title='E:\Programs\wamp64\www\CountDown\includes\functions.php' bgcolor='#eeeeec'>...\functions.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0668</td><td bgcolor='#eeeeec' align='right'>266504</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.trigger-error' target='_new'>trigger_error</a>
( )</td><td title='E:\Programs\wamp64\www\CountDown\includes\functions.php' bgcolor='#eeeeec'>...\functions.php<b>:</b>40</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in E:\Programs\wamp64\www\CountDown\includes\functions.php on line <i>42</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0008</td><td bgcolor='#eeeeec' align='right'>263816</td><td bgcolor='#eeeeec'>{main}( )</td><td title='E:\Programs\wamp64\www\CountDown\includes\functions.php' bgcolor='#eeeeec'>...\functions.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0944</td><td bgcolor='#eeeeec' align='right'>266536</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysqli-num-rows' target='_new'>mysqli_num_rows</a>
( )</td><td title='E:\Programs\wamp64\www\CountDown\includes\functions.php' bgcolor='#eeeeec'>...\functions.php<b>:</b>42</td></tr>
</table></font>
正如“响应是我一生中从未见过的HTML代码块”指出的那样,您在php文件中遗漏了一些重要的代码。 您没有定义访问数据库的$conn
,也没有定义在查询中使用的$email
(由于缺少$conn
(连接字符串),查询没有执行)。