thinkphpÔõôд¶àÇëÇó
Ëæ×Å»¥ÁªÍøÊÖÒÕµÄÒ»Ö±Éú³¤£¬ÎÒÃÇÔÚ¿ª·¢ web Ó¦ÓÃʱÐèÒªÔ½À´Ô½¶àµØʵÏÖ¶àÇëÇó¹¦Ð§¡£ÔÚÇ°ºó¶ËÊèÉ¢µÄ¿ª·¢Ä£Ê½Ï£¬Ç°¶ËÒ³ÃæÐèÒªÏòºó¶Ë·¢ËͶà´ÎÇëÇóÀ´»ñÈ¡²î±ðµÄÊý¾Ý»òʵÏÖ²î±ðµÄ²Ù×÷¡£ÔÚ php ¿ª·¢ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÒ»¸öÓÅÒìµÄ¿ò¼Ü¡ª¡ªthinkphp À´ÊµÏÖÕâһĿµÄ¡£
±¾ÎĽ«ÏêϸÏÈÈÝÔõÑùʹÓà ThinkPHP ¿ò¼ÜÀ´ÊµÏÖ¶àÇëÇó¹¦Ð§¡£
Ò»¡¢Ê²Ã´ÊǶàÇëÇó£¿
¶àÇëÇóÊÇÖ¸ÔÚÒ»¸ö web Ó¦ÓÃÖУ¬Ò»¸öÒ³Ãæ»òÒ»¸ö¹¦Ð§ÐèÒªÏòºó¶Ë·¢ËͶà´ÎÇëÇó£¬ÒÔ»ñÈ¡²î±ðµÄÊý¾Ý»òʵÏÖ²î±ðµÄ²Ù×÷¡£¶àÇëÇóµÄʵÏÖͨ³£ÐèÒªÒÀÀµ JavaScript ÊÖÒÕºÍ AJAX ÊÖÒÕ¡£
¶þ¡¢ThinkPHP ʵÏÖ¶àÇëÇóÔÀí
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔÚ ThinkPHP ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà AJAX ÊÖÒÕÀ´ÊµÏÖ¶àÇëÇó¹¦Ð§¡£AJAX£¨Asynchronous JavaScript and XML£©ÊÇÒ»ÖÖʹÓà JavaScript ÊÖÒÕÏòЧÀÍÆ÷·¢ËÍÒì²½ÇëÇóºÍÎüÊÕÏìÓ¦µÄÊÖÒÕ¡£Ê¹Óà AJAX ÊÖÒÕ·¢ËÍÇëÇóʱ£¬Web Ò³ÃæÎÞÐèˢУ¬Ò²ÎÞÐèÖØÔØÕû¸öÒ³Ã棬¶øÖ»ÐèÒª¸üÐÂÐèÒª¸üеIJ¿·Ö¡£
ThinkPHP ¿ò¼ÜÌṩÁ˱ã½ÝµÄ AJAX ÇëÇó·½·¨£¬¼´Í¨¹ý AjaxController ÀàµÄ index ÒªÁìÀ´´¦ÀíÒì²½ÇëÇ󣬸ÃÒªÁì·µ»ØÒ»¸ö JSON Êý¾ÝÃûÌõÄÏìÓ¦¡£ÎÒÃÇ¿ÉÒÔÔÚ index ÒªÁìÖоÙÐжàÇëÇóµÄ´¦Àí£¬²¢·µ»Ø¶à¸ö JSON ÃûÌõÄÊý¾Ý¡£
Èý¡¢ThinkPHP ¶àÇëÇóµÄʵÏÖ°ì·¨
ÔÚ ThinkPHP ¿ò¼ÜÖУ¬Ê×ÏÈÐèÒª½¨ÉèÒ»¸ö¿ØÖÆÆ÷Àà¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÀ´½¨ÉèÒ»¸öÃûΪ AjaxController µÄ¿ØÖÆÆ÷£º
php think make:controller AjaxController
µÇ¼ºó¸´ÖÆ
½¨Éè¿ØÖÆÆ÷ºó£¬ÎÒÃÇÐèÒªÔڸÿØÖÆÆ÷ÀàÖÐÌí¼ÓÒ»¸ö index ÒªÁ죬ÓÃÓÚ´¦ÀíÒì²½ÇëÇó¡£ÔÚ¸ÃÒªÁìÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà thinkDb Àà¾ÙÐÐÊý¾Ý¿â²Ù×÷£¬»ñÈ¡ËùÐèµÄÊý¾Ý¡£×îºó½«»ñÈ¡µ½µÄÊý¾ÝÒÔ JSON ÃûÌõÄÐÎʽ·µ»Ø¡£
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄÀý×Ó£¬ÎÒÃÇ»ñÈ¡Ò»¸öѧÉúÁÐ±í£¬Ê¹Óà index ÒªÁì·µ»Ø JSON ÃûÌõÄÊý¾Ý£º
// applicationindexcontrollerAjaxController.php namespace appindexcontroller; use thinkController; use thinkDb; class AjaxController extends Controller { public function index() { // »ñȡѧÉúÁбí $students = Db::table('student')->select(); // ·µ»Ø JSON ÃûÌõÄÊý¾Ý return json($students); } }
µÇ¼ºó¸´ÖÆ
ÔÚÇ°¶ËÒ³ÃæÖУ¬ÎÒÃÇÐèҪʹÓà JavaScript ÊÖÒÕÀ´·¢ËÍÒì²½ÇëÇ󣬴Ӷø»ñÈ¡ºó¶Ë·µ»ØµÄ JSON Êý¾Ý¡£ÔÚ JavaScript ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà XMLHttpRequest ¹¤¾ßÀ´·¢ËÍÒì²½ÇëÇó¡£
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄÀý×Ó£¬ÎÒÃÇÏò AjaxController ·¢ËÍÇëÇ󣬲¢ÔÚ»ñÈ¡Êý¾Ýºó½«Êý¾ÝÏÔʾÔÚÒ³ÃæÖУº
// index.html <script> var xhr = new XMLHttpRequest(); xhr.open('GET', '/index/ajax/index', true); // ·¢ËÍÒì²½ÇëÇó xhr.onreadystatechange = function() { if(xhr.readyState === 4 && xhr.status === 200) { var data = JSON.parse(xhr.responseText); // »ñÈ¡ºǫ́·µ»ØµÄ JSON Êý¾Ý // ½«Êý¾ÝÏÔʾÔÚÒ³ÃæÉÏ for(var i = 0; i < data.length; i++) { var tr = document.createElement('tr'); var td1 = document.createElement('td'); var td2 = document.createElement('td'); td1.innerHTML = data[i].name; td2.innerHTML = data[i].age; tr.appendChild(td1); tr.appendChild(td2); document.getElementById('studentList').appendChild(tr); } } } xhr.send(); </script> <table id="studentList"> <thead> <tr> <th>ÐÕÃû</th> <th>ÄêËê</th> </tr> </thead> <tbody> </tbody> </table>
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÏò AjaxController ·¢ËÍ GET ÇëÇ󣬲¢¼àÌý readyState ºÍ status ÊÂÎñ¡£µ± readyState ÊôÐÔÖµ±äΪ 4 ʱ£¬ÌåÏÖÇëÇóÒѾÍê³É£¬status ÊôÐÔֵΪ 200 ÌåÏÖÇëÇóÀֳɡ£´Ëʱ¿ÉÒÔͨ¹ý responseText ÊôÐÔ»ñÈ¡ºǫ́·µ»ØµÄ JSON Êý¾Ý£¬È»ºó½«Êý¾Ý¶¯Ì¬µØÏÔʾÔÚÒ³ÃæÉÏ¡£
ͨ¹ýÒÔÉÏÈý¸ö°ì·¨£¬ÎÒÃǾͿÉÒÔÔÚ ThinkPHP ¿ò¼ÜÖÐʵÏÖ¶àÇëÇóµÄ¹¦Ð§¡£ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃÇ¿ÉÒÔƾ֤ÏêϸÐèÇó£¬ÔÚ index ÒªÁìÖоÙÐжàÇëÇóµÄ´¦Àí£¬²¢½«¶à¸ö JSON ÃûÌõÄÊý¾Ý·µ»ØÇ°¶ËÒ³Ãæ¡£
ËÄ¡¢×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùÔÚ ThinkPHP ¿ò¼ÜÖÐʵÏÖ¶àÇëÇó¹¦Ð§¡£Í¨¹ýʹÓà AJAX ÊÖÒÕ£¬ÎÒÃÇ¿ÉÒÔÔÚÒ³ÃæÎÞÐèÖØмÓÔصÄÇéÐÎÏ£¬ÊµÏÖ¶à´ÎÏòºó¶Ë·¢ËÍÇëÇó²¢»ñÈ¡ÏìÓ¦µÄ¹¦Ð§¡£ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃÇ»¹¿ÉÒÔƾ֤ÏêϸÐèÇ󣬽øÒ»²½ÓÅ»¯¶àÇëÇóµÄʵÏÖ·½·¨£¬ÈçʹÓøßЧµÄÊý¾Ý¿âÅÌÎÊ·½·¨¡¢ºÏÀíµØ·Ö×é·¢ËÍÇëÇóµÈ¡£
ÔÚÏÖʵ¿ª·¢ÖУ¬¶àÇëÇóÊǷǾ³£¼ûµÄÐèÇó¡£ÕÆÎÕÁË ThinkPHP ¿ò¼ÜÖеĶàÇëÇóʵÏÖÒªÁ죬¿ÉÒÔ×ÊÖúÎÒÃÇÔ½·¢¸ßЧµØÍê³É web Ó¦ÓõĿª·¢¡£
ÒÔÉϾÍÊÇthinkphpÔõôд¶àÇëÇóµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡