CodeIgniter框架入门教程——第三课 URL及ajax
本文转载自:http://www.softeng.cn/?p=74
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;
}
}
第三课源代码下载地址:
CodeIgniter框架入门教程——第三课 URL及ajax的更多相关文章
- CodeIgniter框架入门教程——第一课 Hello World!
本文转载自:http://www.softeng.cn/?p=45 今天开始,我将在这里连载由我自己编写的<CodeIgniter框架入门教程>,首先,这篇教程的读着应该是有PHP基础的编 ...
- CI(CodeIgniter)框架入门教程——第二课 初始MVC
本文转载自:http://www.softeng.cn/?p=53 今天的主要内容是,使用CodeIgniter框架完整的MVC内容来做一个简单的计算器,通过这个计算器,让大家能够体会到我在第一节课中 ...
- Veins(车载通信仿真框架)入门教程(三)——多跳路由实现指导
Veins(车载通信仿真框架)入门教程(三)——多跳路由实现指导 Veins(车载通信仿真框架)入门教程(三)——多跳路由实现指导 必要的message类实现 从下面开始是在veins/src/vei ...
- Docker入门教程(三)Dockerfile
Docker入门教程(三)Dockerfile [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第三篇,介绍了Dockerfile的语法,DockerOn ...
- Elasticsearch7.X 入门学习第三课笔记----search api学习(URI Search)
原文:Elasticsearch7.X 入门学习第三课笔记----search api学习(URI Search) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出 ...
- WCF入门教程(三)定义服务协定--属性标签
WCF入门教程(三)定义服务协定--属性标签 属性标签,成为定义协议的主要方式.先将最简单的标签进行简单介绍,以了解他们的功能以及使用规则. 服务协定标识,标识哪些接口是服务协定,哪些操作时服务协定的 ...
- SQLite 入门教程(三)好多约束 Constraints(转)
转于: SQLite 入门教程(三)好多约束 Constraints 一.约束 Constraints 在上一篇随笔的结尾,我提到了约束, 但是在那里我把它翻译成了限定符,不太准确,这里先更正一下,应 ...
- 【知识整理】这可能是最好的RxJava 2.x 入门教程(三)
这可能是最好的RxJava 2.x入门教程系列专栏 文章链接: 这可能是最好的RxJava 2.x 入门教程(一) 这可能是最好的RxJava 2.x 入门教程(二) GitHub 代码同步更新:ht ...
- Java - Struts框架教程 Hibernate框架教程 Spring框架入门教程(新版) sping mvc spring boot spring cloud Mybatis
https://www.zhihu.com/question/21142149 http://how2j.cn/k/hibernate/hibernate-tutorial/31.html?tid=6 ...
随机推荐
- Spring MVC - 配置Spring MVC
写在前面的话: 现在开始一段新的学习历程:Spring MVC.还是按照原来的三步走学习模式(what.why.how)进行讲解. 1.Spring MVC是什么(what) Spring MVC属于 ...
- tar, rar, unrar, zip, unzip
tar 打包/解包/压缩/解压缩文件,注意打包和压缩不是一回事,打包相当于捆绑,压缩是在捆绑好后再把里面的空隙挤出以生成更小的文件 $tar [-zjxcvf] filename.tar[.gz... ...
- android Bitmap类方法属性 详细说明
(转:http://blog.csdn.net/ymangu666/article/details/37729109) 1. BitMap类public void recycle()——回收位图占用 ...
- Python搜索目录下指定的文件,并返回绝对路径(包括子目录)
#!/usr/bin/python #coding=UTF-8 #FileName:search.py #文件搜索 import os; import sys; returnList = []; de ...
- PhpSms 稳定可靠的php短信发送库
可能是目前最聪明.优雅的PHP短信发送库了.从此不再为各种原因造成的个别短信发送失败而烦忧! phpsms的任务均衡调度功能由toplan/task-balancer提供. GitHub地址:http ...
- OpenStack 企业私有云的若干需求(9): 云管理平台 CMP
本系列会介绍OpenStack 企业私有云的几个需求: 自动扩展(Auto-scaling)支持 多租户和租户隔离 (multi-tenancy and tenancy isolation) 混合云( ...
- [Notes] Learn Python2.7 From Python Tutorial
I have planed to learn Python for many times. I have started to learn Python for many times . Howeve ...
- CETV面试总结
在通过CETV的网上笔试之后,我收到了面试通知,我纠结片刻,就买了第二天去广州的高铁,虽然感觉自己硬件方面的知识已经快忘完了,但还是想去一下,起码是一种经历.对一个路痴和知识储备不足的我来讲,这一切都 ...
- POJ3211 Washing Clothes[DP 分解 01背包可行性]
Washing Clothes Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9707 Accepted: 3114 ...
- 第18章 图元文件_18.2 增强型图元文件(emf)(2)
18.2.7 增强型图元文件的查看和打印程序 (1)传递EMF到剪贴板,剪贴板类型应为:CF_ENHMETAFILE (2)CopyEnhMetaFile用于复制图元文件 (3)剪贴板中的图元文件会自 ...