-2

This is my first time asking a question here.

So I have this PHP script

if($_POST['vote']){
    $id = $_POST['id'];
    $vote = $_POST['vote'];
    $sql = "INSERT INTO question_votes ('question_id', 'user_id', 'vote', 'date') VALUES( '$id', '$log', '$vote', now())";
    $query = $pdo->prepare($sql);
    $query->execute();
}

Using this code

<script>
        $function((){
            $('.vote').click(function(){
                    var id = $(this).attr("id"); // cache $this
                    var vote = $(this).data("action");

                    $.ajax({
                        url: 'ajaxvote.php',
                        type: 'POST',
                        data: {id:id, vote:vote}

                    });
                });
        });
</script>

When I run the code it won't insert the data in the database. By the way i'm just a student and this is my first time in website development.

Ashok Chandrapal
  • 1,020
  • 7
  • 27

1 Answers1

0

Few improvements and fixes.

In if check for isset($_POST['vote']) or !empty($_POST['vote']) to avoid run time errors.

For JS:

        $(function() {
        $(document).on('click','.vote',function(){
                var id = $(this).attr("id"); // cache $this
                var vote = $(this).data("action");

                $.ajax({
                    url: 'ajaxvote.php',
                    type: 'POST',
                    data: {"id":id, "vote":vote}

                });
            });
        });

Hope this helps.

Kinshuk Lahiri
  • 1,468
  • 10
  • 23