vendredi 22 septembre 2017

Save and display values of multiple checkboxes in WordPress

I am trying to save the value of multiple checkboxes in WordPress, So if a guest checks a checkbox (front-end), this checked value will be stored in the database.

Next time anyone loads this page, this particular checkbox should be pre-checked.

So far I have got this code in the template file that is responsible for the content of the page I am trying to add these checkboxes to.

(This is no regular WP post/page template but a "collection page" generated by a WooCommerce collection plugin):

<?php 
    //submit code 
      if(isset($_POST['cadeau'])  ){
          $data=serialize($_POST['cadeau']);

          update_post_meta($coll_id, 'cadeau', $data);
      }


    //edit code

        $data=get_post_meta($coll_id, 'cadeau');
        //$data=unserialize($data[0]);
        $data=$data[0];
        print_r($data);

?>

<input type="checkbox" name="cadeau[]" value="<?php echo $product_id;?>" <?php if(in_array($product_id,$data)  ){echo "checked";} ?> >

This is not working however.. Am I on the right track? Or am I doing it completely wrong?




Aucun commentaire:

Enregistrer un commentaire