WordPress.org

Ready to get started?Download WordPress

Forums

Contact Form 7
How to check checkbox-status in "before_send"-Function? (7 posts)

  1. migstein
    Member
    Posted 6 months ago #

    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/

  2. Takayuki Miyoshi
    Member
    Plugin Author

    Posted 6 months ago #

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

  3. migstein
    Member
    Posted 6 months ago #

    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

  4. Takayuki Miyoshi
    Member
    Plugin Author

    Posted 6 months ago #

    OK. Then you can use

    if ($WPCF7_ContactForm->posted_data['checkbox01'] == 'Text') {
  5. migstein
    Member
    Posted 6 months ago #

    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

  6. Takayuki Miyoshi
    Member
    Plugin Author

    Posted 6 months ago #

    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.

  7. Dylan85
    Member
    Posted 5 months ago #

    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.

Reply

You must log in to post.

About this Plugin

About this Topic