如何在表单的POST后重新显示我的非常简单的视图的html项目?
我在下面列出html控件,然后当用户选择时 '上传'提交按钮,文件上传(成功)但全部 先前布局的视图元素消失了。再次,上传 该文件工作正常。这就是我显示的html控件 当表单上传和浏览器窗口时,index.php上的消失 是空白的。
如何在表单POST后获取非常简单的视图?
这是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>
这是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"
}
?>
答案 0 :(得分:2)
file-upload.php文件需要在上传完成后将用户重定向回index.php,或者需要在底部包含原始的index.php文件。
我投票表示您要么重定向,要么只是完全删除file-upload.php文件并在index.php文件中处理它。
答案 1 :(得分:2)
将表单发布到file-upload.php
后,您不会将其重定向回HTML所在的index.php
。您需要在完成表单处理后调用重定向:
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";
// Redirect back....
header("Location: http://www.example.com/index.php");
// Always call exit() right after a redirection header to prevent further script execution.
exit();
}
else {
// echo "errmsg"
}
您可能还需要在错误情况下重定向。在这种情况下,请将header()
电话放在最后。
答案 2 :(得分:1)
对于表单的操作,请指定$ _SERVER ['PHP_SELF']变量:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
然后确保添加逻辑以处理在同一.php文件中处理表单,该文件输出表单的html。另一种方法是让header()调用再次重定向到您的表单:
<form action="process_form.php" method="POST">
然后,在process_form.php中:
header("Location: http://mydomain.com/form_page.html");