How do I get my very simple view’s html items to re-appear after a form’s POST?
I lay out the html controls below, then when the user selects the
‘Upload’ submit button, a file is uploaded (successfully) but all
the previously-laid-out view elements disappear. Again, the upload
of the file works fine. It’s just that the html controls I displayed
on index.php vanish when the form gets uploaded and the browser window
is blank.
How do I get my very simple view back after a form’s POST?
THIS IS index.php:
<body>
<img src="/theWebsite/images/banner2.jpg" /img>
<br />
<input type="button" value="Play" onclick="showAnAlert(){}" />
// other buttons and text field html elements not shown
<form enctype="multipart/form-data" action="file-upload.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
</body>
Here is file-upload.php:
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$uploadingFile = $_FILES['uploaded']['tmp_name'] ;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
// I thought the problem was this 'echo' but the main view still goes blank after
// I commented this out here....
//echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";
}
else {
// echo "errmsg"
}
?>
After posting your form to
file-upload.php, you do not redirect it back toindex.phpwhere the HTML resides. You need to call a redirect after doing your form processing:You may also need to redirect in your error condition. In that case, put the
header()call at the very end.