我正在使用jQuery ajax和php進行註冊系統。在我的劇本開始時,我正在進行兩項檢查。我正在檢查用戶名是否太長,然後我檢查用戶名是否已被佔用。 當我檢查用戶名是否已被使用時,php腳本會返回一個值,但如果用戶名太長,則不會返回任何內容。jQuery ajax php什麼都不返回
代碼(PHP):
$usernameR = $_REQUEST['username'];
if($usernameR > 8)
{
echo "toolong";
}
else
{
$passwordR = $_REQUEST['password'];
require_once("dbConn.php");
$usernameRSF = mysqli_real_escape_string($connect, $usernameR);
$usernameRSF = htmlspecialchars($usernameRSF);
$nQuery = mysqli_query($connect, "SELECT * FROM users WHERE username LIKE '%".$usernameRSF."%'") or die("Query error: ".mysqli_error($connect));
$result = mysqli_num_rows($nQuery);
if($result > 0)
{
echo "taken";
}
}
代碼(JS):
function register()
{
$(document).ready(function()
{
var username = $("#usernameIn").val();
var password = $("#usernameIn").val();
if(username != "" && password != "")
{
$("#reError").text(" ");
$("#reError").fadeOut("slow");
$.ajax({
url: "../system/register.php",
type: "POST",
dataType: "text",
data: { username: username, password: password },
success: function(ajaxResult)
{
alert(ajaxResult);
if(ajaxResult == "taken")
{
$("#reError").text("Username already exists");
$("#reError").fadeIn("slow");
}
if(ajaxResult == "toolong")
{
$("#reError").text("Username too long");
$("#reError").fadeIn("slow");
}
}
});
}
else
{
$("#reError").text("You can't leave username or password empty!");
$("#reError").fadeIn("slow");
}
});
}
哎呀我一直在使用JavaScript這麼長... – Jojo01