Jump to content

AJAX not functioning


ArizonaJohn

Recommended Posts

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!";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.