Welcome Guest, Not a member yet? Register   Sign In
Ajax Toggle Button
#1

[eluser]bennyhill[/eluser]
I have a data table of event attendees. I would like to place a graphical toggle button next to each name that says either "yes" or "no". When the button is pressed I want to make an ajax call to update a database record. Anyone know how to do this? I am mainly confused on the AJAX (javascript) calling a controller function part. I am willing to use JQuery.

Thanks
#2

[eluser]markup2go[/eluser]
It's very easy to handle ajax with jQuery:

Code:
$.ajax({
    type: "POST",
    data: "data=" + data_variable,
    url: "<?=base_url()?>controller/method",
    success: function(description){
        $("#result").append(description);
    }
});
#3

[eluser]bennyhill[/eluser]
How would I call this from a click event and pass an argument to it?
#4

[eluser]mattpointblank[/eluser]
It might be worth you reading up on basic javascript rather than copy/pasting posts from here.

Code:
function doSomething(argument)
   $.ajax({
       type: "POST",
       data: "test=" + argument,
       url: "<?=base_url()?>controller/method",
       success: function(description){
           $("#result").append(description);
       }
   });
}

<a href="#">click me</a>
#5

[eluser]bennyhill[/eluser]
Yeah prolly. Thanks. Think you forgot the click handler in the anchor.
#6

[eluser]mattpointblank[/eluser]
Ah, I put it in but it looks like it got stripped from the forum code. Should be easy enough to add, and for bonus points you can use jQuery's bind() function to add it on load, removing the need for onclick in the html.




Theme © iAndrew 2016 - Forum software by © MyBB