Solved

PHP, AJAX, "Please wait", and a redirect

Posted on 2009-05-20
6
1,693 Views
Last Modified: 2013-12-13
Folks,

I have page1.php  that loads and displays an animated gif image to entertain people while the order they have placed is being processed.

I know there has to be a way to get AJAX to fire off the execution of the processorder.php file that does all of the complex order processing and posts the user order.

The last thing I need to accomplish, is to make the page1.php page redirect to the next php opage when the ajax request is complet. So here's my questions:

1) If there are no form elements on this page, can I call the processorder.php using AJAX  in the <body> tag with an onload event?
2) Once it returns the results, I want he page to be immediately redirected to aother page. How can I do this, assuming that I can use the return from the AJAX call to indicate when it is complete.
3) Is it possible to remove the wait page (page1.php) from the browser's history so that the user does not hit the 'Back' button and start the process over inadvertently

I have seen a great example in asp.net at http://www.codeproject.com/KB/aspnet/wait_page.aspx but can't figure out how to recreate this in javascript and AJAX calls via PHP scripts.

Let me know if anyone needs more details. I've been reading web sites on how to get this done for 2 days, and a lot of them come close, but the redirect is the stumper...

Thanks!
0
Comment
Question by:tinklerb
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:Greg Alexander
ID: 24432115

function process_order(redirect_page)

{

xmlHttp=GetXmlHttpObject()

if (xmlHttp==null){

alert ("Browser does not support HTTP Request")

return

}
 
 

var url="processorder.php"

url=url+"?agetvariable="+agetvariable

url=url+"&sid="+Math.random()

xmlHttp.onreadystatechange=stateChangedVerify

xmlHttp.open("GET",url,true)

xmlHttp.send(null)

}

function stateChangedVerify() 

{ 

if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

 { 

 } 

}

function GetXmlHttpObject()

{

var xmlHttp=null;

try

 {

 // Firefox, Opera 8.0+, Safari

 xmlHttp=new XMLHttpRequest();

 }

catch (e)

 {

 //Internet Explorer

 try

  {

  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

  }

 catch (e)

  {

  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

  }

 }

return xmlHttp;

}

Open in new window

0
 
LVL 18

Accepted Solution

by:
Greg Alexander earned 400 total points
ID: 24432145

function process_order(redirect_page)

{

xmlHttp=GetXmlHttpObject()

if (xmlHttp==null){

alert ("Browser does not support HTTP Request")

return

}
 
 

var url="processorder.php"

url=url+"?agetvariable="+agetvariable

url=url+"&sid="+Math.random()

xmlHttp.onreadystatechange=stateChanged

xmlHttp.open("GET",url,true)

xmlHttp.send(null)

}

function stateChanged() 

{ 

if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

 { 

 window.location="new_page_here.php";

 } 

}

function GetXmlHttpObject()

{

var xmlHttp=null;

try

 {

 // Firefox, Opera 8.0+, Safari

 xmlHttp=new XMLHttpRequest();

 }

catch (e)

 {

 //Internet Explorer

 try

  {

  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

  }

 catch (e)

  {

  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

  }

 }

return xmlHttp;

}

Open in new window

0
 
LVL 18

Assisted Solution

by:Greg Alexander
Greg Alexander earned 400 total points
ID: 24432180
The 2nd one above ought to get you started. basically this ajax request can be called from page one. It then sends a request to processorder.php, and when done, will redirect to another page. There are a couple of things that we will need to changed, but this will get us started.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Assisted Solution

by:manavsi
manavsi earned 100 total points
ID: 24439128
try using xAjax... for PHP

xAjax: http://xajaxproject.org/
0
 

Author Comment

by:tinklerb
ID: 24440152
Folks,
Thanks for the suggestions. I have some time set aside this afternoon to try to put them into place, and will reply later today or forst thing tomorrow with the results. In the mean time, thanks for the help!
Brian
0
 

Author Comment

by:tinklerb
ID: 24502001
Folks,
I have successfully got points one and two to work based on the recommendations. Now on to the third point I had above.
3) Is it possible to remove the wait page (page1.php) from the browser's history so that the user does not hit the 'Back' button and start the process over inadvertently
Once this is solved, this screen can go into production. Any ideas?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now