本文轉載自:http://www.softeng.cn/?p=74javascript
example.com/index.php/floder/class/function/id/
example.com/index.php/floder/class/function/id1/id2/id3/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /CI_03/index.php/$1 [L]
function count() { // 使用輸入類接收參數 $num1 = $this->input->post('num1'); $op = $this->input->post('operate'); $num2 = $this->input->post('num2'); if (is_numeric($num1) && is_numeric($num2)) { // 若是兩個數輸入均爲數字,則調用calculate_model模型下的count方法 $result = $this->calculate_model->count($num1, $num2, $op); // 採用文本做爲格式做爲回傳值,因此直接返回結果 echo $result; }else { echo FALSE; } }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>網頁計算器</title> <style type="text/css"> #calculators { margin: 10% auto; width:600px; border:1px solid #000; } </style> <script type="text/javascript"> var xmlhttp = null; function $(id) { return document.getElementById(id); } //建立ajax引擎 function getXMLHttpRequest() { var xmlhttp; try { //Firefox,Opera 8.0+, Safari xmlhttp = new XMLHttpRequest(); }catch (e) { //Internet Explorer try { xmlhttp = new ActiveXObject("Msxml12.XMLHTTP"); }catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }catch (e) { alert("您的瀏覽器不支持AJAX!"); return false; } } } return xmlhttp; } function isubmit() { xmlhttp = getXMLHttpRequest(); //怎麼判斷建立是否成功 if (xmlhttp) { //以post方式發送 var url = "index.php/calculate/count/"; var data = "num1="+$("num1").value+"&operate="+$("operate").value+"&num2="+$("num2").value; //打開請求 xmlhttp.open("post", url, true); //下面這句話是post方式發送時必需要 xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //指定回調函數,指定的函數名必定不要帶括號 xmlhttp.onreadystatechange = deal; //發送請求 xmlhttp.send(data); } } function deal() { //取出從服務器返回的數據 if (xmlhttp.readyState == 4) { //取出值,根據返回信息的格式而定 $("result").value = xmlhttp.responseText; } } </script> </head> <body> <div id="calculators"> <form> <input type="text" name="num1" id="num1" /> <select name="operate" id="operate"> <option value="add">+</option> <option value="sub">-</option> <option value="mul">x</option> <option value="div">÷</option> </select> <input type="text" name="num2" id="num2" />= <input type="text" name="result" id="result" disabled="disabled" /> <input type="button" value="計算" onclick="isubmit()" /> </form> </div> </body> </html>
function count($num1, $num2, $op) { if ($op == "add") { return $num1 + $num2; }else if ($op == "sub") { return $num1 - $num2; }else if ($op == "mul") { return $num1 * $num2; }else if ($op == "div" && $num2 != 0) { return $num1 / 1.0 / $num2; }else { return FALSE; } }
第三課源代碼下載地址: php