ArizonaJohn Posted May 11, 2009 Share Posted May 11, 2009 Hello, I am trying to use AJAX from a dynamically-generated HTML table in PHP. The AJAX is being triggered, but it returns the "Failed!" message below. I'm not sure why. Does anyone see why it's not working? Thanks in advance, John Here is the table: print "<td>".'<a href="http://'.$row['site'].'" class="links2">'.$row['site'].'</a>'."</td>"; print "<td class='votes'>".'<span class="votes_count" id="votes_count'.$row['id'].'">'.number_format($effective_vote).'</span>'."</td>"; print "<td class='ballot'>".'<span class="button" id="button'.$row['id'].'">'.'<a href="javascript:;" class="cell1" id="'.$row['id'].'">'.Vote.'</a>'.'</span>'."</td>"; Here is the Javascript: <script type='text/javascript' src='jquery.pack.js'></script> <script type='text/javascript'> $(function(){ $("a.cell1").click(function(){ //get the id the_id = $(this).attr('id'); // show the spinner $(this).parent().html("<img src='images/spinner.gif'/>"); //fadeout the vote-count $("span#votes_count"+the_id).fadeOut("fast"); //the main ajax request $.ajax({ type: "POST", data: "action=cell1&id="+$(this).attr("id"), url: "votes11.php", success: function(msg) { $("span#votes_count"+the_id).html(msg); //fadein the vote count $("span#votes_count"+the_id).fadeIn(); //remove the spinner $("span#button"+the_id).remove(); } }); }); $("a.vote_down").click(function(){ //get the id the_id = $(this).attr('id'); // show the spinner $(this).parent().html("<img src='images/spinner.gif'/>"); //the main ajax request $.ajax({ type: "POST", data: "action=vote_down&id="+$(this).attr("id"), url: "votes11.php", success: function(msg) { $("span#votes_count"+the_id).fadeOut(); $("span#votes_count"+the_id).html(msg); $("span#votes_count"+the_id).fadeIn(); $("span#button"+the_id).remove(); } }); }); }); </script> Then, in a separate file called "votes11.php", I have this: <?php mysql_connect("mysqlv3", "username", "password") or die(mysql_error()); mysql_select_db("sand2") or die(mysql_error()); function getAllVotes($id) { /** Returns an array whose first element is votes_up and the second one is votes_down **/ $votes = array(); $q = "SELECT * FROM santafe WHERE id = $id"; $r = mysql_query($q); if(mysql_num_rows($r)==1)//id found in the table { $row = mysql_fetch_assoc($r); $votes[0] = $row['votes_up']; $votes[1] = $row['votes_down']; } return $votes; } function getEffectiveVotes($id) { /** Returns an integer **/ $votes = getAllVotes($id); $effectiveVote = $votes[0] - $votes[1]; return $effectiveVote; } $id = $_POST['id']; $action = $_POST['action']; //get the current votes $cur_votes = getAllVotes($id); //ok, now update the votes if($action=='vote_up') //voting up { $votes_up = $cur_votes[0]+1; $q = "UPDATE santafe SET votes_up = $votes_up WHERE id = $id"; } elseif($action=='vote_down') //voting down { $votes_down = $cur_votes[1]+1; $q = "UPDATE santafe SET votes_down = $votes_down WHERE id = $id"; } $r = mysql_query($q); if($r) //voting done { $effectiveVote = getEffectiveVotes($id); echo $effectiveVote; } elseif(!$r) //voting failed { echo "Failed!"; } ?> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 data: "action=cell1&id="+$(this).attr("id"), action should be vote_up. Quote Link to comment Share on other sites More sharing options...
ArizonaJohn Posted May 12, 2009 Author Share Posted May 12, 2009 Ken2k7, Thanks, it works! I'm impressed with your ability to decipher code. I really appreciate it. 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.