Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-08-15
13
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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:Marcus Bointon
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 2000 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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

618 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