Search code examples
phpif-statementxenforo

Perform PHP actions based on checkbox selections


I have a working PHP mailing script and now I am trying to modify its actions based on two checkboxes. This is for the Xenforo registration script

Here is the HTML of the two checkboxes in the HTML

<ul class="checkboxColumns">
    <li><label><input type="checkbox" name="1004124a662d8161e3e2cb602f73e81d[newsletter_digest][newsletter_receive]" value="newletter_receive"> Newsletter</label></li>
    <li><label><input type="checkbox" name="1004124a662d8161e3e2cb602f73e81d[newsletter_digest][forumdigest_receive]" value="forumdigest_receive"> Forum Digest</label></li>
</ul>

This random string as e.g. '1004124a662d8161e3e2cb602f73e81d' is generated by the core application on each session as a protection mechanism.

Some explainer about the values:

newsletter_digest=group of two checkboxes
newsletter_receive = value of first checkbox
forumdigest_receive = value of second checkbox

The PHP code I tried is this

    if ($user['user_state'] == 'email_confirm')
    {
        $this->_getUserConfirmationModel()->sendEmailConfirmation($user);
    }
    // Check if newsletter and forum digest checkboxes are checked
    $newsletterChecked = !empty($_POST['newsletter_receive']);
    $forumDigestChecked = !empty($_POST['forumdigest_receive']);
    
    
    if (!$newsletterChecked && !$forumDigestChecked) {
        // Case 1: When value="newsletter_receive" and value="forumdigest_receive" are both empty, not checked --> do Action 1
    } elseif ($newsletterChecked && $forumDigestChecked) {
        // Case 2: When value="newsletter_receive" value="forumdigest_receive" are both checked --> do Action 2
    } elseif ($newsletterChecked && !$forumDigestChecked) {
        // Case 3: When value="newsletter_receive"  is checked but value="forumdigest_receive" is left empty --> do Action 3
    } elseif (!$newsletterChecked && $forumDigestChecked) {
        // Case 4: When value="newsletter_receive" is left empty but value="forumdigest_receive" is checked --> do Action 4
    }

    return $this->_completeRegistration($user);
}

I tried it with this simple if/elseif logic and it is not working for some odd reason, as on all occassions, it never picks up the checks on checkboxes, but it always "thinks" it is the Case 1 when checkboxes are empty. I even placed Case 1 code on bottom and it still picks this action.

Even this initial check does not seem to work

$newsletterChecked = !empty($input['newsletter_receive']) ? true : false;
$forumDigestChecked = !empty($input['forumdigest_receive']) ? true : false;

I think the issue is that the php does not pick the frontend html due to that random string?

Any help is highly appreciated


Solution

  • One quick and dirty solution is to loop through $_POST and see if the value is an array and the key newsletter_digest exists. There are other solutions but this is simple and to the point.

    $newsletters = [];
    foreach($_POST as $k => $v){
        if(is_array($v) && isset($v["newsletter_digest"])){
            $newsletters = $v["newsletter_digest"];
        }
    }
    

    The result would be an array like this if both are checked:

    Array
    (
        [newsletter_receive] => newletter_receive
        [forumdigest_receive] => forumdigest_receive
    )
    

    Or like this if one is checked

    Array
    (
        [newsletter_receive] => newletter_receive
    )
    

    Then you could do something like

    $newsletterChecked = isset($newsletters['newsletter_receive']);
    $forumDigestChecked = isset($newsletters['forumdigest_receive']);