mercredi 4 janvier 2017

PHP if statement using !empty() not properly checking existing variables

I've come across a weird problem which I can't seem to debug properly. I'm currently creating a registration form using AngularJS and JSON requests to PHP files. Here is an example of input data that I used for debugging.

Registration form

Now the request sent clearly contains the following data as seen by debugging the JSON response.

Debug 1 Debug 2

Now for some reason, $response['debug-two'] or $response['debug-info-two'] does not get added to the responding JSON, even if all the fields required are !empty() or true. Also, neither is $reponse['empty-fields']. Therefor it must really boil down to the empty fields if-statement, but I can't seem to understand or figure out why, since all the fields are set as of $response['debug-info'].

# Prevent XSRF
if ($session->checkXSRF()) {
    # Get POST data
    $data = json_decode(file_get_contents("php://input"));

    $fullname = $data->fullname;
    $email = $data->email;
    $birthday = $data->bithday;
    $password1 = $data->password1;
    $password2 = $data->password2;
    $agreement1 = $data->agreement1;
    $agreement2 = $data->agreement2;

    $response['debug'] = $data;
    $response['debug-info'] = $fullname.$email.$birthday.$password1.$password2.$agreement1.$agreement2;

    # Check if empty fields
    if ( (!empty($fullname)) && (!empty($email)) && (!empty($birthday)) && (!empty($password1)) && (!empty($password2)) && ($agreement1) ) {
        $response['debug-two'] = $data;
        $response['debug-info-two'] = $fullname.$email.$birthday.$password1.$password2.$agreement1.$agreement2;
    } else {
        # All fields are required
        $reponse['empty-fields'] = true;
    }
} else {
    # XSRF Error detected
    $response['xsrf-invalid'] = true;
}

# Return JSON response
echo json_encode($response);

Aucun commentaire:

Enregistrer un commentaire