Execute PHP file in the background from html page

Reply

Join Date: Jan 2009
Posts: 9
Reputation: Kostas Theof is an unknown quantity at this point 
Solved Threads: 0
Kostas Theof Kostas Theof is offline Offline
Newbie Poster

Execute PHP file in the background from html page

 
0
  #1
Jan 4th, 2009
Hello to everyone and wish for a Happy New Year.
I have an html code from which I want to execute various php files (with various parameters).
Say the html file is called htmlcode.html.
Say one php file is called phpfile1.php and a second php file called phpfile2.php
Everything works fine but the problem is that when the php execution comes to an end, then the web browser turns away from my html page (htmlcode.html) and shows the results of the php file execution (which actually (if there are no warnings) is a blank screen).

I'll try to be more specific.
I have various <form action="phpfile1.php" ...> in html that when a certain <input type="button" ...> is pressed they execute the phpfile1.php.
No php file has any output and is executed in order to run a command in the shell via the exec command or to create an xml file with specific data. I don't actually need a log file to store any output from php file execution, but it would be nice if I could do so.

What I want is to execute the php files in the background, without turning the web browser away from htmlcode.html in order to display the php execution results.

Thank you in advance for your time.
Kostas Theof
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 45
Reputation: DiGSGRL is an unknown quantity at this point 
Solved Threads: 4
DiGSGRL's Avatar
DiGSGRL DiGSGRL is offline Offline
Light Poster

Re: Execute PHP file in the background from html page

 
0
  #2
Jan 4th, 2009
What you should do when they submit your form to phpfile1.php do a redirect back to the htmlcode.html.
  1. header("Location: htmlcode.html");
Or you can change the htmlcode to a php file and have your form validate to the same page instead by putting your php on the same page.
I like to keep things seperate so I would do the first way.
A little clarification goes a long way.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 9
Reputation: Kostas Theof is an unknown quantity at this point 
Solved Threads: 0
Kostas Theof Kostas Theof is offline Offline
Newbie Poster

Re: Execute PHP file in the background from html page

 
0
  #3
Jan 4th, 2009
As I'm a novice user I don't know where exactly to add this code:
header("Location: htmlcode.html");
do you mean as a command in the end of each php file?
The current location of all files is in http://localhost/ .

Even if it is so, I'm afraid this would make a refresh to htmlcode.html.
I actually don't want to make any refresh(es) to htmlcode.html as there is a GoogleMap showing some information dynamically created and after any unwanted (not scheduled) refresh some needed information created via JavaScript functions will be deleted. After any refresh those info will start being created again from the beginnig.

I've tried with success to stop the php execution via an event handler:
<INPUT type="submit" value="Submit1" name="Submit1" onMouseOut="if (navigator.appName=='Microsoft Internet Explorer') document.execCommand('Stop'); else window.stop();">
In this case the php execution continues on the background, creates the desired results and does not “disturb” the web browser.
The problem in this case is that if the user does not move the pointer out of the submit button the javascript code does not execute. Another case where the event handler won't work is when the user does not click on the Submit button but just presses the enter key while the specific form is "active" in the web browser.
I tried the event handler onMouseUp but didn't work (possibly because the action of the form starts when Mouse click is up.).
Finally, the form is the following:
<FORM method="post" action="http://localhost/phpfile1.php?param1=5&param2=1">.

Any suggestions please?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 45
Reputation: DiGSGRL is an unknown quantity at this point 
Solved Threads: 4
DiGSGRL's Avatar
DiGSGRL DiGSGRL is offline Offline
Light Poster

Re: Execute PHP file in the background from html page

 
0
  #4
Jan 4th, 2009
you can use onSubmit instead. So the action takes place when the form is submitted.
And yes the header would cause a refresh of the page because it would go to the php page then back.
A little clarification goes a long way.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 9
Reputation: Kostas Theof is an unknown quantity at this point 
Solved Threads: 0
Kostas Theof Kostas Theof is offline Offline
Newbie Poster

Re: Execute PHP file in the background from html page

 
0
  #5
Jan 4th, 2009
The onSubmit event handler didn't work.
I think this handler executes the JavaScript command before the action of the form (in this case the execution of the phpfile1.php).
It's like when we validate the contents of various input fields before executing the action of the form, the validation triggers with the onSubmit event handler.
I don't know if there is a way to start the execution of the php file onSubmit or onMouseDown. Then I could stop it onMouseUp.
(Or start onKeyDown and stop onKeyUp respactively).
I'll try on that direction.

Thaks for your time.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 45
Reputation: DiGSGRL is an unknown quantity at this point 
Solved Threads: 4
DiGSGRL's Avatar
DiGSGRL DiGSGRL is offline Offline
Light Poster

Re: Execute PHP file in the background from html page

 
0
  #6
Jan 4th, 2009
Sorry I am not very familiar with javascript. I did just check the event is used as onsubmit not onSubmit. But yea I do not think that is what you are looking for anyways.

I totally missed that last lien in your first post saying you didn't want a page refresh. Ignore me, I should go back to sleep and quit messing with other people's posts.

/nap
A little clarification goes a long way.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 45
Reputation: DiGSGRL is an unknown quantity at this point 
Solved Threads: 4
DiGSGRL's Avatar
DiGSGRL DiGSGRL is offline Offline
Light Poster

Re: Execute PHP file in the background from html page

 
0
  #7
Jan 4th, 2009
http://www.w3schools.com/Ajax/ajax_intro.asp
Thats a link to an AJAX tutorial from w3schools, maybe that will be helpful hehe.
A little clarification goes a long way.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 9
Reputation: Kostas Theof is an unknown quantity at this point 
Solved Threads: 0
Kostas Theof Kostas Theof is offline Offline
Newbie Poster

Re: Execute PHP file in the background from html page

 
0
  #8
Jan 4th, 2009
Thank you for your time DiGSGRL.
I'll check the link http://www.w3schools.com/Ajax/ajax_intro.asp you suggest and, as it seems that my problem will be solved via JavaScript, perhaps I'll post a thread in a JS relevant area.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 2
Reputation: robert_c_guy is an unknown quantity at this point 
Solved Threads: 0
robert_c_guy robert_c_guy is offline Offline
Newbie Poster

Re: Execute PHP file in the background from html page

 
0
  #9
Jan 5th, 2009
An alternative to a JavaScript resolution (if for whatever reason you want to or even just to have another way) is simply set the target of your form to a hidden frame which is much the way the AJAX lifestyle had its roots before anyone took the collection of things they were already doing and lumped it all together and called it AJAX. If your need is simple then it is possible that a not visible frame would provide a simple solution.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 954
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 126
ardav's Avatar
ardav ardav is offline Offline
Posting Shark

Re: Execute PHP file in the background from html page

 
0
  #10
Jan 9th, 2009
Just write this at the bottom of your php files (like mentioned previously):

  1. <?php header("Location:htmlcode.html");exit; ?>

or for more generic return code (it will return you to the previous page):

  1. <?php header("Location:{$_SERVER['HTTP_REFERER']}");exit; ?>
You MUST ensure that no text (error messages or echoed php or any HTML) is output to screen before the header function is called or you will get an error.
Last edited by ardav; Jan 9th, 2009 at 7:50 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC