The*_*lis 4 php mysql jquery json
长期读者,第一次海报。我对 jQuery 和 JSON 的世界非常陌生,并且一直看到我正在运行的登录脚本存在问题。
最终目标是从表单中捕获数据,将该数据传递到 PHP 文件以通过 jQuery.ajax() post 进行处理,将数据与 MySQL 数据库进行比较以进行身份验证,并返回数据是否成功。
我的问题是我无法将 JSON 格式的数据从 PHP 脚本传递回 jQuery。使用 Chrome 的开发人员工具查看处理时,我看到“登录失败”。我通过将数组 $rows 扔到我的 error_log 文件中对其进行了双重检查,它返回格式正确的 JSON,但我终生无法将它返回到 jQuery 文件。任何帮助表示赞赏。
我的表单输入:
<!-- BEGIN: Login Page -->
<section data-role="page" id="login">
<header data-role="header" data-theme="b">
<a href="#landing" class="ui-btn-left">Back</a>
<h1>Please Log In</h1>
</header>
<div data-role="content" class="content" data-theme="b">
<form id="loginForm" action="services.php" method="post">
<div data-role="fieldcontain">
<label for="schoolID">School ID</label>
<input type="text" name="schoolID" id="schoolID" value="" />
<label for="userName">Username</label>
<input type="text" name="userName" id="userName" value="" />
<label for="password">Password</label>
<input type="password" name="password" id="password" value="" />
<h3 id="notification"></h3>
<button data-theme="b" id="submit" type="submit">Submit</button>
<input type="hidden" name="action" value="loginForm" id="action">
</div>
</form>
</div>
<footer data-role="footer" data-position="fixed" data-theme="b">
<h1>Footer</h1>
</div>
</section>
<!-- END: Login Page -->
Run Code Online (Sandbox Code Playgroud)
我的 jQuery 处理程序:
// Listen for the the submit button is clicked, serialize the data and send it off
$('#submit').click(function(){
var data = $("#loginForm :input").serializeArray();
var url = $("#loginForm").attr('action');
$.ajax({
type: 'POST',
url: url,
cache: false,
data: data,
dataType: 'json',
success: function(data){
$.ajax({
type: 'GET',
url: "services.php",
success: function(json){
alert(json);
$('#notification').append(json);
}
});
}
});
});
Run Code Online (Sandbox Code Playgroud)
这是我的 PHP 处理:
if (isset($_POST['action'])) {
$schoolID = $_POST['schoolID'];
$userName = $_POST['userName'];
$password = $_POST['password'];
$sql = "SELECT FirstName, LastName, FamilyID, StudentID, UserID ";
$sql .= "FROM Users ";
$sql .= "WHERE SchoolID = '$schoolID' ";
$sql .= "AND Username = '$userName' ";
$sql .= "AND Password = '$password'";
$rs = mysql_query($sql);
$rows = array();
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
$row_array['firstName'] = $row['FirstName'];
$row_array['lastName'] = $row['LastName'];
$row_array['familyID'] = $row['FamilyID'];
$row_array['studentID'] = $row['StudentID'];
$row_array['userID'] = $row['UserID'];
array_push($rows, $row_array);
}
header("Content-type: application/json", true);
echo json_encode(array('rows'=>$rows));
exit;
}else{
echo "Login Failure";
}
Run Code Online (Sandbox Code Playgroud)
看看你在这里犯了什么错误很简单
$.ajax({
type: 'POST',
url: 'services.php',
cache: false,
data: $('#loginForm').serialize(),
dataType: 'json',
success: function(data){
alert(data);
$('#notification').append(data);
}
});
});
Run Code Online (Sandbox Code Playgroud)
使用 jquery 的序列化功能。当你在成功函数中有一个参数时,它不是你在这条指令中拥有的“数据”
data : data,
Run Code Online (Sandbox Code Playgroud)
它是从 php 端返回的,它是成功返回的新数据。为避免冲突,请使用诸如 new_data 之类的其他东西
success: function(new_data){
alert(new_data);
$('#notification').append(new_data);
}
Run Code Online (Sandbox Code Playgroud)
新数据是 json 格式检查它。如果您使用的是 firefox,请使用 console.log 在 firebug 中查看。
success: function(new_data){
console.log(new_data);
alert(new_data);
$('#notification').append(new_data);
}
Run Code Online (Sandbox Code Playgroud)