Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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);
share|improve this question
1  
There's a typo in this line; $reponse['empty-fields'] = true; - should be $response['empty-fields'] = true; (You missed the 's'). What are you actually trying to check with the if statement? – thebluefox 2 days ago
    
@thebluefox Oh, yes, that made the empty-fields go off, but why does it do that? When all the fields are !empty() and agreement1 is true? – PhyCoMath 2 days ago
1  
Do a little debugging and see which of those expressions is false. var_dump((!empty($fullname)) . ' && ' .(!empty($email)) . ' && ' .(!empty($birthday)) . ' && ' .(!empty($password1)) . ' && ' .(!empty($password2)) . ' && ' .($agreement1) ); – thebluefox 2 days ago
    
@thebluefox Found the problem, thank you! :) – PhyCoMath 2 days ago
up vote 1 down vote accepted

You have a couple of typo's that are causing you issues.

The first is in $birthday = $data->bithday; - you missed the 'r' from 'birthday'

Secondly, in $reponse['empty-fields'] = true; you missed the 's' from 'response'.

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

    $fullname = $data->fullname;
    $email = $data->email;
    $birthday = $data->birthday;
    $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
        $response['empty-fields'] = true;
    }
} else {
    # XSRF Error detected
    $response['xsrf-invalid'] = true;
}

# Return JSON response
echo json_encode($response);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.