Wednesday 15 August 2012

Laravel passing data using ajax to controller -



Laravel passing data using ajax to controller -

how pass id ajax phone call testcontroller getajax() function? when phone call url testurl?id=1

route::get('testurl', 'testcontroller@getajax'); <script> $(function(){ $('#button').click(function() { $.ajax({ url: 'testurl', type: 'get', data: { id: 1 }, success: function(response) { $('#something').html(response); } }); }); }); </script>

testcontroller.php

public function getajax() { $id = $_post['id']; $test = new testmodel(); $result = $test->getdata($id); foreach($result $row) { $html = '<tr> <td>' . $row->name . '</td>' . '<td>' . $row->address . '</td>' . '<td>' . $row->age . '</td>' . '</tr>'; } homecoming $html; }

your ajax's method in controller utilize $_post value. problem.

you can

$id = $_get['id'];

but in laravel, have pretty method this. it's here. not need worry http verb used request, input accessed in same way verbs.

$id = input::get("id");

if want, can filter request type command exception. docs here

determine if request using ajax

if (request::ajax()) { // }

ajax laravel routes

No comments:

Post a Comment