avoid anchor tag behavior

0

Hello, I have a system that by clicking on a link, it verifies if the user is logged in and depending on this the link changes; it works well but if the user is not logged in yet he enters the section and then quickly redirects to the page I want, the idea is that he does not enter the section but redirects at once to the page I want.

<div id="r">
 <?php
  foreach ($mysqli->query('SELECT * FROM r') as $fila) {
   echo "<div class='card-action'>
          <a href='"; echo isset($_SESSION['id']) ? "#" : "./gin.php"; echo "' class='rr'>reservar</a>
          <input type='text' value='".$fila['id']."'>
         </div>";
      }
    ?>
  </div>

  <script>
    $(document).ready(function(){
      $("#r").on('click','a.rr',function(){
        var id = $(this).siblings('input').val();
        $.ajax({
          type: "GET",
          url: "vation.php",
          data: { id:id },
          success: function(data) {
            $('#r').html(data);
          }
        });
      });
    });
  </script>

An example of the closest thing I need would be to book something; if the user is registered, he continues normally otherwise he should take it to register and not allow the normal flow.

    
asked by Johan Solano Contreras 10.12.2018 в 05:15
source

1 answer

1

You do not need to run an ajax to check if the user is registered, since you are going to redirect in any case, you can do the check on the destination page and depending on the user's status show one or the other.

In your case:

<div id="r">
    <?php
    foreach ($mysqli->query('SELECT * FROM r') as $fila) {
        echo "<div class='card-action'>
                <a href='reservar.php?material=".$fila['id'] . "' class='rr'>reservar</a>
             </div>";
    }
    ?>
</div>

and in reserva.php :

<?php
 if (!isset($_SESSION['id'])) {
   include('login.php');
  } else {
   include('reserva_material.php');
 }

?>

It's just a very basic approach, but it would do what you ask. You have to also foresee how you want to behave after logging in, surely you are interested in continuing with the reservation.

  

Notice that I modified the anchor you mentioned in the question, a <a> and   a <input> do not work together as you have put. Or you use a    <form> with <input> or use a <a> with parameters in the url

    
answered by 10.12.2018 / 17:42
source