verify php variable before doing the submit

1

I am learning on the fly and half blocked at times with some topic. I have a form with 2 buttons on my page detalle.php:

                    <form action="carrito.php" method="POST" name="compra">
                        <input name="id_txt" type="hidden" value="<?php echo $id?>" />
                        <input name="nombre" type="hidden" value="<?php echo $nombre?>" />
                        <input name="precio" type="hidden" value="<?php echo $precio?>" />
                        <input name="cantidad" type="hidden" value="1" />
                        <input name="stock" type="hidden" value="<?php echo $stock?>" />
                        <?php 
                        if ($stock>0) { ?>
                        <div id="detalle_caja_botones">   
                            <input class="boton_detalle" id="cancela2" name="cancelar" type="submit" value="Cancelar" />
                            <input class="boton_detalle" id="comprar2" name="Comprar" type="submit" value="Comprar" />
                        </div>
                        <?php }else{
                            echo "Producto temporalmente agotado"; ?>
                            <input class="boton_detalle" name="Comprar" type="submit" value="Cancelar" />
                        <?php    
                        }
                        ?>
                    </form>

By clicking the 1st button # cancel2, I capture the click with jquery / preventdefault and redirect to the previous page, that fits me ok.

The problem is the second button, # buy2, when clicking, I need to check that the user is logged into the system (connected PHP variable Boolean $), if it is connected, it executes the submit, if it is not connected I send it a warning and also redirect to another page or to the registration form.

I tried but without success (jquery complicates me because the variable is php), I will appreciate any help before losing the few hairs that I have left haha.

Greetings!

    
asked by look68 20.10.2016 в 05:06
source

1 answer

2

the variables are not made to preserve information along subsequent accesses. You should use Sessions to perform this validation, Enabling session_start(); on all pages that require knowing the status of these. In your login you would have something like this (Basic example)

<?php      
   session_start();
   if("UsuarioRegistrado") /*Verificar en la base de datos */ 
      $_SESSION['logueado'] = true;
?>

In the Shopping Cart you would do the following to validate if you are logged in

<?php      
   session_start();
   if (isset($_SESSION['logueado']) && $_SESSION['logueado'] == true)
       /* Realizar la Compra o Añadir al Carrito */
   else
      header('Location: '."UrlDelLogin");/*redireccionamiento*/
 ?>
    
answered by 20.10.2016 в 05:38