4

My code keeps returning success no matter if the code fails or not. I tried using different methods but I am stumped.

Here is the PHP

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Content-Type: application/json; charset=UTF-8');

if (isset($_POST['username']) && isset($_POST['password']))
{
    if (file_exists("../inc/dbc.php")){
        if(!include('../inc/dbc.php')){
            print(json_encode(array('message' => 'ERROR', 'code' => 404)));
            die();
        } 
    }
    else{
        print(json_encode(array('message' => 'ERROR', 'code' => 404)));
    }
    // store post variables in temp variables
    $temp_user =                    mysql_real_escape_string(stripslashes($_POST['username']));
    $temp_pass =                    mysql_real_escape_string(stripslashes($_POST['password']));

    //Select users that have matching passwords and are currenlty employed
    $query =                        "SELECT afid, logged, active, code FROM employee as emp WHERE (emp.acct = '$temp_user') AND (emp.code = '$temp_pass') AND (emp.active = 'Y') AND ((emp.logged = 'k') OR (emp.logged='K'))"; 

    //Run the query then set the number of results as a query
    $result =                       mysql_query($query); 
    if(!$result){
        $error = mysql_error();
        print(json_encode(array('message' => 'error', 'code' => 500, 'statusText' => $error)));
        die();
    }

Here is the JavaScript

function authorize(dataString,usr,pass){

                $.ajax({
                    crossDomain: true,
                    type: 'POST',
                    url: "domain/app/login.php",
                    dataType: 'json',
                    async: 'true',
                    data: dataString,
                    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                    success: function(dataResponse,status,message){
                        if(status == 'success'){
                            localStorage.setItem("pLog",dataResponse[['message']]);
                            localStorage.setItem('username',usr);
                            localStorage.setItem('password',pass);
                            $("#customer-page").off();
                                $.mobile.loading("show");
                                loadCustomers(localStorage.getItem('ezp-username'),localStorage.getItem('pLog'));
                        }
                    },
                    error: function(dataResponse,status,message){
                        gl.error_msg = "Could not log in; check user name and password.";
                        $(".error-msg").html(gl.error_msg).css('display', 'block');
                        ezReset("error_msg");
                        $.mobile.loading("hide");
                    },
                    timeout: 5000
                });
            }

I can't set the header past content type so I tried to just print error messages when something fails which sometimes works but mostly does not. I don't know why.

  • Are you trying to access an error message that you've defined in your PHP array and printed back to your ajax call as JSON? – showdev Aug 14 '15 at 20:34

1 Answers1

5

If you want jQuery to recognize the response as an error, you'll need to set a HTTP error code in PHP (500 for example). That can be done by setting a header like so:

header('HTTP/1.1 500 Internal Server Error');

Since your PHP is returning a 200 HTTP status code, jQuery will always see that as success. That too is an acceptable way to deal with errors, but you'll have to check your 'dataResponse' for the presence of the error field you set and move your error handling code to the success block.

dperjar
  • 343
  • 2
  • 11
  • For further reference, see [possible values of textStatus](http://stackoverflow.com/questions/11103995/what-are-all-of-the-possible-values-of-the-textstatus-parameter-in-the-callback). – showdev Aug 14 '15 at 20:18
  • When I tried that I get an error saying that I can't because the "Headers already sent". – user3314802 Aug 14 '15 at 20:19
  • 1
    @user3314802 "Headers already sent" means that you're sending something to the browser already. Check and make sure you're not echoing or printing before you're attempting that. – snollygolly Aug 14 '15 at 20:23
  • @ snollygolly That was it, I forgot a die() after a print so the script kept running. Now I need to know how to +1 a sub comment. – user3314802 Aug 14 '15 at 20:52