Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts

    Update page after Ajax call

    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:
    Code:
    $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.
    Last edited by dan-dan; 03-09-2012 at 11:56 PM.

  2. Users who have thanked dan-dan for this post:

    kwyl (03-11-2012)

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    dan-dan (03-10-2012)

  • #3
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    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?

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by dan-dan View Post
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Solved

    I made this change on index.php. Thanks everyone.

    Code:
    <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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •