简体   繁体   中英

How to check checkbox is checked or not in Conatct Form 7 WP

i'm newbie to wordpress. i've created form in wordpress page using contact form 7. after summitting form data's i'm processing the data in functions.php file. recently i added new single checkbox in my form. here's my doubt, now i've to write condition based on checked/unchecked checkbox.

here's my form code

    <div class="col-md-6">
        <label> Full Name <span class="required">*</span></label>
        [text* fullname]
    </div>

    <div class="col-md-6">
        <label> Mobile No <span class="required">*</span></label>
        [tel* mobno]
    </div> 

    <div class="col-md-6">
        <label> Car Model <span class="required">*</span></label>
        [text* carmodel]
    </div>

    <div class="col-md-6">
        [checkbox next_service_date_chk "I don't know my Next Scheduled Service Date"]
    </div>

    <div class="col-md-6">
        [submit id:submit "Submit"]
    </div>
</div>

my functions.php file code

function serviceform( $contact_form ) {

  $id = $contact_form->id;
  $submission = WPCF7_Submission::get_instance();
    if ( $submission ) {
        $posted_data = $submission->get_posted_data();
    }

  if ( '8371' == $id ) {
    $name =  $posted_data['fullname'];  
    $carmodel =  $posted_data['carmodel']; 
    $mobno =  $posted_data['mobno']; 

    $next_service_date_chk =$posted_data['next_service_date_chk'];

    if($next_service_date_chk == true){
     $message1 = urlencode("custome message one");
    }
    else{
     $message1=urlencode("custome message two");
    }

    //sms to customer
    file_get_contents('http://hpsms.dial4sms.com/api/v4/?api_key=xxxxxxxxxxxxxxxxxxxxx&method=sms&message='.$message1.'&to='.$mobno.'&type=2&sender=XXXXXX');
}
}

add_filter( 'wpcf7_support_html5_fallback', '__return_true' );

Try doing this, since your checkbox is returned as an array. What you're doing is defining it whether or not there is a value, which is something.

Also, contact form 7 object should be fetched with CF7 Docs the method instead of the value of the object

/* Don't do this, since id property is no longer accessible. */
$id = $contact_form->id; // Wrong.

/* Use id() method instead. */
$id = $contact_form->id();

Updated function

function serviceform( $contact_form ) {
  // Use function id(); to get id of object $contact_form    
  if ( '8371' !== $contact_form->id() ) return;
  $submission = WPCF7_Submission::get_instance();
    if ( $submission ) {
        $posted_data = $submission->get_posted_data();
    }
    $name =  $posted_data['fullname'];  
    $carmodel =  $posted_data['carmodel']; 
    $mobno =  $posted_data['mobno']; 

    // Checkbox is returned as array. Check if empty.
    $next_service_date_chk = !empty($posted_data['next_service_date_chk'][0]) ? true : false;

    if($next_service_date_chk == true){
        $message1 = urlencode("custome message one");
    }
    else{
        $message1=urlencode("custome message two");
    }

    //sms to customer
    file_get_contents('http://hpsms.dial4sms.com/api/v4/?api_key=xxxxxxxxxxxxxxxxxxxxx&method=sms&message='.$message1.'&to='.$mobno.'&type=2&sender=XXXXXX');
}

instead of this

$next_service_date_chk =$posted_data['next_service_date_chk'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM