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 ...
随机推荐
- .NET领域驱动设计—看DDD是如何运用设计模式颠覆传统架构
阅读目录: 1.开篇介绍 2.简单了解缘由(本文的前期事宜) 3.DomainModel扩展性(运用设计模式设计模型变化点) 3.1.模型扩展性 3.2.设计模式的使用(苦心专研的设计模式.设计思想可 ...
- srping MVC 工程简单搭建
Spring版本:3.2.2.RELEASE 第一步:导入必须的jar包 spring-beans.jar spring-context.jar spring-core.jar spring-expr ...
- Redisd VS Memcached
Redis也常常被当作 Memcached的挑战者被提到桌面上来.关于Redis与Memcached的比较更是比比皆是.然而,Redis真的在功能.性能以及内存使用效率上都超越了Memcached吗? ...
- 六、Android学习第五天——Handler的使用(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 六.Android学习第五天——Handler的使用 注意:有很多功能是不 ...
- Docker+OpenvSwitch搭建VxLAN实验环境
一.概述 1.环境:我这里是2台linux机器(host1和host2),发行版是kali2.0, ...
- System V IPC(1)-消息队列
一.概述 System V三种IPC:消息队列,信号量,共享内存.这三种IPC最先出现在AT&am ...
- 二:shell之bash变量
1.变量的分类: 用户自定义变量: 变量自定义 默认存储是字符串环境变量: 这种变量中主要保存的是和系统操作环境相关的数据.变量可以自定义,但是对系统生效的环境变量名和变 ...
- Docker on Microsoft Azure
Docker蓬勃发展,如日中天.微软自然也不甘落后,且不说即将发布的.支持Docker技术的Windows Nano Server和Windows Server 2016.我们来看看在Microsof ...
- SQL--实现分页查询
在查询数据中,对于某些数据量过大,为了减少页面上单页的加载时间,我们常常会选择分页查询,分页查询有很多方法,下面主要介绍两种分页方法. 一. 通过主键来实现分页: 1.数据库背景. P ...
- Android可移动控件
可移动控件: 效果图: 获取屏幕的宽高: DisplayMetrics dm = getResources().getDisplayMetrics(); screenWidth = dm.widthP ...