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

jQuery Discussion :

Checkbox


Sujet :

jQuery

  1. #1
    Membre du Club
    Inscrit en
    Janvier 2011
    Messages
    117
    Détails du profil
    Informations forums :
    Inscription : Janvier 2011
    Messages : 117
    Points : 47
    Points
    47
    Par défaut Checkbox
    bonjour

    voila après quelques recherches sur le web j'ai trouvé une petite solution,
    grâce a ce nouveau code on peut obtenir les données des checkbox checked
    les données sont sous forme d'un Array , le problème c'est que pour parcourir ce tableau
    (après avoir cocher quelques checkbox) les donnes prennent un index déferrent
    ici dans cette output ci_dessous , j'ai coche 3 checkboxes vous voyez pour accéder a les valeurs de checkboxes
    il y'a celles qui sont enregistrées dans Array['index'] et celles dans Array['value'] !!
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Array ( [0] => 2 [name] => model [model] => Array ( [1] => 3 ) [value] => 1 [check] => model )
    donc ce code nécessite une petite correction pour pouvoir obtenir les données dans l'ordre suivant:

    nom de checkbox, valeur de checkbox.

    voila mon script
    merci d'avance
    index.php
    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
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <html xmlns="http://www.w3.org/1999/xhtml" lang="fr" xml:lang="fr">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
     
     
     
     
        <!-- Bootstrap core CSS -->
     
      </head>
     
    <body>
    <script>
    function getData(names) {
    	var value= $('input:checkbox:checked').val();
    	  var status2 = $('input:checkbox').serialize();
           var dataString = 'name=' + status2 + '&value='+ value + '&check=' + names;
    	$.ajax( {
        type: 'POST',
        url: 'getAll.php',
       data: dataString,
     
        success: function(data) {
            $('#showdata').html(data);
        }
    } );
    }
    </script>
    <p><b>Couleur</b></p>
    <div class="checkbox">
      <label><input type="checkbox" id="model" name="model[]" value=1 onchange="getData(this.name)">Bleu  <span class="badge">(12556)</span></label>
      <label><input type="checkbox" id="model1" name="model[]" value=2 onchange="getData(this.name)">Rouge  <span class="badge">(6268)</span></label>
       <label><input type="checkbox" id="model2" name="model[]" value=3 onchange="getData(this.name)">Argent ou gris  <span class="badge">(21 660)</span></label>
       <label><input type="checkbox" id="model3" name="model[]" value=4 onchange="getData(this.name)">Noir  <span class="badge">(15 740)</span></label>
      <label><input type="checkbox" id="model4" name="model[]" value=5 onchange="getData(this.name)">Beige  <span class="badge">(717)</span></label>
       <label><input type="checkbox" id="model5"  name="model[]" value=6 onchange="getData(this.name)">Brown  <span class="badge">(852)</span></label>
     
     
     
     
    </div>
     
     
    <p><b>Puissance</b></p>
    <div class="checkbox">
      <label><input type="checkbox" name="puissance[]" value=1 onchange="getData(this.name)"><75 ch  <span class="badge">(30 136)</span></label>
      <label><input type="checkbox" name="puissance[]" value=2 onchange="getData(this.name)">75-100 cv  <span class="badge">(15 244)</span></label>
       <label><input type="checkbox" name="puissance[]" value=3 onchange="getData(this.name)">101-125 ch  <span class="badge">(13 885)</span></label>
      <label><input type="checkbox" name="puissance[]" value=4 onchange="getData(this.name)">126-150 ch  <span class="badge">(3707)</span></label>
       <label><input type="checkbox" name="puissance[]" value=5 onchange="getData(this.name)">151-200 ch  <span class="badge">(1976)</span></label>
      <label><input type="checkbox" name="puissance[]" value=6 onchange="getData(this.name)">201-250 hp  <span class="badge">(533)</span></label>
     
     
    </div>
    <div id="showdata">
     
    </div>
    </body>
    </html>
    getAll.php
    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
     
     
     
    <?php
        //The array we begin with
        $start_array = array($_POST);
     
        //Convert the array to a string
        $array_string = print_r($start_array, true);
     
        //Get the new array
        $end_array = text_to_array($array_string);
     
        //Output the array!
        print_r($end_array);
     
        function text_to_array($str) {
     
            //Initialize arrays
            $keys = array();
            $values = array();
            $output = array();
     
            //Is it an array?
            if( substr($str, 0, 5) == 'Array' ) {
     
                //Let's parse it (hopefully it won't clash)
                $array_contents = substr($str, 7, -2);
                $array_contents = str_replace(array('[', ']', '=>'), array('#!#', '#?#', ''), $array_contents);
                $array_fields = explode("#!#", $array_contents);
     
                //For each array-field, we need to explode on the delimiters I've set and make it look funny.
                for($i = 0; $i < count($array_fields); $i++ ) {
     
                    //First run is glitched, so let's pass on that one.
                    if( $i != 0 ) {
     
                        $bits = explode('#?#', $array_fields[$i]);
                        if( $bits[0] != '' ) $output[$bits[0]] = $bits[1];
     
                    }
                }
     
                //Return the output.
                return $output;
     
            } else {
     
                return null;
            }
     
        }
    ?>

  2. #2
    Rédacteur

    Avatar de danielhagnoul
    Homme Profil pro
    Étudiant perpétuel
    Inscrit en
    Février 2009
    Messages
    6 389
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 73
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant perpétuel
    Secteur : Enseignement

    Informations forums :
    Inscription : Février 2009
    Messages : 6 389
    Points : 22 933
    Points
    22 933
    Billets dans le blog
    125
    Par défaut
    php => http://www.developpez.net/forums/f443/php/

    Ici, on ne traite que les problèmes du code source généré par le PHP

    Blog

    Sans l'analyse et la conception, la programmation est l'art d'ajouter des bogues à un fichier texte vide.
    (Louis Srygley : Without requirements or design, programming is the art of adding bugs to an empty text file.)

Discussions similaires

  1. [JSP]checkbox
    Par orisis dans le forum Servlets/JSP
    Réponses: 6
    Dernier message: 16/04/2013, 13h53
  2. [VB.NET] Insérer une colonne de CheckBox dans un DataGrid
    Par Manue.35 dans le forum Windows Forms
    Réponses: 2
    Dernier message: 22/05/2003, 11h44
  3. Checkbox
    Par rgarnier dans le forum XMLRAD
    Réponses: 11
    Dernier message: 06/03/2003, 10h48
  4. Couleur d'un CheckBox
    Par benj63 dans le forum C++Builder
    Réponses: 4
    Dernier message: 15/07/2002, 14h48
  5. CheckBox en Read Only
    Par MrJéjé dans le forum C++Builder
    Réponses: 7
    Dernier message: 23/06/2002, 15h00

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