lundi 20 avril 2015

Checkbox null means it's not, checkbox checked is 1

I want to have it so if a checkbox is NULL, or 0, it is not a fav like it would be if the value was a one.

Also, is there a way to limit the boxes checked to 10?

current code:

if(isset($_POST['submit'])){//to run PHP script on submit
    if(!empty($_POST'favorites')){
    // Loop to store and display values of individual checked checkbox.
        foreach($_POST['favorites[]'] as $selected){
            $query= "UPDATE u_collection SET fav = 0 WHERE username ='".$username."'"; 
            mysqli_query($db,$query);
        }
    } 
}

foreach($gems as $i){
    echo '<tr> 
    <th>'.$i->name.'</th> 
    <th><form action="collection.php" method="post">
    <input type="checkbox"'; 
    if($i -> fav){
        echo 'checked = "checked"';         
    }
    echo 'name="favorites[]" value='.$i->name.'></th></tr></form>'; 
}
?>
<th>
<input type='submit' name='submit' value='Submit'/>
</form>




Aucun commentaire:

Enregistrer un commentaire