• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mootools ajax

#1
[eluser]elmystica[/eluser]
I'm transforming a classic php/mysql-app to CI

It works out good, so far.
Now I want to make some AJAX-calls from the front-end to the dbase.

The old way:
- A javascript event-handler triggers a JS confirm_delete(id) function.
- If confirmed, it calls a JS do_delete(id) function.
- That function makes (thanks to the Mootools framework) an ajax call to a php-file called do_functions.php.
- The php-file returns a value which to the do_delete() functions' callback reciever.
- the callback function then executes whatever has to happen or display ..


How do apply this into CI?
The javascript would be the "easy" part, but how do you make the call to the php-file? Where should that file be? A controller?

Thanks in advance

#2
[eluser]dmitrybelyakov[/eluser]
That is correct. You can do it in your controller - just instruct Mootols to send the request to controller/method and then it will do all the serverside-work. This method can then send a response that get back to mootools.

It may be usefull to find out if a methos was called with ajax or if it was a regular browser call. You may use this code:
Code:
//If request was for module was mady with AJAX
        //don't call View files later on
        $ajax=$this->input->server('HTTP_X_REQUESTED_WITH');
        if($ajax=='XMLHttpRequest') $ajax=true;

#3
[eluser]elmystica[/eluser]
Thanks, very usefull input!
Should the method be situated within the current controller? Or can it be in another controller?

#4
[eluser]elmystica[/eluser]
I'm working on it now.

In my view, this is the javascript that's being called:
Code:
[removed]
        
        function delete_image(id)
            {
                   new Ajax('<?php echo site_url();?>/fnc/test',{postBody:'answerme=ok', onComplete: showResponse, update:'container'}).request();
                return false;
            }
        
        function showResponse(tt)
        {
            alert(tt);
        }

            function confirmDelete(id) {
              if (confirm("Bent u zeker dat u deze foto wenst te verwijderen?")) {
                delete_image(id);
              }
            }
        
    [removed]

And my controller fnc.php looks like this:
Code:
<?php
class Fnc extends Controller{
    
    function Fnc()
    {
        parent::Controller();
    }
    
    function test($item)
    {
        echo $item;
    }
}
?>

However, it doesn't work ...
Where am I thinking wrong?

#5
[eluser]dmitrybelyakov[/eluser]
Can't tell you exactly - but try browsing to /fnc/test - is this the response you whant to get in mootools? Anyway it's better to debug such things with Firebug.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.