Welcome Guest, Not a member yet? Register   Sign In
Using controller actions using jQuery AJAX?
#8

[eluser]xwero[/eluser]
TheFuzzy0ne showed you the right way but maybe it is a bit too vague.
Code:
// do the bookmarking
if (is_ajax())
{
    // return the output of the bookmarking
}
else
{
    // redirect
}
The base of the method stays the same but the action after the manipulation changes. For the ajax function you only need to output status of the bookmark to alter the state of the bookmark link while in php you have to reload the whole page to alter the state of the bookmark link.
The code part in php that makes a difference in the state needs to be ported to javascript for ajax.
Code:
// view
foreach(members as member)
{
    echo '<a href="bookmark/member/32">'
    echo ($member['is_bookmarked'])? '<img src="bookmarked.png">':'<img src="not_bookmarked.png">';
    echo '</a>'
}
// javascript
$('a').click(function(){
var href = $(this).attr('href');
$.ajax({
   type: "GET",
   url: href,
   success: function(msg){ // i assume plain text output
     if(msg == 'true')
     {
        $('img',this).attr('src','bookmarked.png');
     }
     else
     {
       $('img',this).attr('src','not_bookmarked.png');
     }
   }
});
return false; // prevents link from executing
});


Messages In This Thread
Using controller actions using jQuery AJAX? - by El Forum - 02-09-2009, 06:39 AM
Using controller actions using jQuery AJAX? - by El Forum - 02-09-2009, 06:51 AM
Using controller actions using jQuery AJAX? - by El Forum - 02-09-2009, 07:04 AM
Using controller actions using jQuery AJAX? - by El Forum - 02-09-2009, 07:08 AM
Using controller actions using jQuery AJAX? - by El Forum - 02-09-2009, 07:13 AM
Using controller actions using jQuery AJAX? - by El Forum - 02-09-2009, 07:26 AM
Using controller actions using jQuery AJAX? - by El Forum - 02-09-2009, 07:28 AM
Using controller actions using jQuery AJAX? - by El Forum - 02-09-2009, 07:32 AM
Using controller actions using jQuery AJAX? - by El Forum - 02-09-2009, 07:37 AM



Theme © iAndrew 2016 - Forum software by © MyBB