Jeg holder stadig på at dette er en exceptionel dårlig ide, men her er et eksempel, Ajax controllen er hjemmelavet så forbehold for fejl, og det går fuldkommen galt hvis den starter det flere gange uden at den når at stoppe.
status.xml
<?xml version="1.0" encoding="UTF-8"?>
<status total="1" current="0"/>
upload den og sæt permissions til 666.
send_mail.php
- <?php
- $total=60;
- $dom = new DOMDocument();
- $dom->load('status.xml');
- $dom->documentElement->setAttribute("total",$total);
- $dom->documentElement->setAttribute("data",$_POST['test']);
- $dom->documentElement->setAttribute("current",0);
- $dom->save('status.xml');
- for($current=0;$current<$total;$current++){
- sleep(1);
- $dom->documentElement->setAttribute("current",$current);
- $dom->save('status.xml');
- }
- $dom->documentElement->setAttribute("current",$total);
- $dom->save('status.xml');
- ?>
det her er eksempel kode, du skal indlysende nok selv skrive mail koden. mail koden skal udføre $dom->save(); inden for et vist antal iterationer ikke for hver iteration medmindre du
index.php
<html>
<head>
<script src="ajax.js"></script>
<script>
function check(){
checkmail=new AjaxCall();
checkmail.asynchronous=true;
checkmail.onloadedOK=up;
checkmail.url="status.xml";
AJAX.makeCall(checkmail);
}
function up(obj){
var bar=document.getElementById("bar");
var text=document.getElementById("text");
var root=obj.getAsXML().firstChild;
text.innerHTML=root.getAttribute("current")/(root.getAttribute("total")/100)+"%";
text.style.marginLeft="-"+root.getAttribute("current")/(root.getAttribute("total")/100)+"%";
bar.style.width=root.getAttribute("current")/(root.getAttribute("total")/100)+"%";
bar.innerHTML=" ";
if(root.getAttribute("current")!=root.getAttribute("total")){
setTimeout(check,2000);
}
}
function send(){
sendmail=new AjaxCall();
sendmail.url="send_mail.php";
sendmail.method="POST";
sendmail.addParameter("test",document.getElementById("test").value);
sendmail.asynchronous=true;
AJAX.makeCall(sendmail);
check();
}
</script>
</head>
<div style="width:200px;border:1px solid black"><div id="bar" style="float:left;width:0%;background-color:black;"></div><div id="text" style="width:100%;text-align:center;margin-left:0%;float:left;color:red"> </div><div style="clear:both"></div></div>
<form action="javascript:send();">
<input type="text" value="test" id="test"/>
<input type="submit" value="submit"/>
</form>
</form>
</html>
exempel på brugen den sender værdier af text feltet med som test i $_POST
ajax.js
- /*
- AjaxControl made by J. P. Svensson
- */
- function AjaxCall(){
- this.onuninitialized=null;
- this.onopen=null;
-
- this.onsent=null;
- this.onreceiving=null;
- this.onloaded=null;
- this.onloadedOK=null;
- this.onloadedNOTFOUND=null;
-
- this.asynchronous=true;
- this.url="";
- this.method="GET";
- this.request=null;
- this.parameters=null;
- this.getAsXML=function(){
- return this.request.responseXML;
- }
- this.getAsText=function(){
- return this.request.responseText;
- }
- this.addParameter=function(name,value){
- if(this.parameters==null){
- this.parameters=name+"="+escape(encodeURI(value));
- }else{
- this.parameters+="&"+name+"="+escape(encodeURI(value));
- }
- }
- }
-
- function AjaxControl(){
- this.callObjects=new Array();
- this.makeCall=function(aCallObj){
- if (window.XMLHttpRequest) { // Mozilla, Safari, ...
- aCallObj.request = new XMLHttpRequest();
- }else{
- if(window.ActiveXObject) { // IE
- aCallObj.request = new ActiveXObject("Microsoft.XMLHTTP");
- }
- }
- i=this.callObjects.push(aCallObj)-1;
- aCallObj.request.onreadystatechange=new Function("AJAX.readystatechange("+i+");");
- aCallObj.request.overrideMimeType('text/xml');
- aCallObj.request.open(aCallObj.method, aCallObj.url, aCallObj.asynchronous);
- if(aCallObj.method=="POST"){
- aCallObj.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- aCallObj.request.setRequestHeader("Content-length", aCallObj.parameters.length);
- aCallObj.request.setRequestHeader("Connection", "close");
- }
- aCallObj.request.send(aCallObj.parameters);
- }
-
- this.readystatechange=function(aIndex){
- lCallObj=this.callObjects[aIndex];
- if(lCallObj.request.readyState==0){
- if(lCallObj.onuninitialized!=null)
- lCallObj.onuninitialized(lCallObj);
- return;
- }
- if(lCallObj.request.readyState==1){
- if(lCallObj.onopen!=null)
- lCallObj.onopen(lCallObj);
- return;
- }
- if(lCallObj.request.readyState==2){
- if(lCallObj.onsent!=null)
- lCallObj.onsent(lCallObj);
- return;
- }
- if(lCallObj.request.readyState==3){
- if(lCallObj.onreceiving!=null)
- lCallObj.onreceiving(lCallObj);
- return;
- }
- if(lCallObj.request.readyState==4){
- if(lCallObj.onloaded!=null)
- lCallObj.onloaded(lCallObj);
- if(lCallObj.onloadedOK!=null&&lCallObj.request.status==200)
- lCallObj.onloadedOK(lCallObj);
- if(lCallObj.onloadedNOTFOUND!=null&&lCallObj.request.status==404)
- lCallObj.onloadedNOTFOUND(lCallObj);
- return;
- }
- }
- }
- AJAX=new AjaxControl();