Link to home
Start Free TrialLog in
Avatar of warrenlyle
warrenlyle

asked on

how to call a php without loading a new page...

Hi,

I want to call the following php code from a web page, but I do not want the browser to draw a new web page.. Is there a way i can just call the web page in the background? Without having to go to a different page so that the user can continue using the same page??

I am using the following html code to call it..
<form method ="POST" action="copyfriday.php">

Thanks.

<?php

// get the array of the selections
$selection = $_POST['selection'];

$source = "./xmlFiles/"; //the source folder (ie. where the file is being copied from)
$destination = "./xmlFiles2/"; //enter destination folder here (ie. where the file is being copied to)

echo $selection;
echo $source;
echo $destination;

//loop through the array and copy the selected files to the destination folder
foreach ($selection as $var) {
 copy($source.$var, $destination.$var);

 }
 

?>
Avatar of basiclife
basiclife

I'd reccomend opening a new window which closes itself using javascript after the command has been executed.

Failing that, open a new window and in the new window, output the result of the copy operation. This will cause a new window to appear and say "Copy completed!" or similar

If you want an example of the code to open a new window, tell me and I'll go look for the JavaScript to close a window now.

As PHP is server-side, there is no was to use PHP unless there is a new connection to the server. The only other possibility I can think of is a Javascript to open a connection to the script in the background. I don't know if this is possible using post but should be possible using get.

ie on the "onSubmit" event, the script opens "http://www.yourserverhere.com/copyscript.php?selection=somefilename" as if the Javascript were going to read the data for some use but in fact, it'll just launch PHP on the server and execute your script
In either case, I think it's going to be a Java solution you need.
If you just mean you wish to leave the current page open but also open another, you can use the target parameter for the form tag. For example:

<form method = "POST" action="copyfriday.php" target="_blank">

To close the page immediatly (it'll flash) output this in copyfriday.php . Even though the new client window is closed, the server request is still proccessed:

<html>
<title></title>
<head>
<script language='JavaScript'>
self.close()
</script>
</head>

<?php
 //Your php here
?>


If you wish to close the window using a link instead:
<a href='javascript:self.close()'>Close</a>
This works assuming the client browser has javascript enabled (doesn't everyone?).

Hope this helps.
          Stuart Konen
Upon further research (I asked someone)...

The following code will apparently open an invisible window

<script>
function executeURL(form) {
  obj = document.getElementById("myWindow");
  obj.src = "http://somewhere.com/script.php?field1=" + form["field1"].value;
  return false;
}
</script>

<iframe id="myWindow" src="about:blank" style="top:-10;left:-10;position:absolute" width="0" height="0"></iframe>

<form action="#" method="post" onsubmit="return executeURL(this)">
<input type="text" name="field1">
<input type="submit" value="submit">
</form>
You didn't say whether the form page was php and you have access to it.

If it is php, you could remove the action so that the page reloads to itself.  Then at the top of your script have a check for the post.  Then simply include your php code and use a header to reload the page (in case the user presses refresh). ie.  if your submit button is named submit:

if ( isset($_POST['submit']) ) {
 // User has submitted
 // include the code that handles the post
 include( copyfriday.php );

  // Once its done, reload the page.  Your location may be different. . .
  $location = "Location: ".basename($_SERVER['PHP_SELF']);
  header( $location );
  exit;
}

This would simply be a pure php solution. . .

dtan
All of the above solutions only work on the internet explorer. But there is another way:

I guess you want to execute the php script when somebody clicks on a button:
html:
..
<head>
<script type="text/javascript">
function execute(url){
   dummy=new Image();
   dummy.src=url+"?field1="+document.formname.field1.value;
   alert('The script was executed with field1: "+document.formname.field1.value);
   return false;
}
</script>
...

<form name="formname" action="#" onsubmit="return execute('copyfriday.php')">
<input type="text" name="field1">
<input type="submit" value="submit">
</form>

and start copyfriday.php with
<?php
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
...
so that your script is not cached and works every time you click the button
True, but it doesn't meet the criteria of "but I do not want the browser to draw a new web page" except that I suppose this is not _exactly_ a new page but it does require a re-loading of the page, so I avoided your solution. That aside, however, it's a very elegant implementation
d_tans script will work on any client
I forgot, the data will come to copyfriday.php as GET, not POST.
Avatar of Marcus Bointon
The proper way to do a spontaneous background connection without reloading the page is to use an XMLHttpRequest object, which works in nearly all browsers, although MS and Mozilla have slightly differing implementations. With an XMLHttpRequest, you don't have to submit a form or anything - so you could have a javascript news ticker that fetches its headlines dynamically from an RSS feed without having to do a page refresh, and without having to resort to Java, which is just overkill. There is a nice guide here:

http://developer.apple.com/internet/webcontent/xmlhttpreq.html

As basiclife said:
> As PHP is server-side, there is no was to use PHP unless there is a new connection to the server.

XMLHttpRequest provides a solution by allowing a new and arbitrary connection to occur in the background WITHOUT a page refresh, which is its main contrast. Don't confuse Java and Javascript; They could hardly be more different other than in name.
ASKER CERTIFIED SOLUTION
Avatar of Diablo84
Diablo84

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'd love to be miffed but that's a REALLY neat solution