Sim Posted January 30, 2011 Share Posted January 30, 2011 this is just the basic html of my code, my js file is included. my php file works.. i echo an alert and it says you voted. that works when visiting the php page by itself. the problem is the page refreshes... <div class='up'>[url=""]1 Good[/url]</div> <div class='down'>[url=""]2 Bad[/url]</div> <div class='up'>[url=""]1 Good[/url]</div> <div class='down'>[url=""]2 Bad[/url]</div> my JS stuff $(document).ready(function () { $(".vote").click(function() { var id = $(this).attr("id"); var name = $(this).attr("name"); var dataString = 'id='+ id ; var parent = $(this); if(name=='up') { $.ajax( { type: "POST", url: "ajax/voteGood.php", data: dataString, cache: false, success: function(html) { parent.html(html); } }); } else { $.ajax( { type: "POST", url: "ajax/voteBad.php", data: dataString, cache: false, success: function(html) { parent.html(html); } }); } }) }) Quote Link to comment Share on other sites More sharing options...
a_bertrand Posted January 30, 2011 Share Posted January 30, 2011 Why do you think it "refreshes"? Could it not be that simply you replace ALL the HTML? (something btw which is a non sense normally). Quote Link to comment Share on other sites More sharing options...
Spudinski Posted January 30, 2011 Share Posted January 30, 2011 Note that you need to include the jQuery API to use jquery syntax. Also, I can see that you have four identical elements using your "vote" class. This may cause problems with the script, so try renaming each individually. Though, I actually agree with a_bertrand, there's no reason for this script to replace "all" data. As I'm looking you simply want to update a vote count? Try using a container for that. Quote Link to comment Share on other sites More sharing options...
Sim Posted January 30, 2011 Author Share Posted January 30, 2011 Why do you think it "refreshes"? Could it not be that simply you replace ALL the HTML? (something btw which is a non sense normally). I'm not replacing all the html... My vote.php page's just updates the vote if they havn't allready voted and if they logged in. @Spudinski, jQuery is included.. I figured I wouldn't have to show that in the post. If I rename all classe's to vote1 vote2 vote3, I don't think id be able to use this $(".vote").click(function() back to the problem at hand. If I replace success: function(html) { parent.html(html); } with success: function(html) { alert("boo"); } The alert never shows, but it counts the vote.... ;\ Quote Link to comment Share on other sites More sharing options...
Danny696 Posted January 30, 2011 Share Posted January 30, 2011 Try something like this: [js]$(document).ready(function () { $(".vote").click(function() { var id = $(this).attr("id"); var name = $(this).attr("name"); var dataString = 'id='+ id ; var parent = $(this); if(name=='up') { $.post('ajax/voteGood.php?' + dataString, function(data) { $('.vote').html(data); }); } else { $.post('ajax/voteBad.php?' + dataString, function(data) { $('.vote').html(data); }); } }) })[/js] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.