How do I add 1 in a field of a table, when I click on a link?

0

The 1 that I refer to is simply adding 1 to a field in a table, by clicking on a link. This is the link code:

<?php if ($pagina) { ?>
  <div class="row">
    <div class="col-md-6"> 
      <h3><a href="<?php echo $pagina['url']?>" target="blank"><span 
      class="hint--top-right" data-hint="Clic aquí para acceder a la web de 
      la propiedad">PAGINA WEB</a></h3>
    </div>
  </div>
<?php } ?>

And this is the function I should run:

# ----- Sumar click_website a la propiedad
function addClickWebsite($conexion, $id)  {
  $consulta = $conexion->prepare("UPDATE alquileres set 
      click_website=click_website+1 where id=$id");
  $consulta->execute();
  return $consulta->fetchAll();
}

I need help to link the function to the click on the link, someone told me that I must do it with ajax, but unfortunately I have no knowledge. Thanks.

    
asked by Ricardo Pastuszek 06.08.2018 в 13:33
source

2 answers

1

Using .ajax() of jQuery , you can capture the event click and have that action executed on the server side.

HTML

<?php if ($pagina) { ?>
  <div class="row">
    <div class="col-md-6"> 
      <h3><a href="<?php echo $pagina['url']?>" target="blank" id="clickEvent"><span 
      class="hint--top-right" data-hint="Clic aquí para acceder a la web de 
      la propiedad">PAGINA WEB</a></h3>
    </div>
  </div>
<?php } ?>

Javascript:

// le agrego al id clickEvent un evento de click para luego
// ejecutar el ajax
$('#clickEvent').click(function() {
  var id_alquiler = 232; // acá reemplazo esto por el id del alquiler que corresponda
  $.ajax({
    method: 'post',
    url: 'mi-accion.php',
    data: {id: id_alquiler},
    success: function(data) {
      console.log('acción realizada', data);
    }
  });
});

my-action.php

# ----- Sumar click_website a la propiedad

// acá hago la conexión a la DB
$conexion = mysqli_connect("servidor", "usuario", "password");

// id que paso por javascript
$id = $_POST['id'];

$consulta = $conexion->prepare(
  "UPDATE alquileres set click_website=click_website+1 where id=$id"
);
$consulta->execute();
echo json_encode($consulta->fetchAll());
    
answered by 06.08.2018 / 14:54
source
1

With Javascript it is simple, you can add any field: I leave the documentation for you to implement it, it is simple.

Query source: link

    
answered by 06.08.2018 в 14:17