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

Langage PHP Discussion :

Gestion de 2 boutons [PHP 7]


Sujet :

Langage PHP

  1. #1
    Membre actif Avatar de thierrybatlle
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Novembre 2005
    Messages
    618
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Tarn (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Enseignement

    Informations forums :
    Inscription : Novembre 2005
    Messages : 618
    Points : 222
    Points
    222
    Par défaut Gestion de 2 boutons
    Bonjour à tous,

    J'ai fait un petit bout de code avec 2 boutons qui bascule de la couleur verte à la couleur rouge.
    Mon problème :
    - Je clique sur le premier bouton : il bascule en rouge --> fonctionnement normal
    - Je clique sur le deuxième bouton : il bascule en rouge --> fonctionnement normal, par contre le premier bouton bascule en vert !!! --> problème rencontré il devrait rester rouge

    Voici mon code :
    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
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
            <meta content="text/html; charset=utf-8" http-equiv="content-type">
            <title>python</title>
     
            <link rel="stylesheet" href="css/bootstrap.min.css">
            <script src="js/jquery-3.3.1.min.js"></script>
            <script src="js/bootstrap.min.js"></script>
        </head>
        <body>   
            <form method="post">
                <div class="container">
                    <div class="row">
                        <div class="col-sm-4">
                            <br>
                            <?php 
                            if (isset($_POST['etatLedR']) && isset($_POST['btnLedR'])) {
     
                                if ($_POST['etatLedR'] == 'on') {
    				echo'<input type="hidden" name="etatLedR" value="off" />';
    				exec('sudo python3 /var/www/html/Python/python/ledRON.py');
    				echo '<button type="submit" class="btn btn-danger" name="btnLedR">Eteindre la LED rouge</button>';
     
    			// Si la variable session est OFFR nous éteignons la LED R et affichons le btn en vert
                                } elseif ($_POST['etatLedR'] == 'off'){ 
    				echo'<input type="hidden" name="etatLedR" value="on" />';
    				exec('sudo python3 /var/www/html/Python/python/ledROFF.py');
    				echo '<button type="submit" class="btn btn-success" name="btnLedR">Allumer la LED rouge</button>';
                                }
    			} else { 
                                // Nous affichons le btn en vert
                                echo'<input type="hidden" name="etatLedR" value="on" />';
                                echo '<button type="submit" class="btn btn-success" name="btnLedR">Allumer la LED rouge</button>'; 
    			}?>
     
                        </div>
                        <div class="col-sm-4">
                            <br>
                            <?php 
                            if (isset($_POST['etatLedV']) && isset($_POST['btnLedV'])) {
     
                                if ($_POST['etatLedV'] == 'on') {
    				echo'<input type="hidden" name="etatLedV" value="off" />';
    				exec('sudo python3 /var/www/html/Python/python/ledVON.py');
    				echo '<button type="submit" class="btn btn-danger" name="btnLedV">Eteindre la LED verte</button>';
     
    				// Si la variable session est OFFV nous éteignons la LED V et affichons le btn en vert
                                } elseif ($_POST['etatLedV'] == 'off'){ 
                                    echo'<input type="hidden" name="etatLedV" value="on" />';
    				exec('sudo python3 /var/www/html/Python/python/ledVOFF.py');
    				echo '<button type="submit" class="btn btn-success" name="btnLedV">Allumer la LED verte</button>';            
    							}   
                                // Teste si une variable session existe sinon nous affichons le btn en vert
                            } else { 
                                echo'<input type="hidden" name="etatLedV" value="on" />';
                                echo '<button type="submit" class="btn btn-success" name="btnLedV">Allumer la LED verte</button>';
                            }?>
                        </div>
                    </div>
                </div>
            </form>
        </body>
    </html>
    Je vous remercie beaucoup.

  2. #2
    Invité
    Invité(e)
    Par défaut
    Bonjour,

    1- Ton code est beaucoup trop complexe.
    Il peut être largement simplifié.

    2- Tu as 2 boutons type="submit".
    -> UN SEUL est présent dans $_POST quand on clique dessus.

    Donc, tes conditions sont mauvaises.
    C'est avant tout un problème de logique.

    • Si bouton1 est cliqué -> on change led1 (si 'on' -> 'off' sinon 'on')
    • Si bouton2 est cliqué -> on change led2 (si 'on' -> 'off' sinon 'on')

    Point.

    Ca se traduit par :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?php 
    // ---------
    $etatLedR 	= (isset($_POST['etatLedR']))? $_POST['etatLedR'] : 'on';
    $btnLedR 	= (isset($_POST['btnLedR']))? true : false; // bouton R cliqué ou non
    if( $btnLedR ){ $etatLedR = ($etatLedR=='on')? 'off' : 'on'; }
    // ---------
    $etatLedV 	= (isset($_POST['etatLedV']))? $_POST['etatLedV'] : 'on';
    $btnLedV 	= (isset($_POST['btnLedV']))? true : false; // bouton V cliqué ou non
    if( $btnLedV ){ $etatLedV = ($etatLedV=='on')? 'off' : 'on'; }
    // ---------
    ?>
    Ensuite, il suffit d'afficher les bonnes données en fonction de ces résultats.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <?php 
    if( $etatLedR == 'on' ){
    	// ...
    } else {
    	// ...
    }
    // ---------
    if( $etatLedV == 'on' ){
    	// ...
    } else {
    	// ...
    } 
    ?>
    Dernière modification par Invité ; 09/10/2018 à 14h07.

  3. #3
    Membre actif Avatar de thierrybatlle
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Novembre 2005
    Messages
    618
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Tarn (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Enseignement

    Informations forums :
    Inscription : Novembre 2005
    Messages : 618
    Points : 222
    Points
    222
    Par défaut
    Bonsoir,

    J'ai testé le bout de code mais cela ne fonctionne pas.
    Je n'ai pas de grandes connaissances en PHP et j'ai sûrement mal utilisé le code fournis.
    Voici mon nouveau code :
    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 HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
            <meta content="text/html; charset=utf-8" http-equiv="content-type">
            <title>python</title>
     
            <link rel="stylesheet" href="css/bootstrap.min.css">
            <script src="js/jquery-3.3.1.min.js"></script>
            <script src="js/bootstrap.min.js"></script>
        </head>
        <body>
     
            <?php 
                // ---------
                $etatLedR 	= (isset($_POST['etatLedR']))? $_POST['etatLedR'] : 'on';
                $btnLedR 	= (isset($_POST['btnLedR']))? true : false; // bouton R cliqué ou non
                if( $btnLedR ){ $etatLedR = ($etatLedR=='on')? 'off' : 'on'; }
                // ---------
                $etatLedV 	= (isset($_POST['etatLedV']))? $_POST['etatLedV'] : 'on';
                $btnLedV 	= (isset($_POST['btnLedV']))? true : false; // bouton V cliqué ou non
                if( $btnLedV ){ $etatLedV = ($etatLedV=='on')? 'off' : 'on'; }
                // ---------
            ?>
            <form method="post">
                <div class="container">
                    <div class="row">
                        <div class="col-sm-4">
                            <br>
                            <?php   
     
                                if( $etatLedR == 'on' ){
                                    echo'<input type="hidden" name="etatLedR" value="off" />';
    				exec('sudo python3 /var/www/html/Python/python/ledRON.py');
    				echo '<button type="submit" class="btn btn-danger" name="btnLedR">Eteindre la LED rouge</button>';
                                } else {
                                    echo'<input type="hidden" name="etatLedR" value="on" />';
    				exec('sudo python3 /var/www/html/Python/python/ledROFF.py');
    				echo '<button type="submit" class="btn btn-success" name="btnLedR">Allumer la LED rouge</button>';
                                }
                                // ---------
                                if( $etatLedV == 'on' ){
                                    echo'<input type="hidden" name="etatLedV" value="off" />';
    				exec('sudo python3 /var/www/html/Python/python/ledVON.py');
    				echo '<button type="submit" class="btn btn-danger" name="btnLedV">Eteindre la LED verte</button>';
                                } else {
                                    echo'<input type="hidden" name="etatLedV" value="on" />';
    				exec('sudo python3 /var/www/html/Python/python/ledVOFF.py');
    				echo '<button type="submit" class="btn btn-success" name="btnLedV">Allumer la LED verte</button>';
                                } 
                                ?>
     
     
                        </div>
                        <div class="col-sm-4">
                            <br>
                            blabla
                        </div>
                    </div>
                </div>
            </form>
        </body>
    </html>
    Merci à tous pour votre aide.

  4. #4
    Membre actif Avatar de thierrybatlle
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Novembre 2005
    Messages
    618
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Tarn (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Enseignement

    Informations forums :
    Inscription : Novembre 2005
    Messages : 618
    Points : 222
    Points
    222
    Par défaut
    Bonjour,

    Pour ce que cela intéresse voici le code opérationnel.
    Merci pour votre aide

    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
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
            <meta content="text/html; charset=utf-8" http-equiv="content-type">
            <title>python</title>
     
            <link rel="stylesheet" href="css/bootstrap.min.css">
            <script src="js/jquery-3.3.1.min.js"></script>
            <script src="js/bootstrap.min.js"></script>
        </head>
        <body> 
            <?php 
                // ---------
                $etatLedR 	= (isset($_POST['etatLedR']))? $_POST['etatLedR'] : 'on';
                $btnLedR 	= (isset($_POST['btnLedR']))? true : false; // bouton R cliqué ou non
                if( $btnLedR ){ $etatLedR = ($etatLedR=='on')? 'off' : 'on'; }
                // ---------
                $etatLedV 	= (isset($_POST['etatLedV']))? $_POST['etatLedV'] : 'on';
                $btnLedV 	= (isset($_POST['btnLedV']))? true : false; // bouton V cliqué ou non
                if( $btnLedV ){ $etatLedV = ($etatLedV=='on')? 'off' : 'on'; }
                // ---------
            ?>
                <form method="post">
                <div class="container">
                    <div class="row">
                        <div class="col-sm-4">
                            <br>
                            <?php   
     
                                if( $etatLedR != 'on' ){
                                    echo'<input type="hidden" name="etatLedR" value="off" />';
    				exec('sudo python3 /var/www/html/Python/python/ledRON.py');
    				echo '<button type="submit" class="btn btn-danger" name="btnLedR">Eteindre la LED rouge</button>';
                                } else {
                                    echo'<input type="hidden" name="etatLedR" value="on" />';
    				exec('sudo python3 /var/www/html/Python/python/ledROFF.py');
    				echo '<button type="submit" class="btn btn-success" name="btnLedR">Allumer la LED rouge</button>';
                                }                       
                            ?>                                                                        
                        </div>
                        <div class="col-sm-4">
                            <br>
                            <?php
                             if( $etatLedV != 'on' ){
                                    echo'<input type="hidden" name="etatLedV" value="off" />';
    				exec('sudo python3 /var/www/html/Python/python/ledVON.py');
    				echo '<button type="submit" class="btn btn-danger" name="btnLedV">Eteindre la LED verte</button>';
                                } else {
                                    echo'<input type="hidden" name="etatLedV" value="on" />';
    				exec('sudo python3 /var/www/html/Python/python/ledVOFF.py');
    				echo '<button type="submit" class="btn btn-success" name="btnLedV">Allumer la LED verte</button>';
                                } 
                            ?>
                        </div>
                    </div>
                </div>
            </form>
        </body>
    </html>

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

Discussions similaires

  1. Réponses: 4
    Dernier message: 22/03/2012, 11h30
  2. Réponses: 20
    Dernier message: 17/07/2009, 10h17
  3. Gestion de plusieurs boutons dans un formulaire
    Par guy_antoine_mav dans le forum Langage
    Réponses: 2
    Dernier message: 10/10/2008, 16h07
  4. pb gestion focus + clique bouton
    Par stephsoux59 dans le forum AWT/Swing
    Réponses: 3
    Dernier message: 26/11/2007, 13h09
  5. Gestion son click bouton
    Par delavega dans le forum Flash
    Réponses: 1
    Dernier message: 22/02/2007, 10h25

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