js传值给php

17 篇文章 0 订阅

今天心血来潮做个了简单的计算器,用到的技术有html、js、ajax、php。

输入页面写好之后,用js进行按键及显示的操作,本来想以参数的形式把要计算的表达式传给php进行处理,可是传递过去的表达式是string类型的,php无法直接进行运算。所以,用js把所有的数字和运算符挨个放到数组中进行传值,这样就用到了序列化了。

index.php

<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>计算器</title>
<style type="text/css">
input{width:22px;height:22px;}
</style>
<script type="text/javascript"><!--
var str = '';
var num = '';
var varArr = new Array();
var index = 0;
function operate(id){
var objNum = document.getElementById(id);
var objOpe = document.getElementById("show");
val = objNum.value;


if((val>=0 && val<10) || (val=='.' )){
num = num + val;
objOpe.value = num;
}else{
//alert('转换成数字='+parseInt(num , 10));
objOpe.value = val;
str = str + num;
varArr[index] = num; //把在计算其中输入的数字放到数组中
index = index+1; //每当调用一次数学运算符代表前面一个数字输入完毕
alert("varArr[index] == "+varArr[index-1] + "   &nbsp;&nbsp;num="+num);
num = '';
str = str + val;
varArr[index] = val; //把在计算其中输入的运算符放到数组中
index = index+1;
//str = num + val;
}//end if()

//num = num + val;
}//end func operate()


function require(){
varArr[index] = num; //最后输入的数字放到数组中去
 
// for(i=0;i<=index;i++){
// alert("varArr["+i+"]=="+varArr[i]);
// }
var exprssion = str + num;
jsonVar = JSON.stringify(varArr);
// window.location.href='calculator.php?jsonVar='+jsonVar;
// urls = document.getElementById('urlVar');
// urls.value = str+num;


var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}


xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("show").value=xmlhttp.responseText;
}
}
xmlhttp.open("GET","calculator.php?jsonVar="+jsonVar,true);
xmlhttp.send();
}//end func require()


</script>
</head>
<body>
<form action="calculator.php" method="get"></form>
<input type="hidden" name="urlVar" id="urlVar" value=''/>
<input type="text" name="show" id="show" value="0.0" style="width:130px;height:35px;"/>
<table>
<tr>
<td><input type="button" name="one" id="one" value='1' οnclick="operate('one')"></td>
<td><input type="button" name="two" id="two" value='2' οnclick="operate('two')"></td>
<td><input type="button" name="three" id="three" value='3' οnclick="operate('three')"></td>
<td><input type="button" name="plus" id="plus" value='+' οnclick="operate('plus')"></td>
<td rowspan="2"><input type="reset" name="reset" id="reset" value="重
置" style="width:30px;height:44px;"></td>
</tr>
<tr>
<td><input type="button" name="four" id="four" value='4' οnclick="operate('four')"></td>
<td><input type="button" name="five" id="five" value='5' οnclick="operate('five')"></td>
<td><input type="button" name="six" id="six" value='6' οnclick="operate('six')"></td>
<td><input type="button" name="reduce" id="reduce" value='-' οnclick="operate('reduce')"></td>
</tr>
<tr>
<td><input type="button" name="seven" id="seven" value='7' οnclick="operate('seven')"></td>
<td><input type="button" name="eight" id="eight" value='8' οnclick="operate('eight')"></td>
<td><input type="button" name="nine" id="nine" value='9' οnclick="operate('nine')"></td>
<td><input type="button" name="multiply" id="multiply" value='*' οnclick="operate('multiply')"></td>
<td rowspan="2"><input type="submit" name="submit" id="submit" value="确
定" οnclick="require()" style="width:30px;height:44px;"></td>
</tr>
<tr>
<td colspan='2'><input type="button" name="zero" id="zero" value='0' οnclick="operate('zero')"  style="width:50px;"></td>
<td><input type="button" name="point" id="point" value='.' οnclick="operate('point')"></td>
<td><input type="button" name="divide" id="divide" value='/' οnclick="operate('divide')"></td>
</tr>
</table>


</body>
</html>



calculator.php

<?php
$jsonVar = $_REQUEST['jsonVar'];
$varArr = json_decode(stripslashes($jsonVar)); //反序列化之后的值
//echo '<br>varArr='.var_dump($varArr);
$expr;
foreach ($varArr as $key=>$value){
if ($key==0) { $expr = $value; }
if ($value != '+' && $value != '-' && $value != '*' && $value != '/' ) {
//echo "<BR>value=".$value;
$value = (float)$value;
}else{
switch ($value){
case '+':
$expr = $expr + (float)$varArr[$key+1];
break;
case ' ':
$expr = $expr + (float)$varArr[$key+1];
break;
case '':
$expr = $expr + (float)$varArr[$key+1];
break;
case NULL:
$expr = $expr + (float)$varArr[$key+1];
break;
case '-':
$expr = $expr - (float)$varArr[$key+1];
break;
case '*':
$expr = $expr * (float)$varArr[$key+1];
break;
case '/':
$expr = $expr / (float)$varArr[$key+1];
break;
}

}//end if
// var_dump($value);
}//end foreach
//echo "<BR>值:".$expr;
exit($expr);
?>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值