对于一个项目,我们必须创建一个基本的登录系统。我试图使用 REST 来管理我的整个页面。现在我相信我在这里遗漏或误解了一些关键概念,但多年来我一直试图弄清楚如何做到这一点。不允许在这个练习中使用库,所以我不能使用 JQuery。
所以这是我的问题:我有一个 .js 文件,用于检查用户名长度和密码长度等......在这个 .js 文件中,我还有一个 ajax 函数,可以将用户名和电子邮件发送到 PHP 以验证没有已经是使用 SQL 使用该电子邮件或用户名的现有用户。
function checkUser()
{
var ajax = new Ajax(); // instantiates my external ajax class
var email = document.getElementById("newEmail").value;
var username = document.getElementById("newUserName").value
ajax.post("index.php?method=register&check=check", "newEmail=" + email + "&newUserName=" + username, callbackCatch)
}
function callbackCatch(param)
{
var responseMessages = JSON.parse(param.responseText);
emailMessage = responseMessages.emailMessage;
userNameMessage = responseMessages.userNameMessage;
userEmailIsOk = responseMessages.userEmailIsOk;
userNameIsOk = responseMessages.userNameIsOk;
release = true;
if((userEmailIsOk + userNameIsOk) != 2)
{
console.log(userEmailIsOk + userNameIsOk);
release = false;
}
updateMessageFields();
}
我得到一个 JSON 字符串,我对其进行解析,然后与我的表单值进行比较。当一切都找到并且花花公子时,我单击表单页面上的注册按钮,该按钮将所有字段发布回我的 php 文件。但是,似乎当我这样做时,每次都会以某种方式访问我的外部 ajax 类,并且我收到一个错误:NS_NOINTERFACE:组件没有请求的接口。
我假设因为每当我发帖时 AJAX 都会在后台运行,即使从我的表单中它也会尝试发回我的参数,在这种情况下是不兼容的。
我不知道如何解决这个问题。假设处理传输的我的 PHP 文件如下所示:
require_once("../database/table.php");
类测试{
private $SQLtable;
private $newEmail;
private $newPswrd;
private $newUserName;
private $newFullName;
public function test()
{
$this->SQLtable = new table();
$this->initClassVars();
$this->generateUserName();
$this->regUser();
}
function initClassVars()
{
if(isset($_POST["newPassword"]))
{
$this->newPswrd = $_POST["newPassword"];
}
if(isset($_POST['newUserName']))
{
$this->newUserName = $_POST['newUserName'];
}
if(isset($_POST["newEmail"]))
{
$this->newEmail = $_POST["newEmail"];
}
if(isset($_POST["newFullName"]))
{
$this->newFullName = $_POST["newFullName"];
}
}
function generateUserName()
{
if(isset($_POST["newFullName"]))
{
$suggestedUserName = str_replace(" ", "", $this->newFullName);
$this->newUserName = $suggestedUserName;
}
}
function regUser()
{
$this->SQLtable->registerUser($this->newEmail, $this->newPswrd, $this->newFullName, $this->newUserName);
$sessionKey = $this->genKey();
$this->SQLtable->authUser($this->newEmail, $this->newPswrd, $sessionKey);
header("location:Location:index.php?method=feed&key=$sessionKey&user=$this->newEmail");
exit;
}
function genKey($length = 16)
{
$options = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; //Range
$key = ""; //Empty holder for key
for($i = 0; $i < $length; $i++)
{
$code = rand(0, strlen($options) - 1);
$key .= $options[$code]; // Loops through as long as the $length is set and adds a random string char from $options to build a new key.
}
return $key;
}
}
正如您所看到的,我正在玩弄使用 API 密钥来控制身份验证。我还没有让 cookie 使用它,但是在我弄清楚如何对这个注册事物进行排序之后,我就这样做了。
对不起,如果我不清楚。这是我关于 stackoverflow 的第一个问题,基本上直到大约 8 个月前我才写过一行代码,所以请耐心等待我的新代码:D。谢谢..
好的,这里是 HTML 和调用函数的位置:这是我的表单 html 文件。当我反复使用它们时,文档类型和标题是分开添加的。
<body>
<h1> Thanks! <!-- $user -->, you're almost there. We just need a few more details </h1>
<form id="registerUser" name="registerUser" action="index.php?method=test" method="post">
<p>
<label for="newFullName">Full Name: </label>
<input type="text" id="newFullName" name="newFullName" value="$newFullName" />
<span id="nameMessage"></span>
</p>
<p>
<label for="newUserName">User Name: </label>
<input type = "text" id="newUserName" name="NewUserName" value="$newUserName" />
<span id="nicknameMessage"><!-- $newUserNameMessage --></span>
</p>
<p>
<label for="newEmail">Email: </label>
<input type="text" id="newEmail" name="newEmail" value="$newEmail" />
<span id = "newEmailMessage"><!-- $newEmailMessage --></span>
</p>
<p>
<label for ="newPassword">Password: </label>
<input type="password" id="newPassword" name="newPassword" value="$newPswrd" />
<span id = "newPasswordMessage"></span>
</p>
<input type="submit" id="register" disabled="disabled" value="Register" onclick=""/>
</form>
<p><a href="../../../Index.html"> Click here to go back to the login site and login. </a></p>
<script type="text/javascript" language="javascript" src="../../js/net/ajaxHandler.js"> </script>
<script type="text/javascript" language="javascript">var bootstrap = new ajaxHandler(); bootstrap.init(); </script>
</body>
</html>
整个 JS 文件是这样的。它有点乱,但这是因为我一直在玩弄它:
var ajaxHandler = function()
{
var self = this;
var release = true;
var nameField;
var userField;
var emailField;
var passWordField
var emailMessage;
var userNameMessage;
self.init = function()
{
addEventListeners();
checkFormForErrors();
}
self.redirect = function ()
{
}
function addEventListeners()
{
nameField = document.getElementById("newFullName");
userField = document.getElementById("newUserName");
emailField = document.getElementById("newEmail");
passWordField = document.getElementById("newPassword");
Event.addEventListener(nameField, 'click', checkFormForErrors);
Event.addEventListener(userField, 'click', checkUser);
Event.addEventListener(emailField, 'click', checkUser);
Event.addEventListener(passWordField, 'click', checkFormForErrors);
Event.addEventListener(nameField, 'keyup', checkFormForErrors);
Event.addEventListener(userField, 'keyup', checkUser);
Event.addEventListener(emailField, 'keyup', checkUser);
Event.addEventListener(passWordField, 'keyup', checkFormForErrors);
}
function checkPassword()
{
var passWordIsOk;
var patt = /\s/;
var whiteSpace = patt.test(passWordField);
var passwordLength = passWordField.value.length;
if (passwordLength < 8 )
{
document.getElementById("newPasswordMessage").innerHTML = "Your passowrd must be at least 8 charachters long. ";
passWordIsOk = false
if (whiteSpace)
{
document.getElementById("newPasswordMessage").innerHTML += "Your Password cannot contain any spaces.";
passWordIsOk = false
}
}
else
{
document.getElementById("newPasswordMessage").innerHTML = "Password looks good";
passWordIsOk = true
}
return passWordIsOk;
}
function checkName()
{
var userNameIsOk;
var nameMessage = document.getElementById("nameMessage");
if (!nameField.value)
{
nameMessage.innerHTML = "You need to enter a name."
userNameIsOk = false;
}
else
{
nameMessage.innerHTML = "Name looks great."
userNameIsOk = true;
}
return userNameIsOk;
}
function checkEmailSyntax()
{
var EmailIsOk = true
var patt = /^([\w!.%+\-])+@([\w\-])+(?:\.[\w\-]+)+$/ ;
var format = patt.test(emailField.value);
if (!format)
{
document.getElementById("newEmailMessage").innerHTML = "Your email does not appear to be in the right format."
EmailIsOk = false;
}
return EmailIsOk
}
function checkFullNameSyntax()
{
var nameIsOk = true
if (document.getElementById("newUserName").value == "")
{
document.getElementById("nicknameMessage").innerHTML = "You must enter a username. You can always change it later."
nameIsOk = false
}
return nameIsOk;
}
function checkUser()
{
var ajax = new Ajax();
var email = document.getElementById("newEmail").value;
var username = document.getElementById("newUserName").value
ajax.post("index.php?method=register&check=check", "newEmail=" + email + "&newUserName=" + username, callbackCatch)
}
function callbackCatch(param)
{
var responseMessages = JSON.parse(param.responseText);
emailMessage = responseMessages.emailMessage;
userNameMessage = responseMessages.userNameMessage;
userEmailIsOk = responseMessages.userEmailIsOk;
userNameIsOk = responseMessages.userNameIsOk;
release = true;
if((userEmailIsOk + userNameIsOk) != 2)
{
console.log(userEmailIsOk + userNameIsOk);
release = false;
}
updateMessageFields();
var ajax = "";
}
function updateMessageFields()
{
document.getElementById("nicknameMessage").innerHTML = userNameMessage;
document.getElementById("newEmailMessage").innerHTML = emailMessage;
checkFormForErrors();
}
function checkFormForErrors()
{
console.log('hi');
var nameOk = checkFullNameSyntax()
console.log(nameOk);
var passOk = checkPassword()
console.log(passOk);
var userOk = checkName()
console.log(userOk);
var emailOk = checkEmailSyntax()
console.log(emailOk);
console.log(release);
document.getElementById("register").disabled = true;
if(release && nameOk && passOk && userOk && emailOk)
{
console.log('hey');
console.log(release && nameOk && passOk && userOk && emailOk)
document.getElementById("register").disabled = false;
}
}
}