Cocher automatiquement une 'checkbox' si...

Fermé
DjDodo - 28 déc. 2008 à 18:01
dadou9101 Messages postés 107 Date d'inscription mardi 22 avril 2008 Statut Membre Dernière intervention 12 août 2010 - 29 déc. 2008 à 16:49
Bonjour,

Je voudrias savoir si exsiste un script qui nous permettrais de cocher immédiatement une case a cocher dans un formulaire de modification... Si la valeur existe dans la base mysql, on coche la case automatiquement dans le formulaire...

Si ce code fonctionne veuillez me le poster s.v.p.

8 réponses

Smoking bird Messages postés 870 Date d'inscription mardi 11 mars 2008 Statut Membre Dernière intervention 10 juillet 2011 58
28 déc. 2008 à 18:31
$sql="ta requête";
$query=envoi de ta requête;
while($done=récupération de ta requête dans un tableau) {
    echo "<input type='checkbox' name='' ";
    echo (valeur de ta requête==la valeur à comparer) ? "checked" : "";
    echo "/>";
}
0
Je ne comprends pas vraiment ton code

Car j'ai un tableau de 119 checkboxs qui sont différentes les unes les autres quel bout de code est-ce qu'il faut insérer

Code source complet de ma page web

<html>
<head>
<?php $id = $_GET["nom"] ; ?>
<title>Modification de l'horaire de <?php echo $id ;?></title>
</head>
<body>
<?php
//connection au serveur:
$cnx = mysql_connect( "localhost", "gavergol", "(****)" ) ;

//sélection de la base de données:
$db = mysql_select_db( "gavergol_garderie" ) ;

//requête SQL:
$sql = "SELECT * FROM personnes WHERE '.$id.'" ;

//exécution de la requête:
$requete = mysql_query( $sql, $cnx ) ;

//affichage des données:
while( $result = mysql_fetch_object( $requete ) )
{
?>
<form name="insertion" action="modification3.php" method="POST">
<input type="hidden" name="id" value="<?php echo($id) ;?>">
<table border="1" align="center" cellspacing="2" cellpadding="2">
<tr style="background-color:#808080; color:black;" align="center";>
<td colspan="8">  Horaire de <?php echo $id ;?></td>
</tr>
<tr style="background-color:black; color:white;" align="center";>
<td style="background-color:white;"> </td>
<td>Lundi</td>
<td>Mardi</td>
<td>Mercredi</td>
<td>Jeudi</td>
<td>Vendredi</td>
<td>Samedi</td>
<td>Dimanche</td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">7 heure</td>
<td><input type="checkbox" <?php if ( $id ['1h7']=='x') echo 'checked="checked" '; ?> name="1h7" style="width:50px" value="x"></td>
<td><input type="checkbox" <?php if ( $id ['2h7']=='x') echo 'checked="checked" '; ?> name="2h7" style="width:50px" value="x"></td>
<td><input type="checkbox" <?php if ( $id ['3h7']=='x') echo 'checked="checked" '; ?> name="3h7" style="width:50px" value="x"></td>
<td><input type="checkbox" <?php if ( $id ['4h7']=='x') echo 'checked="checked" '; ?> name="4h7" style="width:50px" value="x"></td>
<td><input type="checkbox" <?php if ( $id ['5h7']=='x') echo 'checked="checked" '; ?> name="5h7" style="width:50px" value="x"></td>
<td><input type="checkbox" <?php if ( $id ['6h7']=='x') echo 'checked="checked" '; ?> name="6h7" style="width:50px" value="x"></td>
<td><input type="checkbox" <?php if ( $id ['7h7']=='x') echo 'checked="checked" '; ?> name="7h7" style="width:50px" value="x"></td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">8 heure</td>
<td><input type="checkbox" name="1h8" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h8" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h8" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h8" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h8" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h8" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h8" style="width:50px" value="x"></td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">9 heure</td>
<td><input type="checkbox" name="1h9" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h9" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h9" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h9" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h9" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h9" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h9" style="width:50px" value="x"></td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">10 heure</td>
<td><input type="checkbox" name="1h10" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h10" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h10" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h10" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h10" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h10" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h10" style="width:50px" value="x"></td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">11 heure</td>
<td><input type="checkbox" name="1h11" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h11" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h11" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h11" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h11" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h11" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h11" style="width:50px" value="x"></td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">12 heure</td>
<td><input type="checkbox" name="1h12" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h12" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h12" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h12" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h12" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h12" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h12" style="width:50px" value="x"></td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">13 heure</td>
<td><input type="checkbox" name="1h13" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h13" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h13" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h13" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h13" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h13" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h13" style="width:50px" value="x"></td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">14 heure</td>
<td><input type="checkbox" name="1h14" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h14" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h14" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h14" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h14" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h14" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h14" style="width:50px" value="x"></td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">15 heure</td>
<td><input type="checkbox" name="1h15" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h15" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h15" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h15" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h15" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h15" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h15" style="width:50px" value="x"></td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">16 heure</td>
<td><input type="checkbox" name="1h16" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h16" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h16" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h16" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h16" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h16" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h16" style="width:50px" value="x"></td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">17 heure</td>
<td><input type="checkbox" name="1h17" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h17" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h17" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h17" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h17" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h17" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h17" style="width:50px" value="x"></td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">18 heure</td>
<td><input type="checkbox" name="1h18" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h18" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h18" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h18" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h18" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h18" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h18" style="width:50px" value="x"></td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">19 heure</td>
<td><input type="checkbox" name="1h19" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h19" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h19" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h19" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h19" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h19" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h19" style="width:50px" value="x"></td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">20 heure</td>
<td><input type="checkbox" name="1h20" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h20" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h20" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h20" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h20" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h20" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h20" style="width:50px" value="x"></td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">21 heure</td>
<td><input type="checkbox" name="1h21" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h21" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h21" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h21" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h21" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h21" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h21" style="width:50px" value="x"></td>

</tr>
<tr align="center";>
<td style="background-color:#808080;">22 heure</td>
<td><input type="checkbox" name="1h22" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h22" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h22" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h22" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h22" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h22" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h22" style="width:50px" value="x"></td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">23 heure</td>
<td><input type="checkbox" name="1h23" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h23" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h23" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h23" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h23" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h23" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h23" style="width:50px" value="x"></td>
</tr>
<tr align="center";>
<td style="background-color:#808080;">00 heure</td>
<td><input type="checkbox" name="1h00" style="width:50px;" value="x"></td>
<td><input type="checkbox" name="2h00" style="width:50px" value="x"></td>
<td><input type="checkbox" name="3h00" style="width:50px" value="x"></td>
<td><input type="checkbox" name="4h00" style="width:50px" value="x"></td>
<td><input type="checkbox" name="5h00" style="width:50px" value="x"></td>
<td><input type="checkbox" name="6h00" style="width:50px" value="x"></td>
<td><input type="checkbox" name="7h00" style="width:50px" value="x"></td>
</tr>

<tr align="center">
<td colspan="8"><input type="submit" style="background-color:black; color: white" value="Soumettre"></td>
</tr>
</table>
</form>
</body>
</html>
0
xmath Messages postés 102 Date d'inscription vendredi 1 février 2008 Statut Membre Dernière intervention 4 décembre 2010 1
28 déc. 2008 à 20:43
UP!
0
Bikarfioul Messages postés 40 Date d'inscription jeudi 15 juillet 2004 Statut Membre Dernière intervention 28 juin 2010 5
28 déc. 2008 à 21:18
Houla ! Tu es sur d'avoir bien pensé ton code ?

Généralement on produit un tableau PHP en le générant ligne par ligne voir cellule par cellule ça evite les répétitions et les codes volumineux

Bref ! La solution ne serait pas ça plutôt :

<?php if ( $result['7h7']=='x') echo 'checked="checked" '; ?>


Enfin je te donne un conseil : Une base de donnée où tu ne saisie qu'une valeur Bool (c'est ton cas : coché/pas coché)est plus simple à maintenir avec des valeurs de types tinyint() (1 ou 0) mais evite les varchar() qui sont consommateur de mémoire (c'est encore ton cas avec "x").
0
xmath Messages postés 102 Date d'inscription vendredi 1 février 2008 Statut Membre Dernière intervention 4 décembre 2010 1
28 déc. 2008 à 21:22
Je ne voulais pas me casser la tête et en même temps je ne connais pas les boucles donc... Pour moi si ça marche je vais pas me casser la tête encore plus

Merci pour ta réponse j'essaierais ton code sous peu !
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
xmath Messages postés 102 Date d'inscription vendredi 1 février 2008 Statut Membre Dernière intervention 4 décembre 2010 1
29 déc. 2008 à 02:21
Message annulé laisser faire ce message...
0
xmath Messages postés 102 Date d'inscription vendredi 1 février 2008 Statut Membre Dernière intervention 4 décembre 2010 1
29 déc. 2008 à 02:38
Voici l'erreur maintenant:
Fatal error: Cannot use object of type stdClass as array in modification2.php on line 45
0
xmath Messages postés 102 Date d'inscription vendredi 1 février 2008 Statut Membre Dernière intervention 4 décembre 2010 1
29 déc. 2008 à 03:27
UP
0
Bikarfioul Messages postés 40 Date d'inscription jeudi 15 juillet 2004 Statut Membre Dernière intervention 28 juin 2010 5
29 déc. 2008 à 10:05
Modification2.php c'est le code que tu as affiché au-dessus ?

Si ce n'est pas le cas merci d'afficher les 10 lignes qui précède et suivent la ligne 45 ici inculpé.
0
dadou9101 Messages postés 107 Date d'inscription mardi 22 avril 2008 Statut Membre Dernière intervention 12 août 2010 5
29 déc. 2008 à 16:49
hello
jai vue ton que tu a répondu un post sur un forum au sujet de checkbox qui se coche automatiquement
j aimerais savoir si tu peu m'aider a se sujet la ?

je suis un peu perdu au sujet des checbox et j aimerais en savoir un peu plus
voila mon problème

je cree une checklist pour des référence de magasin dans chaque magasin une checklist apparait ainsi que des checkbox ces checkbox serve simplement a me retrouver sur ma liste de tache a faire par magasin
je procède donc a un enregistrement des checkbox dans ma base de donnée mais le souci c'est quand je retourne pour checker le magasin la checkbox n'est pas cocher

pourrait tu m aider???

je te montre un bout de mon code

<?php

include('connexion.php');
//création de la requête SQL
$sql = "SELECT * FROM `operations` WHERE `id_ope`=1";
//exécution de la requête SQL
$requete = @mysql_query( $sql, $link ) or die( $sql . "<br>" . mysql_error() );
echo "<table width=100% border=2>";
echo "<tr>";
echo "<td width=10%><center>N°</center></td>";
echo "<td width=80%><center>Description</center></td>";
echo "<td width=10%><center></center></td>";
echo "</tr>";

echo "<form action=\"test.php?id_mag=$id_mag\" method=\"post\" enctype=\"multipart/form-data\" name=\"Mon_form\">";


while ($tab = mysql_fetch_array($requete))
{
echo "<tr>";
echo "<td width=10%><center>".$tab['id_ope']."</center></td>";
echo "<td width=80%><a href=\"operations/operation (1).php?nom=".$tab['description_ope']."\" target=\"_blank\">". $tab['description_ope']."</a></td>";
echo "<td width=10%><center><input type=\"checkbox\" name=\"1\" value=\"1\" /></center></td>";
echo "</tr>";
}
?>


comment dois je faire pour quil vérifie que la checkbox est bien cocher

merci de ton aide


cordialement

david
0