0

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?

Ricardo Mehr
  • 300
  • 2
  • 11
  • *"I get the Alert() with the correct string inside"* - Which alert? Your code has two of them in two very different places. Check your browser's debugging tools. Are there any errors on the console? Is the AJAX network request made? Does it contain the data you expect? What is the server's response? – David Aug 25 '19 at 23:20
  • You are right, I only the the first alert. – Ricardo Mehr Aug 25 '19 at 23:22
  • TypeError: $.ajax is not a function – Ricardo Mehr Aug 25 '19 at 23:26

0 Answers0