...

View Full Version : Update page after Ajax call



dan-dan
03-09-2012, 11:54 PM
OK, I've just been working with kwyl, trying to intergrate a like/dislike script to her site.
There was an original script, and although bugged, it's almost there:

What works
The user clicks the like/dislike icon.
Ajax sends a request to PHP:
Either the like/dislike gets inserted into the database.
OR, if the user has already liked/disliked that post, an alert error gets returned.

What doesn't work
If you click to like/dislike a post, it won't appear (the current count is in brackets beside the icons) unless the page is refreshed.

How would I use this along side Ajax for auto update?
$data = mysql_fetch_object(mysql_query('SELECT `like`,`unlike` FROM posts WHERE id="'.$row['id'].'"'));

It's used here:


$data = mysql_fetch_object(mysql_query('SELECT `like`,`unlike` FROM posts WHERE id="'.$row['id'].'"'));
?>

<br />

<div class="vote">

<a href="javascript:;" onclick="doAction('<?php echo $row['id']; ?>','unlike');">
<input type="image" name="submit" src="images/down.gif" /></a>
(<span id=""><?php echo $data->unlike;?></span>)


<a href="javascript:;" onclick="doAction('<?php echo $row['id']; ?>','like');">
<input type="image" name="submit" src="images/up.gif"></a>
(<span id=""><?php echo $data->like;?></span>)


I hate javascript and this is utterly driving me up the wall.

Any help would be much appreciated.
If you need more informaton/code please ask.

felgall
03-10-2012, 03:14 AM
The PHP you want the Ajax call to run is a stand alone PHP script which echos the value to be returned to the JavaScript. The JavaScript then uses DOM commands to insert the result into the web page.

See http://javascriptexample.net/ajax.php for a series of examples that demonstrate the various alternatives. The XML and JSON examples show what format the echoed information should be in to allow the JavaScript shown in that example to process it.

dan-dan
03-10-2012, 03:26 AM
Thank you for your reply felgall.
I have just taken a look at the site and it means nothing to me. I'm so naive to js, it's embarassing.
If I could understand the principals I may stand a chance to write some code based on that, but I don't.

Could anyone write some code to help, pleeeeease?

felgall
03-10-2012, 04:37 AM
Could anyone write some code to help, pleeeeease?

Each of those pages contains an example Ajax call that is fully functional (there's a "Live Example" link on each page to show you what happens when the script actually runs) - you would just need to create the server side code that produces what you want in the right format and then make the minor modifications to the JavaScript to process your data instead of the example data in those working examples.

You could use one of those as your starting point and then if you can't get it to work you can at least post what code you have in order to ask for further help with whatever part isn't working.

If you want to actually learn how to write JavaScript properly then you could start with the basics section of the site that goes through how to write all the basic JavaScript commands and which basically will provide you with what you need to know to make sense of the more advanced examples.

kwyl
03-10-2012, 06:17 PM
I made this change on index.php. Thanks everyone. :)




<script>
function doAction(postid,type){
$.post('doAjax.php', {postid:postid, type:type}, function(data){
if(isNaN(parseFloat(data))){
alert(data);
}else{
$('#'+postid+'_'+type+'s').text(data);
}
{
window.location.reload() // <---HAD TO ADD THIS LINE
}
});
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum