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

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    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
    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
    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 émérite
    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
    Par défaut
    Et affiche l'html du formulaire

  4. #4
    Membre averti
    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
    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
    Membre averti
    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
    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
    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
    Membre averti
    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
    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');
     
    		}
    	}

+ 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