I update the question:
I'm trying to find a way to create a functional I like button. For this I have thought about creating a small <form>
in which there is an input type checkbox and a label, which is an icon. Visually only the label is seen since the checkbox is hidden.
I can not get the data sent to PHP or javascript when I click on the label, I would prefer javascript and then pass it to PHP.
HTML Code:
<form name='like' id='heart' title='Me gusta el post' action='foro.php?foro=Xbox%20One&subforo=General&hilo=Vamos%dale&ID=1' method='post'>
<input type='checkbox' id='corazon' name='corazon'>
<label class='fa fa-heart' onclick='myFunction($respuesta_id)' for='corazon'></label>";
</form>
PHP:
echo $like = isset($_POST['corazon'])? $_POST['corazon'] : 0;
The value of $like
is always 0.
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>
I have to pass two parameters to PHP, on the one hand the value of heart
and on the other the ID
, which is nothing more than to identify the comment referred to by that "I like".
In PHP I can show the value of heart
but not the ID
, because it is not a value that can be obtained in the same way with the POST method, since it is not an input.