我正在尝试使用 ajax 和 php 构建登录系统。我使用日志视图,然后将数据保存在 ajax 中,这会带入我的 doLogin.php(php 文件)。我的问题是当我将 php 文件构建到一个类和一个函数中时,它永远不应该是任何 ajax 数据
看法:
public function DoLoginBox() {
//inloggning form-tagg...
return '<p> </p>
<div id="content">
<h1>Login Form</h1>
<form id="form1" name="form1" action="Handler/doLogin.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="submit" id="login" name="login" />
</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(response)
{
if(response == 'success')
$("#form1").slideUp('slow', function() {
$("#message").html("<p class='success'>You have logged in successfully!</p>");
});
else
$("#message").html("<p class='error'>Invalid username and/or password.</p>");
}
});
return false;
});
});
</script
PHP:
<?php
require_once ("UserHandler.php");
class DoLogingHandler{
public function Login (){
$is_ajax = !empty($_REQUEST['is_ajax']);
if(isset($is_ajax) && $is_ajax)
{
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$UserHandler = new UserHandler();
$UserHandler -> controllDB($username,$password);
if($username == 'demo' && $password == 'demo')
{
echo "success";
}
}
}
} ` $DoLogingHandler = new DoLogingHandler(); $DoLogingHandler->登录();?>
如果我在 stack.php 中添加类和函数,我会得到“连接到服务器时出错”(我希望将此代码放入一个类和函数中以调用其他函数:
<?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');
}
}
}
} ?>