IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bibliothèques et frameworks PHP Discussion :

update si checkbox sélectionnée


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Inscrit en
    Juillet 2010
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Juillet 2010
    Messages : 10
    Points : 7
    Points
    7
    Par défaut update si checkbox sélectionnée
    Bonjour à tous,

    je souhaite faire un update multiple pour les champs où la chekbox est sélectionnée mais ça ne marche pas...
    Je suis sous codeigniter, voici les lignes de code concernées :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
     
    function update_matchs() {
    	if($this->input->post('Update_matchs')=='oui') {
    		$data = array();
    		$match_id    = $this->input->post('match_id');
    		$match_goal1 = $this->input->post('match_goal1');
    		$match_goal2 = $this->input->post('match_goal2');
    		$forfait     = $this->input->post('forfait');
     
    		foreach( $match_id as $rec_key => $rec_val) {
    			if($match_goal1[$rec_key] == '' ) { $match_goal1[$rec_key] = '-1'; } 
    			if($match_goal2[$rec_key] == '' ) { $match_goal2[$rec_key] = '-1'; } 
    			$data[] = array(
    					'match_goal1' => $match_goal1[$rec_key],
    					'match_goal2' => $match_goal2[$rec_key],
    					'forfait' 	  => $forfait[$rec_key],
    					'match_id'    => $match_id[$rec_key],
    			);
    		}
     
    		echo '<pre>';
    		print_r($data);
    		echo '</pre>';
    		exit;
     
    		$this->db->update_batch('foot_matches', $data, 'match_id'); 
    		redirect('admin/Update_matchs/ModifOK');
    	}
    }
    j'ai volontairement décommenté le print_r($data) pour voir ce qu'il se passe et tout fonctionne sauf la valeur de la checkbox.
    Voici le retour du print_r :
    Array
    (
        [0] => Array
            (
                [match_goal1] => 3
                [match_goal2] => 6
                [forfait] => 
                [match_id] => 2587
            )
    
        [1] => Array
            (
                [match_goal1] => 8
                [match_goal2] => 2
                [forfait] => 
                [match_id] => 2503
            )
    
        [2] => Array
            (
                [match_goal1] => 0
                [match_goal2] => 5
                [forfait] => 
                [match_id] => 2506
            )
    
        [3] => Array
            (
                [match_goal1] => -1
                [match_goal2] => -1
                [forfait] => 
                [match_id] => 2504
            )
    ... etc (la valeur des goals à -1 signifiant que le match n'est pas encore joué)

    Dans le formulaire, avant de l'envoyer, j'avais sélectionné la checkbox du match_id 2506 signifiant que c'est 5-0 forfait et non le résultat du match.
    Comme vous voyez, il n'y a jamais de valeur à 'forfait' (que la checkbox soit sélectionnée ou non ...

    Merci de votre aide.

  2. #2
    Membre à l'essai
    Homme Profil pro
    Développeur Web
    Inscrit en
    Août 2013
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Août 2013
    Messages : 7
    Points : 20
    Points
    20
    Par défaut
    Bonjour,

    Pourrais-tu rajouter un var_dump pour voir les données que tu reçois.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    var_dump($this->input->post());

  3. #3
    Membre éprouvé
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mai 2009
    Messages
    736
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2009
    Messages : 736
    Points : 1 101
    Points
    1 101
    Par défaut
    Et affiche l'html du formulaire

  4. #4
    Futur Membre du Club
    Homme Profil pro
    Inscrit en
    Juillet 2010
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Juillet 2010
    Messages : 10
    Points : 7
    Points
    7
    Par défaut
    le
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    var_dump($this->input->post('forfait'));
    me donne : array(1) { [2506]=> string(1) "1" }
    ce qui correspond pourtant bien à l'id du match où j'ai coché la case !

    le formulaire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
     
    <?php 
    echo form_open('admin/update_matchs/', array('class'=>'cssform', 'name'=>'UpdateMatchs') ); 
     
    if($Matchs_a_remplir != null):
    foreach($Matchs_a_remplir as $r):
    ?>
    	<tr>
    		<td width="20">&nbsp;<input type="hidden" name="match_id[]" value="<?php echo $r->match_id; ?>" /></td>
    		<td width="200" align="center"><?php echo $r->match_timestamp; ?></td>
    		<td width="200" align="center"><?php echo $r->team_1; ?></td>
    		<td width="180" align="center">
    			<input type="text" name="match_goal1[]" value="" maxlength="2" size="1"  />
    			&nbsp;&nbsp; - &nbsp;&nbsp;
    			<input type="text" name="match_goal2[]" value="" maxlength="2" size="1"  />
    			&nbsp;&nbsp;&nbsp;&nbsp;
    			<input type="checkbox" name="forfait[]" value="1"  />
    		</td>
    		<td width="200" align="center"><?php echo $r->team_2; ?></td>
    	</tr>
    <?php
    endforeach;
    endif;
     
    echo form_close();
    ?>

  5. #5
    Futur Membre du Club
    Homme Profil pro
    Inscrit en
    Juillet 2010
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Juillet 2010
    Messages : 10
    Points : 7
    Points
    7
    Par défaut
    J'ai beau chercher, je ne trouve toujours pas pourquoi les autres valeurs passent mais pas celles des checkboxes....

  6. #6
    Membre à l'essai
    Homme Profil pro
    Développeur Web
    Inscrit en
    Août 2013
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Août 2013
    Messages : 7
    Points : 20
    Points
    20
    Par défaut
    Re-bonjour,

    Peux-tu faire un var_dump de tous les post avec cette ligne.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    var_dump($this->input->post());
    Comme ça on sera rapidement si ça vient de ton formulaire ou de ton traitement dans le contrôleur.

  7. #7
    Futur Membre du Club
    Homme Profil pro
    Inscrit en
    Juillet 2010
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Juillet 2010
    Messages : 10
    Points : 7
    Points
    7
    Par défaut
    en continuant mes recherches, j'ai modifié mon formulaire html comme ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
     
    <?php 
    echo form_open('admin/update_matchs/', array('class'=>'cssform', 'name'=>'UpdateMatchs') ); 
     
    if($Matchs_a_remplir != null):
    foreach($Matchs_a_remplir as $r):
    ?>
    	<tr>
    		<td width="20">&nbsp;<input type="hidden" name="match_id[]" value="<?php echo $r->match_id; ?>" /></td>
    		<td width="200" align="center"><?php echo $r->match_timestamp; ?></td>
    		<td width="200" align="center"><?php echo $r->team_1; ?></td>
    		<td width="180" align="center">
    			<input type="text" name="match_goal1[]" value="" maxlength="2" size="1"  />
    			&nbsp;&nbsp; - &nbsp;&nbsp;
    			<input type="text" name="match_goal2[]" value="" maxlength="2" size="1"  />
    			&nbsp;&nbsp;&nbsp;&nbsp;
                            <input type="hidden" name="forfait[]" value="0"  />
    			<input type="checkbox" name="forfait[]" value="1"  />
    		</td>
    		<td width="200" align="center"><?php echo $r->team_2; ?></td>
    	</tr>
    <?php
    endforeach;
    endif;
     
    echo form_close();
    ?>
    J'ai rajouté un <input type="hidden" name="forfait[]" value="0" /> juste avant le checkbox.
    cela me renvoit bien des valeurs '1' lorsque je coche une case mais toujours un enregistrement plus loin !!!
    La variable forfait devrait être à 1 où le match_id = 2506 et c'est à l'enregistrement suivant qu'elle est à 1 !
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
     
    Array
    (
        [0] => Array
            (
                [match_goal1] => 3
                [match_goal2] => 6
                [forfait] => 0
                [match_id] => 2587
            )
     
        [1] => Array
            (
                [match_goal1] => 8
                [match_goal2] => 2
                [forfait] => 0
                [match_id] => 2503
            )
     
        [2] => Array
            (
                [match_goal1] => 0
                [match_goal2] => 5
                [forfait] => 0
                [match_id] => 2506
            )
     
        [3] => Array
            (
                [match_goal1] => -1
                [match_goal2] => -1
                [forfait] => 1
                [match_id] => 2504
            )
     
        [4] => Array
            (
                [match_goal1] => -1
                [match_goal2] => -1
                [forfait] => 0
                [match_id] => 2507
            )
    Je remets le contrôleur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
     
    	function update_matchs() {
     
    		if($this->input->post('Update_matchs')=='oui') {
     
    			$data = array();
     
    			$match_id    = $this->input->post('match_id');
    			$match_goal1 = $this->input->post('match_goal1');
    			$match_goal2 = $this->input->post('match_goal2');		
    			$forfait     = $this->input->post('forfait');
     
    			foreach($match_id as $rec_key => $rec_val) {
     
    				if($match_goal1[$rec_key] == '' ) { $match_goal1[$rec_key] = '-1'; } 
    				if($match_goal2[$rec_key] == '' ) { $match_goal2[$rec_key] = '-1'; } 
     
    				$data[] = array(
    					'match_goal1' => $match_goal1[$rec_key],
    					'match_goal2' => $match_goal2[$rec_key],
    					'forfait' 	  => $forfait[$rec_key],
    					'match_id'    => $match_id[$rec_key],
    				);
    			}
     
    			echo '<pre>';
    			print_r($data);
    			echo '</pre>';
    			exit;
     
     
    			$this->db->update_batch('foot_matches', $data, 'match_id'); 
     
    			redirect('admin/Update_matchs/ModOK');
     
    		}
    	}

  8. #8
    Futur Membre du Club
    Homme Profil pro
    Inscrit en
    Juillet 2010
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Juillet 2010
    Messages : 10
    Points : 7
    Points
    7
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    echo '<pre>';
    	var_dump($this->input->post('match_id'));
    	var_dump($this->input->post('match_goal1'));
    	var_dump($this->input->post('match_goal2'));
    	var_dump($this->input->post('forfait'));
    echo '</pre>';
    me donne :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
     
    array(15) {
      [0]=>
      string(4) "2587"
      [1]=>
      string(4) "2503"
      [2]=>
      string(4) "2506"
      [3]=>
      string(4) "2504"
      [4]=>
      string(4) "2507"
      [5]=>
      string(4) "2510"
      [6]=>
      string(4) "2508"
      [7]=>
      string(4) "2505"
      [8]=>
      string(4) "2509"
      [9]=>
      string(4) "2514"
      [10]=>
      string(4) "2511"
      [11]=>
      string(4) "2512"
      [12]=>
      string(4) "2515"
      [13]=>
      string(4) "2517"
      [14]=>
      string(4) "2516"
    }
    array(15) {
      [0]=>
      string(1) "3"
      [1]=>
      string(1) "8"
      [2]=>
      string(1) "0"
      [3]=>
      string(0) ""
      [4]=>
      string(0) ""
      [5]=>
      string(0) ""
      [6]=>
      string(0) ""
      [7]=>
      string(0) ""
      [8]=>
      string(0) ""
      [9]=>
      string(0) ""
      [10]=>
      string(0) ""
      [11]=>
      string(0) ""
      [12]=>
      string(0) ""
      [13]=>
      string(0) ""
      [14]=>
      string(0) ""
    }
    array(15) {
      [0]=>
      string(1) "6"
      [1]=>
      string(1) "2"
      [2]=>
      string(1) "5"
      [3]=>
      string(0) ""
      [4]=>
      string(0) ""
      [5]=>
      string(0) ""
      [6]=>
      string(0) ""
      [7]=>
      string(0) ""
      [8]=>
      string(0) ""
      [9]=>
      string(0) ""
      [10]=>
      string(0) ""
      [11]=>
      string(0) ""
      [12]=>
      string(0) ""
      [13]=>
      string(0) ""
      [14]=>
      string(0) ""
    }
    array(16) {
      [0]=>
      string(1) "0"
      [1]=>
      string(1) "0"
      [2]=>
      string(1) "0"
      [3]=>
      string(1) "1"
      [4]=>
      string(1) "0"
      [5]=>
      string(1) "0"
      [6]=>
      string(1) "0"
      [7]=>
      string(1) "0"
      [8]=>
      string(1) "0"
      [9]=>
      string(1) "0"
      [10]=>
      string(1) "0"
      [11]=>
      string(1) "0"
      [12]=>
      string(1) "0"
      [13]=>
      string(1) "0"
      [14]=>
      string(1) "0"
      [15]=>
      string(1) "0"
    }
    15 entrées pour les id_match
    15 entrées pour les match_goal1
    15 entrées pour les match_goal1
    16 entrées pour les forfait !!!! Pourquoi 16 ?

  9. #9
    Invité
    Invité(e)
    Par défaut
    Bonjour,
    supprime la ligne (c'est le "16ème"...) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <input type="hidden" name="forfait[]" value="0"  />
    Et modifie :
    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    	<tr>
    		<td width="20">&nbsp;<input type="hidden" name="match_id[<?php echo $r->match_id; ?>]" value="<?php echo $r->match_id; ?>" /></td>
    		<td width="200" align="center"><?php echo $r->match_timestamp; ?></td>
    		<td width="200" align="center"><?php echo $r->team_1; ?></td>
    		<td width="180" align="center">
    			<input type="text" name="match_goal1[<?php echo $r->match_id; ?>]" value="" maxlength="2" size="1"  />
    			&nbsp; - &nbsp;
    			<input type="text" name="match_goal2[<?php echo $r->match_id; ?>]" value="" maxlength="2" size="1"  />
    			&nbsp;
    			<input type="checkbox" name="forfait[<?php echo $r->match_id; ?>]" value="1"  />
    		</td>
    		<td width="200" align="center"><?php echo $r->team_2; ?></td>
    	</tr>
    ...name="...[<?php echo $r->match_id; ?>]"...
    => L'utilisation de $r->match_id permet d'identifier de façon unique et sûre les éléments d'une même ligne.

    Pour info : SEULE les checkbox COCHEES sont envoyées par $_POST.
    Dernière modification par Invité ; 20/08/2013 à 11h38.

  10. #10
    Membre à l'essai
    Homme Profil pro
    Développeur Web
    Inscrit en
    Août 2013
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Août 2013
    Messages : 7
    Points : 20
    Points
    20
    Par défaut
    Bonjour,

    En continuant avec le code de jreaux62, il te suffit de boucler sur le tableau $_POST['forfait']. Ce tableau contient que les checkboxs que tu as coché, avec les clefs du tableau, tu obtiendras les id des matchs que tu souhaites modifier.

    Voila ton contrôleur modifier
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
     
    	function update_matchs() {
     
    		if($this->input->post('Update_matchs')=='oui') {
     
    			$data = array();
     
    			$match_id    = $this->input->post('match_id');
    			$match_goal1 = $this->input->post('match_goal1');
    			$match_goal2 = $this->input->post('match_goal2');		
    			$forfait     = $this->input->post('forfait');
     
    			foreach($forfait as $key=> $value) {
     
    				if($match_goal1[$key] == '' ) { $match_goal1[$key] = '-1'; } 
    				if($match_goal2[$key] == '' ) { $match_goal2[$key] = '-1'; } 
     
    				$data[] = array(
    					'match_goal1' => $match_goal1[$key],
    					'match_goal2' => $match_goal2[$key],
    					'forfait' 	  => $forfait[$key],
    					'match_id'    => $key,
    				);
    			}
     
    			echo '<pre>';
    			print_r($data);
    			echo '</pre>';
    			exit;
     
     
    			$this->db->update_batch('foot_matches', $data, 'match_id'); 
     
    			redirect('admin/Update_matchs/ModOK');
     
    		}
    	}

  11. #11
    Invité
    Invité(e)
    Par défaut
    Il suffit de "boucler" sur $match_id.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    			foreach($match_id as $key=> $value) {
    $key est :
    • l'INDEX de l'array, et même de tous les array de la même ligne !
    • mais aussi l'ID de la ligne.


    Mais comme seules les cases COCHEES sont envoyées :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    				if(!isset($match_goal1[$key])) { $match_goal1[$key] = '-1'; } 
    				if(!isset($match_goal2[$key])) { $match_goal2[$key] = '-1'; }

  12. #12
    Futur Membre du Club
    Homme Profil pro
    Inscrit en
    Juillet 2010
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Juillet 2010
    Messages : 10
    Points : 7
    Points
    7
    Par défaut
    Un tout grand merci à vous pour votre aide. Cela fonctionne maintenant parfaitement.

    ... en espérant pouvoir aussi un jour aider quelqu'un (avec mes modestes connaissances)...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Combien de checkboxes sélectionnées
    Par papawemba dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 13/03/2008, 15h15
  2. gridview+checkbox+sélectionner 1 checkbox
    Par tortuegenie dans le forum ASP.NET
    Réponses: 2
    Dernier message: 07/03/2008, 08h51
  3. Réponses: 9
    Dernier message: 23/06/2006, 17h30
  4. [checkbox] Sélectionner tous les checkbox
    Par GLDavid dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 02/05/2006, 17h05
  5. Envoi selon checkbox sélectionnées
    Par Polaire76 dans le forum Langage
    Réponses: 1
    Dernier message: 29/03/2006, 11h32

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo