Solved

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

Posted on 2004-08-15
13
232 Views
Last Modified: 2008-03-06
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
Comment
Question by:warrenlyle
  • 6
  • 2
  • 2
  • +3
13 Comments
 
LVL 5

Expert Comment

by:basiclife
ID: 11807113
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
 
LVL 5

Expert Comment

by:basiclife
ID: 11807114
In either case, I think it's going to be a Java solution you need.
0
 
LVL 4

Expert Comment

by:Skonen
ID: 11807330
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
 
LVL 4

Expert Comment

by:Skonen
ID: 11807337
This works assuming the client browser has javascript enabled (doesn't everyone?).

Hope this helps.
          Stuart Konen
0
 
LVL 5

Expert Comment

by:basiclife
ID: 11807390
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
 
LVL 2

Expert Comment

by:d_tan
ID: 11807830
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 9

Expert Comment

by:_GeG_
ID: 11807850
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
 
LVL 5

Expert Comment

by:basiclife
ID: 11807852
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
 
LVL 5

Expert Comment

by:basiclife
ID: 11807861
d_tans script will work on any client
0
 
LVL 9

Expert Comment

by:_GeG_
ID: 11807869
I forgot, the data will come to copyfriday.php as GET, not POST.
0
 
LVL 25

Expert Comment

by:Squinky
ID: 11807902
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
 
LVL 27

Accepted Solution

by:
Diablo84 earned 500 total points
ID: 11808407
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
 
LVL 5

Expert Comment

by:basiclife
ID: 11811281
I'd love to be miffed but that's a REALLY neat solution
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now