This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
php_jquery_ajax [2016/05/23 11:25] mh created |
php_jquery_ajax [2016/05/23 11:27] (current) mh |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | <code> | ||
| <html> | <html> | ||
| <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script> | <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script> | ||
| Line 48: | Line 49: | ||
| </div> | </div> | ||
| <html> | <html> | ||
| + | </code> | ||
| + | |||
| + | Oder die Trennung von Präsentation und Datengenerierung | ||
| + | |||
| + | php, liefert JSON | ||
| + | list-persons-ajax.php | ||
| + | <code> | ||
| + | <?php | ||
| + | require('db.php'); | ||
| + | $dbh = get_db(); | ||
| + | $person_list = | ||
| + | $dbh->query('select fullname, birthdate, country_code from person') | ||
| + | ->fetch_all(MYSQLI_ASSOC); | ||
| + | | ||
| + | header('Content-type: application/json'); | ||
| + | echo json_encode($person_list); | ||
| + | </code> | ||
| + | |||
| + | welches dann mit jquery get abgefragt und danach formatiert angezeigt wird | ||
| + | <code> | ||
| + | <html> | ||
| + | <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script> | ||
| + | |||
| + | <div id='liclass'> | ||
| + | </div> | ||
| + | <script> | ||
| + | $(document).ready(function() { | ||
| + | var ret = $.get("/kurs/list-persons-ajax.php") | ||
| + | .done(function( person_list) { | ||
| + | $.each( person_list, function( key, person ) { | ||
| + | $('#liclass').append("<li>"+ person['fullname'] +"</li" ); | ||
| + | }); | ||
| + | }); | ||
| + | }); | ||
| + | </script> | ||
| + | </html> | ||
| + | </code> | ||
| + | |||