Hi everyone,
I need to use AJAX to update the database when it is clicked on an <a>
element, but is not working…If I delete the href
content then it works.
HTML code for that issue:
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>";
JS code:
<script>
function leido(foro) {
var foro = foro;
console.log(foro);
$.ajax ({
type: 'POST',
url: 'process_leido.php',
data: { "id_respuesta":id_respuesta }
});
};
</script>
And finally the code is inside the process_leido.php file:
<?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));
?>
I don’t know how to solve this problem. The href content is ok, so I don’t know if I am missing something.