javascript - Dynamically generate table in form using Ajax with symfony -
i working symfony project . want generate table in form when select option in form.
this task.
there name options in form ( eg. john , mia , lia .. etc) when select "john" . want display detail "john" using table. table should locate in form .
there example in link http://www.w3schools.com/php/php_ajax_database.asp
but want using symfony.
what best way this.
please mention example.
using javascript , jquery or ajax symfony
the symfony keyword misleading here because generate table client javascript code. in javascript initiate ajax call post request php/symfony application. 1 returns json array of objects, , ajax complete/done method create table using jquery example. here example:
<html> <head> <script src="https://code.jquery.com/jquery-2.2.0.js"></script> <script> $(document).ready (function () { }) ; function go () { $.post ('/index.php/api/mycall', //$.post ('/php.php', { "param1": "param1", "param2": 2 }) .done (function (ret) { var tbl =$('#mytable') ; tbl.empty () ; $(document.createelement('tr')) .append ('<th>id</th><th>firstname</th><th>lastname</th>') .appendto (tbl) ; $.each (ret, function (index, val) { $(document.createelement('tr')) .append ('<td>' + val.id + '</td><td>' + val.firstname + '</td><td>' + val.lastname + '</td>') .appendto (tbl) ; }) ; }) ; } </script> </head> <body> <input type="button" onclick="go()" /> <div id="mytable"></div> </body> </html>
and somewhere in symphony code handle post query /api/mycall
$results =[ (object)[ "id" => 1, "firstname" => "john", "lastname" => "doe" ], (object)[ "id" => 2, "firstname" => "jane", "lastname" => "doe" ] ] ; return (new jsonresponse ($results, response::http_ok)) ;
hope helps
Comments
Post a Comment