[英]How to check checkbox is checked or not in Conatct Form 7 WP
i'm newbie to wordpress.我是 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.
我已经使用联系表格 7 在 wordpress 页面中创建了表格。在完成表格数据之后,我正在处理 functions.php 文件中的数据。 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我的函数。php 文件代码
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此外,联系表格 7 object 应使用CF7 Docs方法而不是 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
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'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.