Welcome Guest, Not a member yet? Register   Sign In
How to call Controller function and pass a variable from jquery
#1

[eluser]maddtechwf[/eluser]
I'm trying to write a small jquery function that will watch for either a button click or drop down selected. I've got that part covered it just the part of calling a CI Controller function and passing a variable to that particular function.

Here is an example of my function I want to call and the url up to the function name but not sure how to address the variable part.

Code:
public function GetPoints($category_id)
    {
        $points = $this->map_model->get_category_points($category_id);

        foreach ( $points as $point)
        {
            $address_temp = '<table><tr><td>'.$coordinate->Address1.'</td></tr><tr><td>'.$coordinate->City.','.$coordinate->State.' '.$coordinate->Zip.'</td></tr></table>';
            $this->gmap->addMarkerByCoords( $coordinate->Long, $coordinate->Lat, $coordinate->Name, $address_temp);
        }

        $this->__EndItem();
    }

URL Example: mysite.com/map/index.php/maps/GetPoints/
#2

[eluser]Pert[/eluser]
You can use JSON

JS:
Code:
$.post(url, post_data, function (result){
   console.log(result);
}, 'json');

Conrtoller:
Code:
function getpoints()
{
   $return = new stdclass;
   $return->myvar = 'value';
   echo json_encode($return);
   die();
}

I call <b>die</b> because if you get any other random stuff appearing after JSON string then jQuery will fail. You can always make it nicer and add proper HTML headers and what not.




Theme © iAndrew 2016 - Forum software by © MyBB