2017-05-21 50 views
0

我是php和html的新手,我遇到了問題。我有這個form上傳文件:從php返回數據到html

<h2>Upload file</h2>     
    <form name="uploadFile" action = "upload.php" method = "POST" enctype = "multipart/form-data"> 
    <input type = "file" name = "document"/> 
    <input type = "submit"/> 

在我的外部upload.php文件,如果上傳成功我贊同與文件和消息的名稱的字符串。如果我用echo函數顯示值是正確的,但我不想重定向到upload.php頁面,我想用HTML顯示它。

如何在HTML表單頁中打印我的upload.php文件的返回值?

+1

如果你不想將用戶重定向到另一個頁面,您可以處理POSTDATA在當前腳本。 要檢查表單是否提交使用如果是這樣的: 'if(isset($ _ POST ['document']))' – datagutten

+2

使用ajax並返回來自upload.php的響應以在html頁面上顯示 – Nimish

+0

how我可以嗎?你能提供代碼或例子嗎? – pnappa

回答

0

您可以重定向到PHP頁面,像這樣GET變量:upload.php?status=success 然後你就可以檢查status變量

+2

不downvoting但這並沒有幫助。 – RST

+0

你能舉個例子嗎? – pnappa

+0

上傳完成後重定向到upload.php吧?改爲重定向upload.php?status = success。然後你可以像這樣檢查狀態'$ status = isset($ _ GET ['status'])? $ _GET ['status']:「noStatus」;' –

0

加入你的PHP代碼在您的形式是文件和名稱的頂部上傳它作爲.PHP文件。請嘗試下面的代碼,

<?php 
    if(isset($_POST['document'])) { 
    //do your post actions here 
    //validate your input 
    //upload the file 
    //print success or error message 
    } 
?> 
<h2>Upload file</h2>     
<form name="uploadFile" method = "POST" enctype = "multipart/form-data"> 
    <input type = "file" name = "document"/> 
    <input type = "submit"/> 
+0

當你的action屬性有'upload.php'時,你永遠不會回到這裏'$ _POST ['document']'not null,它會在提交時總是重定向到'upload.php'。 –

+0

@NiravMadariya,假設文件本身upload.php或我將有空行動。我會糾正我的答案。對不起,我錯過了。謝謝你的收穫。 – manian

+0

如何在我的index.php(表單所在的位置)顯示upload.php表單動作的返回數據?在你發佈的php代碼中,如何從upload.php打印返回數據? – pnappa

1

請更改您index.php文件,如下所示:

<?php 
     if(isset($_FILES['document'])) { 
     echo $_FILES["document"]["name"]; 
     echo "<p id='results'></p>"; 
?> 
<script> 
     $(document).ready(function() { 
      $.ajax({ 
       url: "data.json", 
       dataType: "text", 
       success: function(data) { 
        $('#results').html(data); 
       } 
      }); 
     }); 
</script> 

<?php } ?> 
<h2>Upload file</h2>     
<form name="uploadFile" method = "POST" enctype = "multipart/form-data"> 
    <input type = "file" name = "document"/> 
    <input type = "submit"/> 
</form> 

這將打印您上傳的文件的文件名。
當您使用文件上傳時,PHP保存名稱,文件大小,以及所有其他屬性爲$_FILES變量,它是一個全局變量。

正如你所說你想在文件上傳後立即閱讀其他文件,這可能會有所幫助。

+0

是的,它的工作表示感謝。我還需要從upload.php外部腳本返回另一個信息,而不僅僅是文件名。任何想法? – pnappa

+0

其他信息,比如說什麼? –

+0

一個字符串裏有文件中的內容。 – pnappa

-1

因爲Nirav已經回答了。通過一些修改來使用它。

<?php 
if(isset($_FILES['document'])) { 
    echo $_FILES["document"]["name"]; 
    // when uploaded the file assign status=1; 
    $file_name=$_FILES["document"]["name"]; 
    $status="1"; 
} 
?> 
<script type="text/javascript"> 
var status = <?php echo $status; ?>; 
var filename = <?php echo $file_name; ?>; 
$(document).ready(function(){ 
    if(status){ 
     $.ajax({ 
     url: "upload.php", 
     type: "POST", 
     data: { 
      status:status, 
      filename: filename 
     } 
     success: function(result){ 
        $('#element').html(result); 
       } 
     }); 
    } 
    else{ 
     $('#element').html("failed"); 
    } 

}); 
</script> 

HTML

<h2>Upload file</h2>     
<form name="uploadFile" action="upload.php" method = "POST" enctype = "multipart/form-data"> 
    <input type = "file" name = "document"/> 
    <input type = "submit"/> 
</form> 
<div id="element"><!--Here Show message from upload.php--></div> 

upload.php的

<?php 
if(isset($_POST['status']) && !empty($_POST['status'])){ 

    echo $_POST['filename']." uploaded successfully"; 
} 
?>