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 ...
随机推荐
- PHP 5.3.0以上推荐使用mysqlnd驱动
1. 什么是 mysqlnd 驱动 ? PHP 手册上的描述 : MySQL Native Driver is a replacement for the MySQL Client Library ( ...
- 1、Hadoop的伪分布式部署
伪分布式模式搭建: 1.环境准备 (1)主机名(root用户) # vi /etc/sysconfig/network HOSTNAME=hadoo1 (不要用下划线) (2)创建普通用户cong ...
- 一个有趣的SQL Server 层级汇总数据问题
看SQL Server大V宋大侠的博客文章,发现了一个有趣的sql server层级汇总数据问题. 具体的问题如下: parent_id emp_id emp_nam ...
- android 设置布局为无标题样式
<!-- Application theme. --> <style name="AppTheme" parent="android:Theme.Lig ...
- 客户访问站点将bbs/链接 跳转至forum/链接下的两种方式
显性 302 暂时重定向跳转 server { listen 80 ; server_name localhost; index index.html index.htm index.php; roo ...
- 阿里云+wordpress搭建个人博客网站【小白专用的图文教程】
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- CF723D. Lakes in Berland[DFS floodfill]
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codevs哈希水题
1230 多重hash练习一下,不用也可以 // // main.cpp // codeves1230 // // Created by Candy on 9/29/16. // Copyright ...
- jmeter 函数助手里的P,property的使用
1.函数助手里的 p及property的使用 ${__P(init,2)} , ${__property(init,start,200)} 可以自行定义变量名称,及变量的默认值 P 变量名为init, ...
- Maven系列三Maven内置变量
Maven内置变量说明: ${basedir} 项目根目录(即pom.xml文件所在目录) ${project.build.directory} 构建目录,缺省为target目录 ${project. ...