How to store multiple checkbox values โโin wordpress
This is my code for checkboxes.
<p class="full_form"><label for="prop_category"><?php _e('We are into','wpestate');?></label>
<input type="checkbox" id="architecture" size="80" name="service_name[]" value="architecture" >Architecture
<input type="checkbox" id="builder_developer" size="80" name="service_name[]" value="builder_developer">Builders and Developeres
<input type="checkbox" id="material_supplier" size="80" name="service_name[]" value="material_supplier">Material Supplier
<input type="checkbox" id="contractor" size="80" name="service_name[]" value="contractor">Contractor
<input type="checkbox" id="int_decorator" size="80" name="service_name[]" value="int_decorator">Interior Decorator
<input type="checkbox" id="prop_finance" size="80" name="service_name[]" value="prop_finance">Property Finance
<input type="checkbox" id="prop_legal_ad" size="80" name="service_name[]" value="prop_legal_ad">Property Legal Advisor
<input type="checkbox" id="prop_valuer" size="80" name="service_name[]" value="prop_valuer">Property Valuer
<input type="checkbox" id="vastu_consult" size="80" name="service_name[]" value="vastu_consult">Vaastu Consultant
How to store multiple checkboxes in Wordpress postmeta table? Should I save them as an array? How can I access them after submitting the form? And how to display the selected checkbox value while editing?
+3
source to share
1 answer
try this way html
<p class="full_form"><label for="prop_category">We are into</label>
<input type="checkbox" id="architecture" size="80" name="service_name[]" value="Architecture" >Architecture
<input type="checkbox" id="builder_developer" size="80" name="service_name[]" value="Builders and Developeres">Builders and Developeres
<input type="checkbox" id="material_supplier" size="80" name="service_name[]" value="Material Supplier">Material Supplier
<input type="checkbox" id="contractor" size="80" name="service_name[]" value="Contractor">Contractor
<input type="checkbox" id="int_decorator" size="80" name="service_name[]" value="Interior Decorator">Interior Decorator
<input type="checkbox" id="prop_finance" size="80" name="service_name[]" value="Property Finance">Property Finance
<input type="checkbox" id="prop_legal_ad" size="80" name="service_name[]" value="Property Legal Advisor">Property Legal Advisor
<input type="checkbox" id="prop_valuer" size="80" name="service_name[]" value="Property Valuer">Property Valuer
<input type="checkbox" id="vastu_consult" size="80" name="service_name[]" value="Vaastu Consultant">Vaastu Consultant
Send code to save
<?php
//submit code
if(isset($_POST['service_name']) ){
$data=serialize($_POST['service_name']);
update_post_meta($postid, 'service_name', $data);
}
?>
Change code
<?php
//edit code
$data=get_post_meta($postid, 'service_name');
//$data=unserialize($data[0]);
$data=$data[0];
print_r($data);
?>
<input type="checkbox" id="architecture" size="80" name="service_name[]" value="Architecture" <?php if(in_array('Architecture',$data) ){echo "checked";} ?> >Architecture
//other checkbox put as this
in_array check it
+4
source to share