Reputation: 49
I built a vote system with ajax and php, and I send data to php page for saved data in db. I tried to send data with ajax post and php. My problem is the data is not send to the page. My js code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.ajaxSetup({
url: 'vote.php',
type: 'POST',
cache: 'false'
});
$('.vote').click(function(){
var self = $(this);
var action = self.data('action');
var parent = self.parent().parent();
var imgid = <?=$array['id'];?>;
if (!parent.hasClass('.disabled')) {
if (action == 'up') {
parent.find('#image-like').addClass('disabled_up');
$.ajax({data: {'imgid' : imgid, 'action' : 'up'}});
}
else if (action == 'down'){
parent.find('#image-dislike').addClass('disabled_down');
$.ajax({data: {'imgid' : imgid, 'action' : 'down'}});
};
parent.addClass('.disabled');
};
});
});
</script>
and my html code:
<a href="javascript:void(0);" id="image-like" data-action="up" class="vote"></a>
<a href="javascript:void(0);" id="image-dislike" data-action="down" class="vote"></a>
Upvotes: 1
Views: 1378
Reputation: 139
Use post method. This is not the correct code, but it's an idea, always works for me.
$('.vote').click(function(){
//Your vars
var data='voteup';
//Your actions... ddClass/removeClass...
$.post('vote.php',data,function(data){
//On your vote.php use "if($data=='voteup') else ;"
//And show message here...
alert(data);
});
return false;
});
example of vote.php
<?php
$data=$_POST['data'];
if($data=='voteup')
echo "You voted up!";
else echo "You voted down!";
?>
It's just an idea (:
Upvotes: 1
Reputation: 1783
Try using .post() function, you can set a callback when your action is done
jQuery.post(URL_TO_REACH, {ID_VALUE1 : 'my value' , ID_VALUE2 : 'my second value' })
.done(function( data_ajax ) { // data_ajax : Your return value from your php script
alert( data_ajax );
})
});
Hope this will help you
Official documentation : http://api.jquery.com/jQuery.post/
Upvotes: 0
Reputation: 74738
You can try changing this:
if (!parent.hasClass('.disabled')) {
to this:
if (!parent.hasClass('disabled')) {
For $.ajaxSetup()
Description: Set default values for future Ajax requests. Its use is not recommended.
Upvotes: 0