Hi everyone,
I will try to explain my problem as simple as I can. I am creating a forum, and I want each post to have the button to like the post. I am using a little form with a label and an input (which is not displayed), there is not submit button. What I want is that if somebody clicks on the label, send information to PHP using AJAX and with that information update the database, just to know the people which have liked the post.
I need to receive some data, and only one is send through the form. To resume, I want to update the information of the page (and the database) withouth refreshing the page. If I am not wrong using PHP the page refreshes, but not with AJAX.
HTML Code:
echo "<div class='busqueda_hilo'>";
echo "<input type='text' name='search_hilo' placeholder='Buscar en este foro...'>";
echo "<label class='fa fa-search' for='search_hilo'></label>";
echo "</div>";
JS:
<script>
function myFunction(respuesta_id) {
let heart;
var id_respuesta = respuesta_id;
if( $('#corazon').prop('checked') ) {
console.log('Seleccionado');
heart = 1;
} else {
console.log('No seleccionado');
heart = 0;
}
console.log(id_respuesta);
console.log(heart);
$.ajax ({
type: 'POST',
url: 'proces_like.php',
data: { "corazon": heart, "id_respuesta":id_respuesta },
success:function(datos){
$("#resultado").html(datos);
}
});
};
</script>
In process.php file where the data is processed:
<?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();
}
echo $hola = isset($_POST['corazon'])? $_POST['corazon'] : 0;
echo $respuesta_id;
?>
In this case, the only variable I am receiving from AJAX is heart, but not the second one. I donāt know how to resolve this problemā¦in the console of the browser I can see that both variables are OK, but one is not sent to PHP.