• Hello,

    I’ve build a form with several checkboxes with “Contact Form 7”.

    Each checkbox stands for an event with a price which the user can select (e.g. “Sightseeing-Tour in Berlin – 10 Euro”).

    After the user presses the “submit”-button a function should add the prices for the choosen events and write the sum in the email, which will be send automatically from Contact Form 7 to the user.

    My problem: how can I check in the function whether a chechbox is checked or not? I have tried the following code which does not work:

    add_action('wpcf7_before_send_mail', 'wpcf7_calculate_sum');
    
    function wpcf7_calculate_sum ($WPCF7_ContactForm) {
        $total_sum=0;
    
        if ($WPCF7_ContactForm->posted_data['checkbox01'] == 1) {
        	$total_sum = $total_sum + 1;
        }
    
        if ($WPCF7_ContactForm->posted_data['checkbox02'] == 1) {
        	$total_sum = $total_sum + 2;
        }
    
        $WPCF7_ContactForm->mail['body'] .= 'Total sum = '.$total_sum.' Euro</p>';
    }

    These comparisons do not work, the result ist always FALSE, so the total sum will always be zero.

    Any idea?

    Thank you for your help in advance!

    Michael

    https://wordpress.org/plugins/contact-form-7/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    So, what checkboxes do you actually have in the form?

    Thread Starter migstein

    (@migstein)

    Hello Takayushi Miyoshi,

    sorry, but I don’t understand your question (and BTW: thanks for your good work – generally it works very well!).

    Are there different sorts of checkboxes? I used simple statements like “[checkbox checkbox01 “Text”]” in my form.

    You can see the form at http://www.klm-kdg-we-2014.de – please choose there the drop-down-menu “Anmeldung” and then the menuitem “Anmeldeformular” to see the original form.

    Michael

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    OK. Then you can use

    if ($WPCF7_ContactForm->posted_data['checkbox01'] == 'Text') {
    Thread Starter migstein

    (@migstein)

    Hello Takayushi Miyoshi,

    thanks for your fast answer, but your suggestion does not work.

    I assume that ‘text’ in your suggestion means the text which is displayed on screen behind the checkbox?

    If I understood you correctly than there must be another problem.

    Have you got any other idea?

    Michael

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    Maybe you don’t need to verify the value. You just need to check existence of it. So you can use this:

    if ( ! empty( $WPCF7_ContactForm->posted_data['checkbox01'] ) ) {

    If this won’t work, probably there is another problem.

    Dylan85

    (@dylan85)

    I am experiencing the same problem.

    When I use this statement within the wpcf7_before_send_mail hook:

    if ( $WPCF7_ContactForm->posted_data[‘Checkbox12’] == ‘Yes’ ) {
    $WPCF7_ContactForm->mail[‘recipient’] = “<email1@gmail.com>”;
    }else{
    $WPCF7_ContactForm->mail[‘recipient’] = “<email2@gmail.com>”;
    }

    The message is sent to email2 @gmail.com, even if the check box is selected as “Yes”.

    Could the problem be that, the values of checkboxes and radio buttons are stored within an array in [‘values’] ? When assigning a variable to $WPCF7_ContactForm->posted_data[‘Checkbox12’] it returns as an array, I am not a php expert, but is there some way we can get the specific values assigned and stored within that array?

    Thanks in advance.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to check checkbox-status in "before_send"-Function?’ is closed to new replies.