I'm creating a simple poll webapp. I need to increment a row in mysql DB on click.
I read a lot about PHP working with AJAX, but I can't find the issue.
Here is my code:
<form role="form" method="post">
<div class="form-group">
<a id="vote_<?php echo $_SESSION['idanswer']; ?>" href="#" num-rep="<?php echo $_SESSION['idanswer']; ?>" class="vote_btn btn btn-primary" id="#" type="submit">OPTION 1</a>
</div>
</form>
Actually, in my real code, this a WHILE
loop to show all choices : OPTION 1, OPTION 2, OPTION 3. My goal is to increment a value on click.
My JS :
$( document ).ready(function() {
$('.vote_btn').click( function(e){
e.preventDefault();
var idRep = $(this).attr('num-rep');
$.ajax({
type: 'POST',
url: 'ajax_vote.php',
dataType: 'json',
data: {idAns : idAns},
success: function(data){
if(data.response == 'vote_ok'){
$('#vote_'+idAns).addClass('voted');
alert('Well done bro !');
}
else{
alert('Not workinnnnnng');
}
}
});
});
});
And finally, my ajax_vote.php :
<?php
session_start();
include_once("../connect.php");
$vote = $bdd->query("SELECT * FROM poll_answers WHERE id='$id'");
while ($voteactuel = $vote->fetch()) {
$voteactu = $voteactuel['counter']; // get the value from database
$voteactu = $voteactu + 1; // add one
$bdd->query("UPDATE poll_answers SET counter = $voteactu WHERE id = '$_SESSION['idanswer']'"); //update value
}
?>
When I do this, I can see in the Firebug console that the POST is working, with the value. But the response is
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\BPATLive\sondage\ajax_vote.php on line 10
I can't find how to solve the issue :(
I know this is not the good way to do on many things (such as my way to increment), but this is only for an internal use in my company. So issues concerning security or load are not really the point (even if advices would be appreciated ;))