Welcome Guest, Not a member yet? Register   Sign In
Calling php controlller script from javascript file
#11

[eluser]Computerzworld[/eluser]
give me simple example or syntax please. suppose i have one function getData() in Event controller and i am in currently in js file which is located in system/jscripts/ajax.js .so how can i call getData() function which is in Event controller class.

Thanks for your support.
#12

[eluser]TheFuzzy0ne[/eluser]
Rather than try to guess what you want and do it for you, we would like to see what you have so far, and then we may be able to help you better.
#13

[eluser]Computerzworld[/eluser]
In javascript i have follwoing function

function getCities(stateid,userId)
{
http.open('get', 'Event.php?action=getRecord&stateid;=' + stateid + '&userId;=' + userId);
http.onreadystatechange = handleCities;
http.send(null);
}

in above funtion i have to call one controller function getRecord which contain some argument like stateid and userId

so how can i call this function ???
#14

[eluser]wiredesignz[/eluser]
CodeIgniter does not allow query_strings by default, you must either enable query_strings in the config.php file or change the URL to match the CI segmented URL.
#15

[eluser]TheFuzzy0ne[/eluser]
So:

Code:
http.open(’get’, ‘index.php/Event.php/getRecord/’ + stateid + ‘/’ + userId);

Would work better, but you will need to change your controller so that it expects those parameters.
#16

[eluser]Computerzworld[/eluser]
Thanks

What changes i have to made in controller class? can you give the syntax .Thanks for your support.
#17

[eluser]TheFuzzy0ne[/eluser]
Sorry, I got that slightly wrong. It should be:
Code:
http.open('get', 'index.php/event/getRecord/' + stateid + '/' + userId);

So you controller would be something like this:

event.php
Code:
class Event extends Controller {

    function index()
    {
        // ...
    }

    function getRecord()
    {
        // Load the URI library
        $this->load->library('uri');
        
        // Set the vars.
        $state_id = $this->uri->segment(0);
        $user_id  = $this->uri->segment(1);
        
        // Do stuff here.
    }
}

I'm not sure if I got the segments right, but hopefully you get the point.
#18

[eluser]wiredesignz[/eluser]
Why use segments?

function getRecord($state_id, $user_id) is how CI is designed to run.
#19

[eluser]TheFuzzy0ne[/eluser]
IMHO opinion, you're less likely to get a notice for not passing in the right number of parameters.
#20

[eluser]wiredesignz[/eluser]
Use default values to avoid those errors my friend.

function getRecord($state_id = '', $user_id = '');




Theme © iAndrew 2016 - Forum software by © MyBB