Well it's a simple result of the order in which data is requested/sent from/to the server by the client.
First of all you have your main web page which contains instruction to the user to upload an image and an iframe element. Within this iframe element is a web page which contains a form with a file selection input for an image.
The iframe's web page might look something like this:
HTML Code:
<html>
<body>
<form method="post" action="upload.php">
<input type="file" name="image" />
</form>
</body>
</html>
The user will select an image in the file selection input of the iframe of the main web page (yeah?) and submit the form inside the iframe. This begins a request to the server for upload.php with the selected file attached in $_FILE['image'].
The addition of the selected file in the request is the only difference between the request to the server in this case and the request to the server in any other case. So a response is still sent from the server and still displayed in the iframe.
When the server receives the request (made from within the iframe) it will take the uploaded image and do with it what it will. We then want it to return content based on the success of the operation.
So, at the end of our upload.php script, we can simply add something like:
PHP Code:
echo "Upload successful.";
When the server processes this pat of the upload.php script, 'Upload successful.' will be added to the output buffer
and sent back to the client. When the client receives this data it will display it as a regular piece of text.
What we have done here is create a main web page in which there is an iframe that can upload an image and display the result without changing the state of the main web page. We want to do something slightly different, though. We want the finishing of the upload to refresh the main web page.
In the order of events in which this occurs (based on the above) we can do this at any time after the script has processed the command to move the image into a more permanent location. There is only one time at which we can do it, however, and that is when the response is received from the server by the client (within the iframe). To do this we can simply output a small HTML page with some client-side scripts (which will automatically execute) sent to the browser at the end of upload.php.
In this case, the script to refresh the main web page is such that our output requires the following HTML to be sent to the browser:
HTML Code:
<html>
<head>
<script type="text/javascript">
// Reload the parent window
window.top.location.href = window.top.location.href;
</script>
</head>
</html>
(which doesn't actually require the window.onload event function as I had before).
Does that make sense? Hope it helps
