Multiple buttons on the same page with different functions

Asked

Viewed 324 times

-1

I have the following code that has 3 buttons, when pressing a button, it should change a value in my table in Mysql, however, when I press one of the buttons, it is the same as having pressed all the other buttons at the same time.

Follows the code:

<?php
    include("salvateste.php");

    $QUERYM = "SELECT * FROM botoes";

    $execute = mysqli_query($conexao, $QUERYM);

    $line = mysqli_fetch_array($execute);

    $valor  = $line['b1'];
    $valor1 = $line['b2'];
    $valor2 = $line['b3'];

    if($valor == '1'){
        $est1 = "Ligado";
    } else {
        $est1 = "Desligado";
    }

    if($valor1 == '1'){
        $est2 = "Ligado";
    } else {
        $est2 = "Desligado";
    }

    if($valor2 == '1'){
        $est3 = "Ligado";
        $est4 = "Desligado";
    } else {
        $est3 = "Desligado";
        $est4 = "Ligado";
    }


    ?>
<br>

<center>
<form method="GET" ation="" action="controles.php">
<button class="css_btn_class" type="submit" name="Lamp1">Lampada 1 - <?php echo $est1 ?></button>&nbsp;&nbsp;
</form>
<form method="GET" ation="" action="controles.php">
<button class="css_btn_class" type="submit" name="Lamp2">Lampada 2 - <?php echo $est2 ?></button>
</form>
    <br>
    <br>
    <br>
    <form method="GET" ation="" action="controles.php">
    <button class="css_btn_class" type="submit" name="EE">Energia Convencional - <?php echo $est3 ?><br>
    Energia limpa - <?php echo $est4 ?></button>
</form>
</center>


<?php

include("salvateste.php");

$queryA1 = "UPDATE botoes SET b1='1' WHERE b1 = '0'";
$queryA0 = "UPDATE botoes SET b1='0' WHERE b1 = '1'";

$queryB1 = "UPDATE botoes SET b2='1' WHERE b2 = '0'";
$queryB0 = "UPDATE botoes SET b2='0' WHERE b2 = '1'";

$queryC1 = "UPDATE botoes SET b3='1' WHERE b3 = '0'";
$queryC0 = "UPDATE botoes SET b3='0' WHERE b3 = '1'";

if(isset($_GET['Lamp1'])){
    if($est1 == "Desligado"){
        $est1 = "Ligado";
    } else if($est1 = "Ligado"){
        $est1 = "Desligado";
    }
} 

if(isset($_GET['Lamp2'])){
    if($est2 == "Desligado"){
        $est2 = "Ligado";
    } else if($est2 = "Ligado"){
        $est2 = "Desligado";
    }
} 

if(isset($_GET['EE'])){
    if($est3 == "Desligado" && $est4 == "Ligado"){
        $est3 = "Ligado";
        $est4 = "Desligado";
    } else if($est3 = "Ligado" && $est4 == "Desligado"){
        $est3 = "Desligado";
        $est4 = "Ligado";
    }
} 

 if($est1 == "Ligado"){
 mysqli_query($conexao, $queryA0);
 }else {
 mysqli_query($conexao, $queryA1);
 }   

 if($est2 == "Ligado"){
 mysqli_query($conexao, $queryB0);
 }else {
 mysqli_query($conexao, $queryB1);
 } 

 if($est3 == "Ligado" && $est4 == "Desligado"){
 mysqli_query($conexao, $queryC0);
 }else {
 mysqli_query($conexao, $queryC1);
 } 

 ?>
  • All buttons have the same action... So you need to see how the controles.php

  • I understood that Voce differentiated by the value of the button and it should work, I made a test here and also did not work I believe that it is better to create one <input type=hidden name='EE'> even

  • what this Hidden guy would do?

1 answer

1


I’m not sure I understand what you want to do. How all actions send to the same file (control.php) Do the action with variable in GET like this:

<form method="GET" ation="" action="controles.php?qlamp=lamp1">
<form method="GET" ation="" action="controles.php?qlamp=lamp2">
<form method="GET" ation="" action="controles.php?qlamp=lamp3">

In the archive Controle.php you treat the variables as they should be treated

and this parameter ation=""is there? this right?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.