Good morning,
I am using a small script to update my database when I click on a link. That link has an onclick
event. At the beginning to see the values that I was giving in the console, I did not have any links. Once seen that it worked, when putting the link, something happens, since the database is not modified.
The HTML code referring to the link:
echo "<a rel='nofollow' onclick='leido(" . $id_mencion . ")' href='foro.php?foro=" . $mencion['foro'] . "&subforo=" . $mencion['subforo'] . "&hilo=" . str_replace(" ", "%",$informacion_hilo[0]['asunto']) . "&ID=" . $id_hilo . "&pagina=" . $mencion['pagina_hilo'] . "'><strong>" . $informacion_hilo[0]['asunto'] . "</strong></a>";
The Script:
<script>
function leido(respuesta_id) {
var id_respuesta = respuesta_id;
console.log(id_respuesta);
$.ajax ({
type: 'POST',
url: 'process_leido.php',
data: { "id_respuesta":id_respuesta }
});
};
</script>
I also put the code of process_leido.php:
<?php
session_start();
require 'admin/config.php';
try {
$conexion = new PDO($bd_config['dbname'], $bd_config['usuario'], $bd_config['password'] );
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
$id = isset($_POST['id_respuesta'])? $_POST['id_respuesta'] : 0;
$statement = $conexion->prepare("UPDATE menciones SET leido = 1 WHERE id = :id");
$statement->execute(array(":id" => $id));
?>
However, if I modify the link to and remove the content of the href
parameter, it works:
echo "<a rel='nofollow' onclick='leido(" . $id_mencion . ")' href='#'><strong>" . $informacion_hilo[0]['asunto'] . "</strong></a>";
I looked and looked back and I do not see where the error is. In fact, the link is correct.
EDIT:
This is the HTML that PHP generates:
<div class="info_hilo">
<a rel="nofollow" onclick="leido(16)" href="foro.php?foro=Xbox%One&subforo=General&hilo=Primer%trailer%oficial%de%Destiny%2&ID=45&pagina=0"><strong>Primer trailer oficial de Destiny 2</strong></a>
<p class="creador">GoitxoCv</p>
</div>