JQuery.ajax Why not access the element from php json_encode?

I don’t understand, because when the data was entered into the login, it’s correct, and I do this comparison. Response.success == "success" nothing happens, I check with firebug, and the result is

Answer

[{"ncontrol":"09680040","nombre":"Edgardo","apellidop":"Ramirez","apellidom":"Leon","tUser":"Admin","status":"success"}]

jQuery.ajax script to send data

    // jQuery.ajax script to send json data to PHP 

    var action = $("#formLogin").attr('action');
    var login_data = {
        ncontrol: $("#ncontrolLogin").val(),
        password: $("#passwdLogin").val(),
        is_ajax: 1
    };  

    $.ajax({
        type: "POST",
        url: action,
        data: login_data,
        dataType: "json",
        success: function(response)
        {   
            **if(response.status == "success")** {
                $("#status_msg").html("(+) Correct Login");

            }
            else {
                $("#status_msg").html("(X) Error Login!");
            }
        }
    }); 
    return false;   

And a PHP script to handle variables from jQuery.ajax

$ncontrolForm = $_REQUEST['ncontrol'];
$passForm = $_REQUEST['password'];
$jsonResult = array();
    $query = "SELECT * FROM users WHERE ncontrol = '$ncontrolForm' AND cve_user = SHA('$passForm')";
    $result = mysqli_query($con, $query) or die (mysqli_error());
    $num_row = mysqli_num_rows($result);
    $row = mysqli_fetch_array($result);
    if( $num_row >= 1 ) {       

        $_SESSION['n_control'] = $row['ncontrol'];
        $_SESSION['t_user'] = $row['tUser'];

        $jsonResult[] = array (
            'ncontrol' => $row['ncontrol'],
            'nombre' => $row['nombre'],
            'apellidop' => $row['apellidop'],
            'apellidom' => $row['apellidom'],               
            'tUser' => $row['tUser'],
            'status' => 'success',

        );
        header('Content-Type: application/json');
        echo json_encode($jsonResult);

    }
+3
source share
1 answer

You have an array, so do this:

if(response[0].status == "success") {

An object is the first element of an array.

EDIT:

By looking more closely at your PHP, it looks like you can direct multiple lines in response to a request and add them to yours $jsonResult. I see it right?

+6
source

All Articles