我正在使用 ajax 和 php 构建登录。我的代码运行良好,直到我在我的 php 代码中调用另一个类。当我的 php 代码死掉时,我得到了 int 甚至放了一个 var_dump。你可以在我的 php 代码中看到我已经将它注释掉了,因为我真的想做
看法:
<body>
<p> </p>
<div id="content">
<h1>Login Form</h1>
<form id="form1" name="form1" action="stack.php" method="post">
<p>
<label for="username">Username: </label>
<input type="text" name="username" id="username" />
</p>
<p>
<label for="password">Password: </label>
<input type="password" name="password" id="password" />
</p>
<p>
<input type="button" id="login" name="login" value="submit"/>
</p>
</form>
<div id="message"></div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#login").click(function(){
var action = $("#form1").attr('action');
var form_data = {
username: $("#username").val(),
password: $("#password").val(),
is_ajax: 1
};
$.ajax({
type: 'POST',
url: action,
data: form_data,
success: function(data){
if(typeof(data) != 'undefined' && (data == 'success' || data == 'error')){
if(data == 'success'){
$("#form1").slideUp('slow', function() {
$("#message").html("<p class='success'>You have logged in
successfully!</p>");
});
} else if(data == 'error'){
$("#form1").slideUp('slow', function() {
$("#message").html("<p class='error'>Invalid username and/or
password.</p>");
});
}
} else {
console.log("här");
console.log(data);
$("#message").html("<p class='error'>Error to connect to server</p>");
}
}
});
return false;
});
});
</script>
</body>
</html>
PHP:
class DologinHandler{
public function Login(){
if(isset($_REQUEST['is_ajax']))
{
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
// $UserHandler = new UserHandler();
//$UserHandler -> controllDB($username,$password);
if($username == 'demo' && $password == 'demo')
{
exit('success');
} else {
exit('error');
}
}
}
}
?>