Welcome Guest, Not a member yet? Register   Sign In
Calling php script that interact with CI with jquery ajax ?
#1

[eluser]Rushino[/eluser]
Hello,

It is possible to call a php script using jquery ajax that interact with our application ? If yes, how i can do this ?

Thanks!
#2

[eluser]boltsabre[/eluser]
check out the official jquery documentation, you'll want to look at post, get and ajax methods, all three allow you to interact with php scripts, but without any more details from you I don't know what else to say...

I spoke too soon... check out the video tutorials on this site, there are several about how to use AJAX with CI.
#3

[eluser]Rushino[/eluser]
Well i typically know how to use AJAX with jQuery. The problem i have is how to make the ajax scripts "to work with" the CI instance.
#4

[eluser]cideveloper[/eluser]
In the most basic situation

Controller
Code:
class Test extends CI_Controller {

    function __construct(){
        //Contructor function
        parent::__construct();
    }

    function index(){
        //Default Controller Loaded
        $data = array (
            'page' => 'ci188744'
        );
        $this->load->view('testing/ci188744',$data);
    }

    function ci188744_ajax() {
        $data = array (
            'name' => 'cideveloper',
            'site' => 'http://www.cideveloper.com',
            'email' => '[email protected]',
            'result' => FALSE
        );

        if($this->input->is_ajax_request()) {
            echo json_encode($data);
        } else {
            echo '<pre>' . chr(10);
            print_r($data);
            echo '</pre>' . chr(10);
        }
    }

}

View
Code:
<!DOCTYPE HTML>
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;&lt;/title>
    &lt;meta charset="UTF-8"&gt;
&lt;/head&gt;
&lt;body&gt;
    <div id="page">
        <div id="request"><a href="#" id="ci188744_request">Click Here</a></div>
        <div id="result"></div>
    </div>
    [removed][removed]
    [removed][removed]
&lt;/body&gt;
&lt;/html&gt;

Javascript
Code:
$(function() {

    $("#ci188744_request").live('click', function(e) {

        e.preventDefault();

        $.get('ci188744_ajax', function(data) {
            if (data.result)
            {
                $('#result').html(data.name);
            } else {
                $('#result').html('Why U no Work?');
            }
        },'json');        

    });

});
#5

[eluser]Rushino[/eluser]
it seem i can't get this part to work...

Code:
$.get('ci188744_ajax', function(data) {
            if (data.result)
            {
                $('#result').html(data.name);
            } else {
                $('#result').html('Why U no Work?');
            }
        },'json');


it doesnt want to call the method from the controller (look like i receive no data), any idea ?
#6

[eluser]Rushino[/eluser]
nvm.. got it to work! thanks!
#7

[eluser]Rushino[/eluser]
umm.. bah finally it doesnt work that well lol it only show "Why U no work ?". Any ideas ?
#8

[eluser]cideveloper[/eluser]
Do you have firebug installed? You can use that to see what url is being called by the ajax call.

Check out this video

It will always say "Why U no work ?" data["result"] is set false
#9

[eluser]toopay[/eluser]
[quote author="Rushino" date="1305138762"]Well i typically know how to use AJAX with jQuery. The problem i have is how to make the ajax scripts "to work with" the CI instance.[/quote]

Which part of CI instance you pointed on?
#10

[eluser]Rushino[/eluser]
The script called by the ajax function but well simce it can point out to a controller method that doesnt mater so that fixed my problem thanks a lots.




Theme © iAndrew 2016 - Forum software by © MyBB