9 Replies - 1799 Views - Last Post: 11 November 2008 - 07:13 AM Rate Topic: -----

#1 bstonehill   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 113
  • Joined: 21-January 08

Popup doesn't open until after PHP operation is complete

Posted 10 November 2008 - 09:40 AM

I am trying to open a small popup window to provide some sort of progress report or at the very least to say "Please wait..." while a PHP script operates. I'm calling the popup before launching the php script, I even tried placing it in a separate if statement but it still does not popup until after the PHP finishes. Can someone please show me what I'm doing wrong?

//Change computer name
if(isset($_POST['submitname'])){
  echo "<script type='text/javascript'> 
		  window.open('changeme.php', 'newWin', 'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0,width=250,height=250');
		  newWin.focus();
		</script>";
}

if(isset($_POST['submitname'])){
  $name = $_POST['namebox'];
  system('c:\installation\apps\baptize.exe '.$name);
  
  $hostname = php_uname('n'); //Get current computer name
  
  //Check if change was successful
  if($hostname == $name){
	echo "<script type='text/javascript'> 
			window.close('newWin');
			alert('Name change successful');			  
		  </script>";
  }
  else {
	echo "<script type='text/javascript'> 
			window.close('newWin');
			alert('Name change failed'); 
		  </script>";
  }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Popup doesn't open until after PHP operation is complete

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6875
  • View blog
  • Posts: 31,665
  • Joined: 10-May 07

Re: Popup doesn't open until after PHP operation is complete

Posted 10 November 2008 - 09:44 AM

View Postbstonehill, on 10 Nov, 2008 - 10:40 AM, said:

Can someone please show me what I'm doing wrong?

Since PHP executes at server side, in order to check if $_POST['submitname']) is set, it won't be set until the page is sent back to the server for processing. Something like this would be better suited for Javascript, that executes Client Side.
Was This Post Helpful? 0
  • +
  • -

#3 Hary   User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 427
  • Joined: 23-September 08

Re: Popup doesn't open until after PHP operation is complete

Posted 10 November 2008 - 12:10 PM

You could use output buffering, and send your first part of the PHP file using ob_flush. This will be quite hard to get rendered right, some clientside JS will be easier to implement (You could try to implement it using an AJAX call)
Was This Post Helpful? 0
  • +
  • -

#4 mocker   User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Re: Popup doesn't open until after PHP operation is complete

Posted 10 November 2008 - 12:14 PM

Another way to implement it is to have your script print out the "Please wait.." message in a div, then in the <body onload event, call a function that hides that div. The onload doesn't trigger until the document is done loading, so as long as the main processing part is started after outputting "Please wait", users should see that while it loads.
Was This Post Helpful? 0
  • +
  • -

#5 spearfish   User is offline

  • Monkey in Training
  • member icon

Reputation: 10
  • View blog
  • Posts: 746
  • Joined: 10-March 08

Re: Popup doesn't open until after PHP operation is complete

Posted 10 November 2008 - 02:07 PM

I would definitely recommend some AJAX there. Here's why:

1) User requests a page from the server
2) Before sending the page, the server notices the .php extension on the file, and calls in the PHP parser
3) The PHP parser executes all PHP code, echos, database interactions, whatever
4) The finished HTML document is sent to the web browser

So, as you can see, before the browser can even *start* to render the HTML, the PHP pieces have already been finished. It definitely sounds like you'd like some AJAX here. AJAX stands for "Asynchronous Javascript and XML". In short, it's how client side languages, like Javascript, can talk to server side languages, like PHP.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Popup doesn't open until after PHP operation is complete

Posted 10 November 2008 - 02:39 PM

Could it be that you're trying to solve a symptom and not the problem?

1) Does your site get a lot of traffic?

2) Is your php code doing something very complex?


If you don't answer yes to both, then maybe you need to work on making the code more efficient or maybe you need to work on getting more/better hardware.
Was This Post Helpful? 0
  • +
  • -

#7 bstonehill   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 113
  • Joined: 21-January 08

Re: Popup doesn't open until after PHP operation is complete

Posted 10 November 2008 - 02:51 PM

I guess I should elaborate a little more... This is strictly running locally so the server and client are one and the same. The page posts to itself and then checks if $_POST['submit'] isset.

Thanks
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Popup doesn't open until after PHP operation is complete

Posted 10 November 2008 - 03:08 PM

Then why do you need a progress report?
Was This Post Helpful? 0
  • +
  • -

#9 Valek   User is offline

  • The Real Skynet
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Popup doesn't open until after PHP operation is complete

Posted 10 November 2008 - 04:00 PM

View PostCTphpnwb, on 10 Nov, 2008 - 02:08 PM, said:

Then why do you need a progress report?


I would have to ask the same thing.

Also, the two being on the same computer doesn't change the "order of operations", so to speak. The server side language is still executed before the page is sent to the browser, so you'd still have to use straight Javascript, or implement AJAX.
Was This Post Helpful? 0
  • +
  • -

#10 engale   User is offline

  • D.I.C Addict
  • member icon

Reputation: 3
  • View blog
  • Posts: 594
  • Joined: 30-September 08

Re: Popup doesn't open until after PHP operation is complete

Posted 11 November 2008 - 07:13 AM

if I where you I would use another page that showed the "pop up" then direct them to the next page auto. This way you get your pop up before your php script starts.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1