Etat d'une checkbox

Fermé
Melooo Messages postés 1405 Date d'inscription vendredi 28 novembre 2008 Statut Membre Dernière intervention 18 mars 2013 - 18 mars 2011 à 14:28
Melooo Messages postés 1405 Date d'inscription vendredi 28 novembre 2008 Statut Membre Dernière intervention 18 mars 2013 - 28 mars 2011 à 15:10
Bonjour,
J'ai un tableau html rempli avec des données php, lorsque on clique sur l'entête "Type" apparait des checkbox du genre : Type 1 , Type 2, Type 3, l'utilisateur choisi donc les types à afficher, ensuite il peut également trier les types choisi, mon code fonctionne très bien, sauf que quand je décoche par exemple :
Type 1 et que juste après je fais trier, ma checkbox type1 redevient coché alors que je viens de la décocher, voici le code de l'entête type :
<div style="margin-left:24px;">
		<div  id="test" style="position:absolute;z-index:555;margin:-24px;width:123px;height:130px;background-color:#454545;display:none;">
		<table border="0" width="123px" height="50px" style="padding-top:5px;margin-left:5px;color:white;font-family:Arial;font-size:11px;">
		<tr>
			<td>
				<strong><a style="color:white;text-decoration:none;" href="javascript:displayMessage('test');">Filtrer :</a></strong>
				<br /><form name="menu_deroulant" action="" method="post">
				<input class="checkbox" type="checkbox" rel="Exploitation" checked  value="exploitation" name="check_exploit"><div width="6px" style="display:inline;">&nbsp;</div><img src="images/ico/exploitation.png"><div width="6px" style="display:inline;">&nbsp;</div>Exploitation
				<br /><input type="checkbox" class="checkbox" rel="Infrastructure" checked  id="check2" value="infrastructure"  name="check[]"><div width="6px" style="display:inline;">&nbsp;</div><img src="images/ico/infrastructure.png"><div width="6px" style="display:inline;">&nbsp;</div>Infrastructure
				<br /><input type="checkbox" class="checkbox" rel="Batiment" checked id="check3" value="batiment" name="check[]"><div width="6px" style="display:inline;">&nbsp;</div><img src="images/ico/batiment.png"><div width="6px" style="display:inline;">&nbsp;</div>Bâtiment
				<br /><br />
				<div style="margin-left:5px" class="trier" id="trier_type"><img src="images/fleche.png"><strong>Trier</strong>	</div>
				<div style="margin-left:5px" class="masquer"><img src="images/ico/masquer.png"><strong>Masquer</strong></div>
				</form>
				<div id="msg"></div>
			</td>
		</tr>
		</table>
		</div>

Et voici le code lorsque l'utilisateur clique sur trier (juste un extrait) :
<td bgcolor="white" width="280px" id="entete_tableau"><span style="margin-left:10px">Libellé <img src="images/fleche.png"></span></td>
		<td bgcolor="white" valign="middle" align="center" class="processus" width="40px"  id="entete_tableau"><a style="color:white;text-decoration:none;" href="javascript:displayMessage('test');"><img src="images/carre.png" border="0" width="6" height="5px">&nbsp;&nbsp;<img border="0" src="images/fleche.png">
		<?php include ('trie.php'); ?>

		<td bgcolor="white" valign="middle" align="center" id="priorite" class="priorite" width="70px"><a style="color:white;text-decoration:none;" href="javascript:displayMessage('test5');">Priorité <img border="0" src="images/fleche.png"></a>
<td align="center" width="40px" bgcolor='.$bg.' class="processus" id="contenu_tab">'.$image.'</td>
		<td align="center" width="70px" bgcolor='.$bg.' class="priorite" id="contenu_tab">'.$resultat[2].'</td>
L'appel de cette page ce fait en ajax :

$(document).ready(function() {

	$("#trier_type").click(function () {
	var where = " ORDER BY tableau_type ASC";
	var tab = ($("#tableau_tab").html());
		$.ajax({
			type: "POST",
			url: "trier_ligne.php",
			data: "where+"&tabl="+tab,
			success: function(x){
			$('#resultat4').html(x);
			}
		});	
	});
});
Je récupère tout bien, sauf l'état des checkbox
merci de votre aide

1 réponse

Eastchild Messages postés 318 Date d'inscription lundi 23 juillet 2007 Statut Membre Dernière intervention 28 mars 2012 31
18 mars 2011 à 14:35
Bonjour,

Retire les checked de tes input. Si tu les laisses, ça indique qu'il faut que les checkbox soient cochés par défaut.

0
Melooo Messages postés 1405 Date d'inscription vendredi 28 novembre 2008 Statut Membre Dernière intervention 18 mars 2013 84
Modifié par Melooo le 18/03/2011 à 14:36
Oui mais elles doivent être cochés par défaut, lors de la première visite de la page, car par défaut tous les types sont affichés..
0
Eastchild Messages postés 318 Date d'inscription lundi 23 juillet 2007 Statut Membre Dernière intervention 28 mars 2012 31
18 mars 2011 à 14:41
Dans ce cas, utilise des variables PHP dans lesquelles tu vas mettre checked si c'est la première visite et selon le choix de l'utilisateur checked ou rien lors d'une autre visite.

Je te le fais en pseudo code :
si première_visite alors
   $checked1 = 'checked';
sinon 
   si le visiteur a selectionné l'option alors
      $checked1 = 'checked';
   sinon 
      $checked1 = '';
   finsi
finsi


et après pour l'input tu notes :
<input class="checkbox" type="checkbox" rel="Exploitation" <?php echo $checked1; ?>  value="exploitation" name="check_exploit">
0
Melooo Messages postés 1405 Date d'inscription vendredi 28 novembre 2008 Statut Membre Dernière intervention 18 mars 2013 84
Modifié par Melooo le 18/03/2011 à 14:52
Oui, mais sauf que mon code ne s'exécutera pas car le bouton trier et mes checkbox sont dans le même formulaire. voici le code qui ne fonctionne pas et que j'ai fait selon ton aide :
<?php 
$i = 0; 
if($i == 0) 
{ 
$i++; 
$checked = 'checked'; 
} 
if($_POST['check_exploit']) 
{ 
$checked = 'checked'; 
} 
else 
{ 
$checked = ''; 
} 

?> 
<div style="margin-left:24px;"> 
  <div  id="test" style="position:absolute;z-index:555;margin:-24px;width:123px;height:130px;background-color:#454545;display:none;"> 
  <table border="0" width="123px" height="50px" style="padding-top:5px;margin-left:5px;color:white;font-family:Arial;font-size:11px;"> 
  <tr> 
   <td> 
    <strong><a style="color:white;text-decoration:none;" href="javascript:displayMessage('test');">Filtrer :</a></strong> 
    <br /><form name="menu_deroulant" action="" method="post"> 
    <input class="checkbox" type="checkbox" rel="Exploitation" <?php echo $checked; ?>  value="exploitation" name="check_exploit"><div width="6px" style="display:inline;"> </div><img src="images/ico/exploitation.png"><div width="6px" style="display:inline;"> </div>Exploitation 
    <br /><input type="checkbox" class="checkbox" rel="Infrastructure" checked  id="check2" value="infrastructure"  name="check[]"><div width="6px" style="display:inline;"> </div><img src="images/ico/infrastructure.png"><div width="6px" style="display:inline;"> </div>Infrastructure 
    <br /><input type="checkbox" class="checkbox" rel="Batiment" checked id="check3" value="batiment" name="check[]"><div width="6px" style="display:inline;"> </div><img src="images/ico/batiment.png"><div width="6px" style="display:inline;"> </div>Bâtiment 
    <br /><br /> 
    <input type="button" class="trier" name="bt" id="trier_type" value="essai"> 
    <div style="margin-left:5px" class="trier" id="trier_type"><img src="images/fleche.png"><strong>Trier</strong> </div> 
    <div style="margin-left:5px" class="masquer"><img src="images/ico/masquer.png"><strong>Masquer</strong></div> 
    </form> 
    <div id="msg"></div> 
   </td> 
  </tr> 
  </table> 
  </div> 
0
Eastchild Messages postés 318 Date d'inscription lundi 23 juillet 2007 Statut Membre Dernière intervention 28 mars 2012 31
25 mars 2011 à 17:14
Bonjour et désolé d'avoir mis du temps à répondre.

Je voudrais pouvoir posté un bout de code pour corriger celui que tu as posté cependant je ne sais pas pourquoi, le forum m'en empêche :'(

Il faut que tu créé un variable $checked pour chaque input avec un contrôle sur chaque $_POST.

Dans ton cas tu as 3 input, utilisé $checked1, $checked2 et $checked3. De plus pas la peine d'utilisé un indice au tout début, cela ne fonctionnera pas même si c'est sur plusieurs pages. Voici une petite aide pour ton code

<?php

$checked1 = '';
$checked2 = '';
$checked3 = '';

if(!isset($_POST) || empty($_POST)){
$checked1 = 'checked';
$checked2 = 'checked';
$checked3 = 'checked';
}else{
if($_POST['check_exploit']){
$checked1 = 'checked';
}
if($_POST['check_infrastructure']){
$checked2 = 'checked';
}
if($_POST['check_batiment']){
$checked3 = 'checked';
}


Et sur la partie HTML, je ne l'avais pas vu la première fois mais tu as 2 input portant le même nom : check[]. Appelle celui sur l'infrastructure : check_infrastructure et celui sur les batiments : check_batiment

<input class="checkbox" type="checkbox" rel="Exploitation" <?php echo $checked1; ?>  value="exploitation" name="check_exploit"><div width="6px" style="display:inline;"> </div><img src="images/ico/exploitation.png"><div width="6px" style="display:inline;"> </div>Exploitation 
    
<input type="checkbox" class="checkbox" rel="Infrastructure" <?php echo $checked2; ?> value="infrastructure"  name="check_infrastructure"><div width="6px" style="display:inline;"> </div><img src="images/ico/infrastructure.png"><div width="6px" style="display:inline;"> </div>Infrastructure 

<input type="checkbox" class="checkbox" rel="Batiment" <?php echo $checked3; ?>  value="batiment" name="check_batiment"><div width="6px" style="display:inline;"> </div><img src="images/ico/batiment.png"><div width="6px" style="display:inline;"> </div>Bâtiment 


Logiquement, cela devrait beaucoup mieux fonctionner.
0
Melooo Messages postés 1405 Date d'inscription vendredi 28 novembre 2008 Statut Membre Dernière intervention 18 mars 2013 84
25 mars 2011 à 17:29
Merci tout de même de ta réponse ;)
je test ca dès lundi ! en espérant que tu sois la pour m'aider ;)
Bon week end et merci de tes réponses
0