Iul*_*sca 5 html javascript php ajax
我想为服务器端任务创建一个进度条(用php编写)
出于学习目的,示例和任务将非常简单.我会在客户端页面上有一个文本字段,读取a number,用ajax将其传递给php脚本并使其计算所有数字的总和from 0 to number(简单的任务需要一些时间来处理大数字,只是为了模拟一些服务器端工作)
在.html文件中,我将创建一个计时器,每隔n几秒钟调用一个函数获取我的for循环得到的索引并更新进度条.
我的问题是:
是否有可能在同一个php文件中有两个函数,我怎么能用ajax调用一个特定的函数:一个会阻塞循环到number另一个我会调用来获取当前索引的for循环得到的至.
我到目前为止的代码:
<!DOCTYPE html>
<html>
<head>
<script>
function myTimer()
{
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("percentageDiv").innerHTML=xmlhttp.response;
alert(xmlhttp.response);
}
}
xmlhttp.open("GET","getter.php",true);
xmlhttp.send();
}
function loop(){
var loop_index = document.getElementById("loop_nr").value;
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("sumDiv").innerHTML="Total sum = " + xmlhttp.response;
clearInterval(myVar);
}
}
xmlhttp.open("GET","server_side.php?nr="+loop_index,true);
xmlhttp.send();
var myVar=setInterval(function(){myTimer()},1000);
}
</script>
</head>
<body>
<div id="percentageDiv"> Percentage div</div>
<div id="sumDiv"></div>
<input type="text" id="loop_nr">
<input type="submit" onclick="loop()">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
server_side.php
<?php
session_start();
$index=$_GET["nr"];
$progress = 0 ;
$sum = 0 ;
for ($i = 1; $i <= $index; $i++) {
$sum = $sum + $i;
$progress++;
$_SESSION['progress'] = $progress;
}
echo $sum;
?>
Run Code Online (Sandbox Code Playgroud)
getter.php
<?php
session_start();
$progress = $_SESSION['progress'];
echo $progress;
?>
Run Code Online (Sandbox Code Playgroud)
谢谢!
Tom*_*ado 12
你的问题是两个:
你的AJAX代码很好.您在PHP中唯一需要做的就是接收此调用.
看看你的要求.您nr根据请求发送变量:
server_side.php?nr="+loop_index
这将帮助我们在PHP代码中确定这是一个AJAX调用来执行求和操作.现在在PHP中:
<?php session_start();
//We look at the GET php variable to see if the "nr" is coming
if(isset($_GET['nr'])) {
//Its coming!. Now we procede to call our function to sum
$result = sum($_GET['nr']);
}
function sum($nr) {
$progress = 0 ;
$sum = 0 ;
for ($i = 1; $i <= $nr; $i++) {
$sum = $sum + $i;
$progress++;
$_SESSION['progress'] = $progress;
}
echo $sum;
}
Run Code Online (Sandbox Code Playgroud)
而已.
我们需要进行其他AJAX调用来请求PHP的进度.
首先,我们进行另一个AJAX调用以使用计时器检索进度!
var timer;
//try to delete duplications. Do a generic function that does the request to php
function makeRequest(toPHP, callback) {
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)
{
callback(xmlhttp.response);
}
}
xmlhttp.open("GET",toPHP,true);
xmlhttp.send();
}
function loop() {
var loop_index = document.getElementById("loop_nr").value;
makeRequest("server_side.php?nr="+loop_index, function(response) {
document.getElementById("sumDiv").innerHTML="Total sum = " + response;
clearInterval(timer);
});
timer=setInterval(makeRequest("getter.php", function(response) {
document.getElementById("percentageDiv").innerHTML=response;
}),1000);
}
Run Code Online (Sandbox Code Playgroud)
然后在php端我们检索这个调用,就像我们之前做的那样并回显$_SESSION['progress'](就像你已经拥有的那样)
<?php
session_start();
$progress = $_SESSION['progress'];
echo $progress;
?>
Run Code Online (Sandbox Code Playgroud)
就是这样!
| 归档时间: |
|
| 查看次数: |
22852 次 |
| 最近记录: |