Jump to content
MakeWebGames

stupid ajax will not work =)


Recommended Posts

Posted

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);
			} 
		});
	 }
})
})
Posted

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.

Posted
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.... ;\

Posted

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]

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.   Paste as plain text instead

  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...