我想用Aptana IDE和xampp做一个网站,它有几个页面。索引页是所有未登录系统的用户和主页是所有登录系统的用户必须访问的位置。我是开发网站的新手。
我决定使用Ajax登录注册页面。它实际上工作正常。但现在我不知道为什么没有。
在我的登录.html页面
<form id="login-form" class="text-left" method="post">
<div class="main-login-form">
<div class="login-group">
<div class="form-group">
<label for="lg_username" class="sr-only">Username</label>
<input type="text" class="form-control" id="lg_username" name="username" placeholder="username"
data-container="body" data-toggle="popover" data-placement="left" data-content=""
value="">
</div>
<div class="form-group">
<label for="lg_password" class="sr-only">Password</label>
<input type="password" class="form-control" id="lg_password" name="password" placeholder="password"
data-container="body" data-toggle="popover" data-placement="left" data-content="">
</div>
<div class="form-group login-group-checkbox">
<input type="checkbox" id="lg_remember" name="lg_remember">
<label for="lg_remember">remember</label>
</div>
</div>
<button type="submit" class="login-button">Submit</button>
</div>
</form>
并使用 JQuery 验证进行表单验证
$(document).ready(function () {
$('#login-form').validate({
rules: { //rules goes here},
messages: { //messages goes here},
submitHandler: submitForm
});
它工作正常。我的所有规则都得到了实施。
提交功能:
function submitForm(){
var username = $('#lg_username').val();
var password = $('#lg_password').val();
var checkbox = $('#lg_remember').is(":checked");
var strAjax = "username=" + username + "&password=" + password + "&checkbox=" + checkbox;
alert(strAjax);
$.ajax({
type: 'POST',
url: 'loginAjax.php',
data: strAjax,
cache: false,
success: function(data) {
alert(data);
if(data == "1") {
window.location.href = "home.php";
}
else {
//Error messages
}
}
});
return false;
}
登录Ajax.php文件:
<?php
require_once 'dbcon.php';
session_start();
$username = mysql_real_escape_string($_POST["username"]);
$password = mysql_real_escape_string($_POST["password"]);
$sql = "SELECT * FROM tbl_user WHERE userName = '$username' AND password = '$password'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
$row = mysql_fetch_array($result);
if($count == 1){
if($_POST['checkbox'] == TRUE) {
if(!isset($_COOKIE['name'])) {
setcookie( 'name', $row['userName'], time() + (86400 * 30) );
}
}
$_SESSION['user_session'] = $row['userName'];
echo TRUE;
}
else {
echo FALSE;
}
?>
dbcon.php文件连接到MySQL数据库。我尝试控制它是输入 ajax 方法还是从带有警报的 php 文件返回的内容。strAjax 似乎是正确的,但警报(数据(像这样显示我的 php 代码;
PHP 返回此
我将登录文件更改为 php 文件而不是 html。但问题不在于此。我所有的ajax代码都从php获取类似的东西。在我的电脑中,php文件运行正常。如果您需要更多信息,我可以编辑我的帖子。为什么是这个 ajax conk?
var formData = {
'username' : $('input[name=username]').val(),
'password' : $('input[name=password]').val(),
'lg_remember' : $('input[name=lg_remember]').val()
};
// process the form
$.ajax({
type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
url : 'process.php', // the url where we want to POST
data : formData, // our data object
dataType : 'json', // what type of data do we expect back from the server
encode : true
})
试试这个
问题是你的 ajax 代码。您正在使用POST
方法并将参数作为查询字符串传递。您只需要使用serialize()
来传递数据。
只需用这个更改您的 ajax 代码:
$.ajax({
type: 'POST',
url: 'loginAjax.php',
data: $('#login-form').serialize(),
cache: false,
success: function(data) {
alert(data);
if(data == "1") {
window.location.href = "home.php";
}
else {
//Error messages
}
}
});
只需删除strAjax
变量,它不是必需的。
旁注:请不要使用
mysql_*
。它已被弃用并删除 从 PHP 7.使用mysqli_*
或PDO
尝试在ajax函数上使用data: $("#login-form").serialize()
将其添加到您的登录名Ajax.php在第二行
header('Content-Type: application/json');
注意:您的网址应为:
url : 'loginAjax.php',
我已经想通了。以前我尝试与 http://127.0.0.1:8020/联系。当我将其更改为本地主机时,所有问题都消失了。谢谢大家,我感谢你们的热情。
<html>
<head>
<title></title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#login-form').on("submit", function(){
$.ajax({
type: 'POST',
url: 'loginAjax.php',
data: $("#login-form").serialize(),
success: function(data) {
alert(data);
// if(data == "1") {
// window.location.href = "home.php";
// }
// else {
// //Error messages
// }
}
});
return false;
});
});
</script>
<form id="login-form" class="text-left" method="post">
<div class="main-login-form">
<div class="login-group">
<div class="form-group">
<label for="lg_username" class="sr-only">Username</label>
<input type="text" class="form-control" id="lg_username" name="username" placeholder="username"
data-container="body" data-toggle="popover" data-placement="left" data-content=""
value="">
</div>
<div class="form-group">
<label for="lg_password" class="sr-only">Password</label>
<input type="password" class="form-control" id="lg_password" name="password" placeholder="password"
data-container="body" data-toggle="popover" data-placement="left" data-content="">
</div>
<div class="form-group login-group-checkbox">
<input type="checkbox" id="lg_remember" name="lg_remember">
<label for="lg_remember">remember</label>
</div>
</div>
<button type="submit" class="login-button">Submit</button>
</div>
</form>
</body>
</html>
尝试整个页面,我只是在 PHP 中print_r(( 结果由于您使用的是jQuery的$((,因此我假设您已经包含jQuery库,干杯兄弟尝试在示例页面中使用代码。