Inbefore:对不起的标题感到抱歉,无法弥补一个合适的标题。
现在我正在为电影和动漫开发一个基于Web的,以用户为中心的信息池,用户可以通过登录和提交表单来自己填充数据库。由于这是我的第一个项目,包括php,sql和ajax + jquery,有时可能还没有考虑最佳实践,对不起。
现在问题是:我确实有登录,注册,删除帐户和注册电影/动漫的表格。所有这些表单都是通过ajax提交的,以确保易于处理错误警报。
示例:deleteUser.js(工作正常)
$(document).ready(function() {
$("#deletion").submit(function(e) {
e.preventDefault(), $.ajax({
type: "POST",
url: "/include/userDB/deleteUser.php",
data: $(this).serialize(),
success: function(e) {
if(e == "success") {
location.href = "/html/deleted.html"
} else if(e == "nologin") {
alert("Bitte einloggen")
} else if(e == "wronglogin") {
alert("Bitte mit dem zu löschenden Account einloggen")
} else {
alert("Benutzername oder Passwort falsch")
}
}
})
})});
至于我的电影/动漫注册:ajax-success在registerMovani.js上无法正常工作:
$(document).ready(function() {
$("#registrationForm").submit(function(e) {
e.preventDefault(), $.ajax( {
url: "/include/movaniDB/registerMovani.php",
type: "POST",
enctype: "multipart/form-data",
data: new FormData(this),
processData: false,
contentType: false,
success: function(e) {
if(e == "success") {
location.href = "/html/success.html"
} else if(e == "wrongFileOrSize") {
alert("Falsche Dateiendung oder Datei zu groß")
} else if(e == "duplicate") {
alert("Eintrag bereits vorhanden")
} else if(e == "error") {
alert("Systemfehler")
} else {
alert(e)
}
}
})
})});
与我的其他ajax提交的唯一区别是他们不使用jQuery“表单数据”,因为不需要上传文件。
每当提交“registrationForm”时,相应的.php被称为正常。数据库已正确填充,但是虽然.php回应“成功”,但未调用“location.href”。相反,会弹出包含“成功”的警报,因此它会跳转到最后一个else子句,同时警告应该由先前的if子句处理的字符串。对于“wrongFileOrSize”和“duplicate”,这是相同的。
Image: Popup after submitting form
registerMovani.php中的回声拼写正确。
在没有ajax的情况下提交以下.php页面只显示回声,所以除了需要的字符串之外,不应该有别的东西来比较ajax的成功:function(e)和php的回声。
Image: .php echo after submitting form (in this example: duplicate instead of success)
老实说我在这里完全失败了(特别是因为它正在为其他所有提交工作)。为什么ajax从php获取正确的字符串,甚至通过alert(e)显示它们,但是无法将它们与之前在其他if-clause中使用的字符串进行比较并显示正确的警报/更改位置?
我尝试了什么:“===”而不是“==”;关闭php通知和错误以确保只留下作为字符串的回声;改变回声打印; json_encode而不是纯回声;删除表单enctype并使用$(this).serialize()而不是FormData,重新启动服务器并重新启动所有php,nginx服务。
最后但并非最不重要的是.phps“echo-syntax”的示例:
if (preg_match('/^Duplicate entry/', $sth->errorInfo()[2])) { //Wenn der Eintrag schon existiert und der Fehler daher rührt
echo ('duplicate');
exit();
}
else{ //Wenn ein anderer Fehler mit dem Statement aufgetreten ist
echo ('error');
exit();
}
正如你所看到的,除了相应的回声之外别无其他,通常应该通过ajax的成功呼叫来识别。
感谢您的帮助和提前的时间。
LinkeyZ。
字符串empty
中有一个e
字符。通过添加以下代码行确认:
success: function(e) {
alert(e + ' ' + e.length); <--- This line
if(e == "success") {
location.href = "/html/success.html"
在文件registerMovani.js
。
警报的输出:
duplicate 10
要修复使用此代替:
success: function(e) {
e = e.trim() <---- This
if(e == "success") {
location.href = "/html/success.html"
正如@ Mohamed-Yousef指出的那样。