2011-06-27 40 views
0

如何讓我非常簡單的視圖的html項目在表單的POST後重新出現?表單POST擦除視圖

我鋪陳HTML控制之下,則當用戶選擇 「上傳」提交按鈕,一個文件被上傳(成功),但所有 先前佈局的視圖元素消失。再次,該文件的上傳 工作正常。只是在index.php上顯示的html控件 在表單上傳並且瀏覽器窗口 爲空時消失。

如何在表單的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> 

這裏是文件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" 
      } 
?> 

回答

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()呼叫放在最後。

+0

Shaka帥哥!那是我做的!唯一讓我誤解StackOverflow的是你只能'接受'一個答案 - 根據我的經驗,我發現自己從所有幫助過的人那裏學習 - 謝謝你們,我很樂意繼續這個。 – wantTheBest

+1

@ wantTheBest:Michael有最完整的答案(我也贊成他)。 – NotMe

2

的文件upload.php的文件需要要麼將用戶重定向回一旦上傳完成,或者需要在底部包含原始index.php文件,將其添加到index.php。

我投票說你要麼重新定向回來,要麼乾脆刪除file-upload.php文件,並在你的index.php文件中處理它。

+0

謝謝,這聽起來天真,但我怎麼'將用戶重定向回index.php'? – wantTheBest

+0

@wantTheBest在下面的答案中查看'header()'調用以瞭解如何重定向。 –

1

爲表單的動作,指定$ _ SERVER [「PHP_SELF」]變量:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> 

然後一定要添加的邏輯來處理處理在同一個PHP文件輸出的HTML表單你的形式。另一種方法是有再次重定向到您的窗體的標題()調用:

<form action="process_form.php" method="POST"> 

然後,在process_form.php:

header("Location: http://mydomain.com/form_page.html");