0

I am working on User authentication using Jquery mobile & php by referring a tutorial. But the tutorial back end is mongoDB. I want my application connected to PHP & mysql.

I have made some change to the tutorial code, i am confused with php return on error or success.

AJAX code

 $.ajax({
            type: 'POST',
            url: BookIt.Settings.signUpUrl,
            data:"submitted=" + "1" + "&mobile=" + mobileNumber + "&firstName=" + firstName + "&lastName=" + lastName + "&password=" + password,
            dataType: "json",
            success: function (resp) {
            alert(resp);
                console.log("success");
                if (resp.success === true) {
                    $.mobile.navigate("signup-succeeded.html");
                    return;
                }
                if (resp.extras.msg) {
                    switch (resp.extras.msg) {
                        case BookIt.ApiMessages.DB_ERROR:
                        case BookIt.ApiMessages.COULD_NOT_CREATE_USER:
                            // TODO: Use a friendlier error message below.
                            $ctnErr.html("<p>Oops! BookIt had a problem and could not 2 register you.  Please try again in a few minutes.</p>");
                            $ctnErr.addClass("bi-ctn-err").slideDown();
                            break;
                        case BookIt.ApiMessages.MOBILENUMBER_ALREADY_EXISTS:
                            $ctnErr.html("<p>The mobile number that you provided is already registered.</p>");
                            $ctnErr.addClass("bi-ctn-err").slideDown();
                            $txtMobileNumber.addClass(invalidInputStyle);
                            break;
                    }
                }

            },
            error: function (e) {
                console.log(e.message);
                // TODO: Use a friendlier error message below.
                $ctnErr.html("<p>Oops! BookIt had a problem and could not register you.  Please try again in a few minutes.</p>");
                $ctnErr.addClass("bi-ctn-err").slideDown();
            }
        });

MY PHP return JSON encode output

echo json_encode("success");

API Message

var BookIt = BookIt || {};
BookIt.ApiMessages = BookIt.ApiMessages || {};
BookIt.ApiMessages.EMAIL_NOT_FOUND = 0;
BookIt.ApiMessages.INVALID_PWD = 1;
BookIt.ApiMessages.DB_ERROR = 2;
BookIt.ApiMessages.NOT_FOUND = 3;
BookIt.ApiMessages.EMAIL_ALREADY_EXISTS = 4;
BookIt.ApiMessages.COULD_NOT_CREATE_USER = 5;
BookIt.ApiMessages.PASSWORD_RESET_EXPIRED = 6;
BookIt.ApiMessages.PASSWORD_RESET_HASH_MISMATCH = 7;
BookIt.ApiMessages.PASSWORD_RESET_EMAIL_MISMATCH = 8;
BookIt.ApiMessages.COULD_NOT_RESET_PASSWORD = 9;
BookIt.ApiMessages.PASSWORD_CONFIRM_MISMATCH = 10;
BookIt.ApiMessages.MOBILENUMBER_ALREADY_EXISTS=11;
BookIt.ApiMessages.USERNAME_ALREADY_EXISTS=12;

how i can return error/success to this ajax, so it work fine

Tutorial am referring

Raj
  • 31
  • 1
  • 8

1 Answers1

1

You're checking

if (resp.success === true)

which means that the json data must be formatted like this:

{"success":true}

which is produced by:

echo json_encode( ['success'=>true] ); // or array() instead of [] for PHP < 5.4

Similarly, for errors (if (resp.extras.msg)),

echo json_encode( [ 'extras' => ['msg' => $errorcode] ] );

produces this JSON (with $errorcode = 2 (BookIt.ApiMessages.DB_ERROR))

{"extras":{"msg":2}}
Kenney
  • 9,003
  • 15
  • 21
  • Hello @Kenney this always return to the error function – Raj Nov 09 '15 at 14:04
  • Try `console.log(e)` in your `error: function(e)`, see what comes back. Maybe the php scipt has a syntax error? – Kenney Nov 09 '15 at 14:14
  • the php log file is empty. i tried alert (console.log(e)); its say's undifined – Raj Nov 09 '15 at 16:40
  • You either `alert` or `console.log` (press F12 and click the Console tab to see it's output) - not both. What happens if you access the signup URL directly in your browser? It's hard to say what's wrong without seeing it's code. – Kenney Nov 09 '15 at 16:50
  • XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' – Raj Nov 09 '15 at 17:00
  • For that, [see this SO question](http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource). It's a security feature of the browser - basically, you're making a request to another domain and that's not allowed unless some HTTP headers are set. Add this to the top of your php script: ``. – Kenney Nov 09 '15 at 17:41
  • Okay, i will try it and let you know. – Raj Nov 09 '15 at 18:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94638/discussion-between-raj-and-kenney). – Raj Nov 09 '15 at 19:31