2

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.

8
  • What is the output of alert(e) ; right after success: function(e) { in registerMovani.js? Commented Dec 18, 2017 at 0:04
  • 1
    Can you please try one of those problematic scenarios, and add this alert(e + ' ' + e.length); right after success: function(e) { and post what you get. Commented Dec 18, 2017 at 0:18
  • 1
    try to use .trim() e = e.trim() on the first line on ajax success function Commented Dec 18, 2017 at 0:19
  • 1
    @Ivan86 alert(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 =) Commented Dec 18, 2017 at 0:32
  • 1
    You're Welcome .. I'm sure @Ivan86 was need to be sure there's any whitespaces or not by alert(e + ' ' + e.length); and he is totally right .. and using .trim() do the trick .. Have a great day :) Commented Dec 18, 2017 at 0:35

1 Answer 1

1

There is an empty character in the string e. Confirmed by adding this line of code:

success: function(e) {
        alert(e + ' ' + e.length);           <--- This line
        if(e == "success") {
               location.href = "/html/success.html"

in file registerMovani.js.

The output of the alert:

duplicate 10

To fix use this instead:

success: function(e) {
    e = e.trim()          <---- This
    if(e == "success") {
           location.href = "/html/success.html"

as pointed out by @Mohamed-Yousef.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.