mercredi 20 avril 2016

Checkbox with PHP form

I'm working on a form on my website that would display different categories, checked or not regarding if the category contain the product or not. One product can have multiple categories.

<form method='post' action='action.php'> 
            <?php foreach ($ls_categories as $c): ?> 
                <input 
                    type='checkbox' 
                    name='checkboxvar[]' 
                    value='<?= $c->id ?>' 
                    <!--  -->
                    <?= (contains($c, $categories)) ? 'checked' : "" ?>> 

                    <?= $c->title ?><br>
                <?php endforeach; ?> 
            <input type='submit'> 
        </form>

When I look at the source code I got this :

<input
       type='checkbox' 
       name='checkboxvar[]' 
       value='1' 
       <!--  -->
       checked> 

       Title1<br>
<input 
       type='checkbox' 
       name='checkboxvar[]' 
       value='2' 
       <!--  -->
       > 

       Title2<br>

But on the screen I see this :

 checked> Cat1
 > Cat2
 > Cat3
 checked> Cat4
 > Cat5

(with the boxes on the left, all empty)

Probably something wrong in my foreach loop but I don't see it.

Displaying categories is the first part. After that I want to edit the product's categories with what will be checked when submitting the form. Any idea how to do that?

English is not my first langage so maybe you'll need some more precisions. Feel free to ask if you don't understand well. Thanks!

Aucun commentaire:

Enregistrer un commentaire