1

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

Barmar
  • 741,623
  • 53
  • 500
  • 612
valdroni
  • 158
  • 3
  • 18
  • Maybe you want to use something like `isset` instead of `empty`. https://stackoverflow.com/questions/1219542/in-where-shall-i-use-isset-and-empty – Fanis Despoudis Apr 18 '23 at 21:05
  • 2
    There must be something in your framework managing the inputs, as you mentioned the "random string" must some how get translated into some other router/input object. Obviously you won't be able to just consume $_POST directly without a lot of pre-processing, I'd be looking at your "xenforo" documents on how to access form variables. – Scuzzy Apr 18 '23 at 21:17
  • What happened to the `newsletter_digest` part of the input name? – Barmar Apr 18 '23 at 21:29

1 Answers1

1

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']);
imvain2
  • 15,480
  • 1
  • 16
  • 21
  • Thanks, although it is not the most optimal implementation, the structure of the core app make it difficult to change of remove the main obstacle, i.e. that random string. Therefore your solution was just what I needed. Sorry for late reply. Cheers. – valdroni May 12 '23 at 14:03