Inbefore: Sorry for the bad title, couldn't make up a fitting one.
Right now I am developing a web based, user centered information pool for movies and anime where users are able to fill the database themselves by logging in and submitting forms. Since it's my first project including php, sql and ajax+jquery, best practices might have not been considered sometimes, sorry for that.
Now to the problem: I do have forms for login, registration, account deletion and registrating a movie/anime. All these forms are submitted through ajax to ensure easy to handle error-alerts.
AJAX/jQuery
Example: deleteUser.js (which is working fine)
$(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")
}
}
})
})});
As for my movie/anime registration though: the ajax-success does not work properly on 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)
}
}
})
})});
The only difference to my other ajax-submits is that they do not use jQuerys "FormData" since there is no need to upload a file.
Whenever the "registrationForm" is submitted the respective .php is called just fine. The database is filled correctly, but the "location.href" is not called although the .php echoes "success". Instead, an alert including "success" pops up, therefore it's jumping right to the last else-clause while alerting a string which should be handled by prior if-clauses. This is the same for "wrongFileOrSize" and "duplicate".
Image: Popup after submitting form
The echoes in registerMovani.php are spelled correctly.
Submitting without ajax the following .php page only shows the echo so there shouldn't be anything else but the needed string to compare ajax' success: function(e) to php's echoes.
Image: .php echo after submitting form (in this example: duplicate instead of success)
I'm honestly at a total loss here (especially since it's working for every other submit). Why is ajax getting the correct strings from php and even showing them through alert(e) but is not able to compare them to the strings used prior in the other if-clauses and show the correct alert/change the location?
What I tried: "===" instead of "=="; turning off php notices and errors to ensure there is only the echo left as string; change echo to print; json_encode instead of pure echo; delete the forms enctype and use $(this).serialize() instead of FormData, reboot the server and restart all php, nginx services.
PHP
Last but not least an example of what the .phps "echo-syntax" looks like:
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();
}
As you can see there is nothing else but the respective echo which usually should be recognizable by ajax' success-call.
Thanks for your help and time in advance.
LinkeyZ.
alert(e) ;right aftersuccess: function(e) {inregisterMovani.js?alert(e + ' ' + e.length);right aftersuccess: function(e) {and post what you get.e = e.trim()on the first line on ajax success functionalert(e + ' ' + e.length);showed duplicate 10 so there really was some space since duplicate only counts 9 elements. Thanks a lot! @Mohamed-Yousef Thanks a lot! This worked for me. And I learned a new piece of useful code. Thanks for the fast and spot on help, both of you =)alert(e + ' ' + e.length);and he is totally right .. and using.trim()do the trick .. Have a great day :)