943,580 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 7504
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 4th, 2009
0

Execute PHP file in the background from html page

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kostas Theof is offline Offline
9 posts
since Jan 2009
Jan 4th, 2009
0

Re: Execute PHP file in the background from html page

What you should do when they submit your form to phpfile1.php do a redirect back to the htmlcode.html.
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 4
Light Poster
DiGSGRL is offline Offline
45 posts
since May 2008
Jan 4th, 2009
0

Re: Execute PHP file in the background from html page

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kostas Theof is offline Offline
9 posts
since Jan 2009
Jan 4th, 2009
0

Re: Execute PHP file in the background from html page

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.
Reputation Points: 10
Solved Threads: 4
Light Poster
DiGSGRL is offline Offline
45 posts
since May 2008
Jan 4th, 2009
0

Re: Execute PHP file in the background from html page

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kostas Theof is offline Offline
9 posts
since Jan 2009
Jan 4th, 2009
0

Re: Execute PHP file in the background from html page

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
Reputation Points: 10
Solved Threads: 4
Light Poster
DiGSGRL is offline Offline
45 posts
since May 2008
Jan 4th, 2009
0

Re: Execute PHP file in the background from html page

http://www.w3schools.com/Ajax/ajax_intro.asp
Thats a link to an AJAX tutorial from w3schools, maybe that will be helpful hehe.
Reputation Points: 10
Solved Threads: 4
Light Poster
DiGSGRL is offline Offline
45 posts
since May 2008
Jan 4th, 2009
0

Re: Execute PHP file in the background from html page

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kostas Theof is offline Offline
9 posts
since Jan 2009
Jan 5th, 2009
0

Re: Execute PHP file in the background from html page

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.
Reputation Points: 22
Solved Threads: 1
Newbie Poster
robert_c_guy is offline Offline
4 posts
since Dec 2008
Jan 9th, 2009
0

Re: Execute PHP file in the background from html page

Just write this at the bottom of your php files (like mentioned previously):

PHP Syntax (Toggle Plain Text)
  1. <?php header("Location:htmlcode.html");exit; ?>

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

PHP Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 1046
Solved Threads: 942
Sarcastic Poster
ardav is offline Offline
6,661 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Using Prepared Statement - Help!
Next Thread in PHP Forum Timeline: php mod_rewrite help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC