Form button

0

I have a page with a form in which you have to fill in the user field and select an ingredient with select. I have to verify that the user that is entered is in the database. If not, I'll get the following echo and a button to go back:

$usuario=$_POST['usuario'];
 if( !( $usuario == "" ) ){
     $consulta="SELECT * FROM usuario WHERE usuario = '$usuario'";
     $resultado=mysqli_query($con, $consulta);
     $con=mysqli_num_rows($resultado);
     if($con == 0){
        echo"Usuario inexistente. Introduzca un usuario registrado";
    }else{
        $con==1;
    }
    echo"
    <form action=\"http://localhost/PROYECTO/pedirPlato.php\">
    <div class=\"boton\"><input type=\"submit\" value=\"Vuelve\"/></div>
    /form>
    ";
}

The fact is that when I enter a user who is in the database and I also select an ingredient, I have to put a different button to take me to another page. Code:

if (isset($_POST['ingrediente']) && ($con == 1)){

    $resultadoConsulta="SELECT apellidos FROM usuario WHERE usuario = '$usuario'";
    $sql=mysqli_query($con, $resultadoConsulta);

            $fila=  mysqli_fetch_assoc($sql);
            while ($fila != null){
                $fila['apellidos'];
                echo "Apellidos: " .$fila['apellidos'];
                $fila= mysqli_fetch_assoc($sql);
            }
     echo"
    <form action=\"http://localhost/PROYECTO/pagar.php\">
    <div class=\"boton2\"><input type=\"submit\" value=\"Haz el pago\"/></div>
    /form>
    ";
}

The problem I have is that when I add this last bit of code, the two buttons appear, when I would only have to leave the last "Make the payment" and not the one above "Come back". Where should I place the first button so it does not repeat?

    
asked by LolaBonet 23.12.2018 в 20:58
source

2 answers

0

First, if the user is not in the database and you want the "Go back" button to appear, it would be:

$usuario=$_POST['usuario'];
 if( !( $usuario == "" ) ){
     $consulta="SELECT * FROM usuario WHERE usuario = '$usuario'";
     $resultado=mysqli_query($con, $consulta);
     $con=mysqli_num_rows($resultado);
     if($con == 0){
        echo"Usuario inexistente. Introduzca un usuario registrado";
        echo "<form action=\"http://localhost/PROYECTO/pedirPlato.php\">
        <div class=\"boton\"><input type=\"submit\" value=\"Vuelve\"/></div>
        /form>";
     }else{
        $con=1;
     }
}

Then if the user is in the database and you want the other code, it would be:

if (isset($_POST['ingrediente']) && ($con == 1)){
    $resultadoConsulta="SELECT apellidos FROM usuario WHERE usuario = '$usuario'";
    $sql=mysqli_query($con, $resultadoConsulta);
    $fila=  mysqli_fetch_assoc($sql);
            while ($fila != null){
                $fila['apellidos'];
                echo "Apellidos: " .$fila['apellidos'];
                $fila= mysqli_fetch_assoc($sql);
            }
     echo"
    <form action=\"http://localhost/PROYECTO/pagar.php\">
    <div class=\"boton2\"><input type=\"submit\" value=\"Haz el pago\"/></div>
    /form>
    ";
}
    
answered by 23.12.2018 / 21:16
source
0

You can make the button show, and when the user exists in the bd that the button is hidden:

Code:

$tipo = "button";  //ya que de momento es un boton visible
$usuario=$_POST['usuario'];
 if( !( $usuario == "" ) ){
     $consulta="SELECT * FROM usuario WHERE usuario = '$usuario'";
     $resultado=mysqli_query($con, $consulta);
     $con=mysqli_num_rows($resultado);
     if($con == 0){
        echo"Usuario inexistente. Introduzca un usuario registrado";
    }else{
        $tipo = "hidden"; //si el usuario existe el input pasa a ser de tipo oculto.
        $con=1;
    }
    echo"
    <form action='http://localhost/PROYECTO/pedirPlato.php'>
    <div class='boton'><input type=".$tipo." value='Vuelve'/></div>
    </form>
    ";
}

In case you want the button to have it in html:

<input type="<?php echo $tipo; ?>" value="Vuelve" />
  

I have noticed that you have variable $con after else   comparing with 1 and I do not know if it's a bug in the writing of the code or   if it is appropriate, but if it is to assign the value 1 is: $con=1; .

     

And I've also seen that you do not close the form tag, but as in the   I do not know what is due and I only inform you, and I mean that   the form tag does not have < and it should be </form> .

    
answered by 24.12.2018 в 02:41