user meta - how to save multiple checkbox in usermeta and get it?

admin2025-06-02  9

I want to create a new part as multiple checkbox in user profile page that user can check their services.

But the issue is i do not know how to display the checked options after saving as checked and how to display checked options in another page by get_user_meta!!!

my codes:

<input type="checkbox" name="service_name[]" value="Architecture" >Architecture<br>
<input type="checkbox" name="service_name[]" value="Builders and Developeres">Builders and Developeres<br>
<input type="checkbox" name="service_name[]" value="Material Supplier">Material Supplier<br>
<input type="checkbox" name="service_name[]" value="Contractor">Contractor<br>
<input type="checkbox" name="service_name[]" value="Interior Decorator">Interior Decorator<br>
<input type="checkbox" name="service_name[]" value="Property Finance">Property Finance<br>

the way i save them in user meta table:

if( isset($_POST['service_name']) ){
    //$data=serialize($_POST['service_name']);
    $data = $_POST['service_name'];
    update_user_meta($userid, 'service_name', $data);
}  

i need your help

I want to create a new part as multiple checkbox in user profile page that user can check their services.

But the issue is i do not know how to display the checked options after saving as checked and how to display checked options in another page by get_user_meta!!!

my codes:

<input type="checkbox" name="service_name[]" value="Architecture" >Architecture<br>
<input type="checkbox" name="service_name[]" value="Builders and Developeres">Builders and Developeres<br>
<input type="checkbox" name="service_name[]" value="Material Supplier">Material Supplier<br>
<input type="checkbox" name="service_name[]" value="Contractor">Contractor<br>
<input type="checkbox" name="service_name[]" value="Interior Decorator">Interior Decorator<br>
<input type="checkbox" name="service_name[]" value="Property Finance">Property Finance<br>

the way i save them in user meta table:

if( isset($_POST['service_name']) ){
    //$data=serialize($_POST['service_name']);
    $data = $_POST['service_name'];
    update_user_meta($userid, 'service_name', $data);
}  

i need your help

Share Improve this question asked Mar 12, 2019 at 12:41 Sh.DehnaviSh.Dehnavi 1093 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

First get the data

$data = get_user_meta( $user_id, 'service_name', true);

You find an array of all entries in $data. You can do it this way:

<input type="checkbox" name="service_name[]" value="Architecture" <?php echo in_array('Architecture', $data) ? 'checked' : ''); ?>>Architecture<br>

Untested, but should work.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748816066a313957.html

最新回复(0)