I have a button to send data to a database, but it doesnt work. Here is my code.
Voto.php Tested and working.
<?
include('conexion.php');
if(!empty($_GET['voto'])){
$id = mysqli_real_escape_string($conexion, $_GET['id']);
$voto = mysqli_real_escape_string($conexion, $_GET['voto']);
if($voto === 'up'){
$votos = 'votos+1';
}elseif($voto === 'down'){
$votos = 'votos-1';
}else{
echo 'error';
}
$sql = "UPDATE definiciones SET votos=$votos WHERE id=$id";
$result = $conexion->query($sql);
}
?>
HTML
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<button type="button" class="btn btn-success voto" id="140" voto="up">Bien</button>
Jquery 3 most likely causing problems
<script type="text/javascript">
$(function() {
$(".voto").click(function () {
var id = $(this).attr("id");
var value = $(this).attr("voto");
var dataString = 'id=' + id + '&voto=' + value;
alert("string: " + dataString);
$.ajax({
type: "GET",
url: "https://url.com/voto.php",
data: dataString,
cache: false,
success: function (result) {
alert("voto grabado");
}
});
return false;
});
});
</script>
I tested the php park by manually input data in the URL, no problems there. When pressing the button I get the first Alert() with the correct string inside, but the .ajax() part seems not to work as the data is not saved in the DB. What can be the cause?