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

What you should do when they submit your form to phpfile1.php do a redirect back to the htmlcode.html.

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.

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?

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.

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.

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

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.

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

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

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

<?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.

Thank you ardav and robert_c_guy, I'll check what you suggest as soon as possible.

Is it possible by the way that the output of the php file gets stored in an external log file? (Either by appending new info about warnings, errors, normal output etc, or by replacing each time the existing log file with the new output? You see, this might be helpful sometimes.)

Thanks.

$output = bla blah bla; /* the data output from the background php page. I had to put SOMETHING here, */
$getdate = date( 'd-m-Y, H:i:s' );// Get the date.
$user_ip = $_SERVER['REMOTE_ADDR'];// Get the user IP, just in case.
$referer = getenv("HTTP_REFERER");// Get the refering page. just in case
$file = "./your path to logfile/logfile.csv"; /*define the file as required, csv opens easily in excell or imports to sql if REQ, but is really just a text file.*/
$fp = fopen($file, "a+"); //open the text file for writing.
// Write into the text file.
fputs ($fp, "$getdate, $user_ip, $referer, $output\n"); //see note after $output definition above
fclose($fp);// Close the text file.

create a blank text file logfile.csv (or whatever), this scrap is just an appendage to the existing page, not optimized, just an example, there can be any number of data fields to comprise $output

Seems i am a bit late to the game, but your problem definitely lends itself to using AJAX.

and it would be a rather simple solution at that.
Although instead of recommending you learn AJAX from scratch, I'm going to point you to the prototype library http://www.prototypejs.org/ I'm sure you've probably heard of it.

Now,as far as working with prototype, I think the most all encompassing tutorial I have ever seen has to be this one http://www.phpriot.com/series/8-weeks-of-prototype

If you browse through those 8 lessons you'll have a pretty sound understanding of how it all works and comes together.

Nice tutorial link. I agree if you don't want to leave/return to the html page, use Ajax. I use prototype myself and it's REALLY easy, and I don't even do JS that well.

very easy just put this in header

<script language="php" src='yourfile.php'></script>


YAHYA AL-SALMANI
SAUDI ARABIA

This article has been dead for over six months. Start a new discussion instead.