[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

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);

 }
 

?>
0
warrenlyle
Asked:
warrenlyle
  • 6
  • 2
  • 2
  • +3
1 Solution
 
basiclifeCommented:
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
0
 
basiclifeCommented:
In either case, I think it's going to be a Java solution you need.
0
 
SkonenCommented:
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>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
SkonenCommented:
This works assuming the client browser has javascript enabled (doesn't everyone?).

Hope this helps.
          Stuart Konen
0
 
basiclifeCommented:
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>
0
 
d_tanCommented:
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
0
 
_GeG_Commented:
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
0
 
basiclifeCommented:
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
0
 
basiclifeCommented:
d_tans script will work on any client
0
 
_GeG_Commented:
I forgot, the data will come to copyfriday.php as GET, not POST.
0
 
Marcus BointonCommented:
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.
0
 
Diablo84Commented:
you should be able to do it just by using an invisible iframe without the need of any additional js etc

somewhere on your page (near the bottom would probably be most practical)  add:

<iframe name="iprocess" frameborder="0" height="10px" width="10px" src="about:blank"></iframe>

then change the form line to:

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

The form should then be processed in the iframewithout having to open a new window
0
 
basiclifeCommented:
I'd love to be miffed but that's a REALLY neat solution
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now