2015-10-27 36 views
0

基本上,我使用的是<form action=MyPhp.php>將客戶端上的文件上傳到我的服務器。當上傳成功(或不成功)時,我會向客戶端發送消息以顯示結果(成功或失敗)。在由form =「action」調用的警報框中顯示PHP函數的輸出

這裏有個訣竅:我不知道如何捕捉那條消息。如果只是javascript/php我會使用XMLHttpRequest(),但我認爲它可以更容易。我已經檢查過參數「onsubmit =」和「target =」,但我不知道如何訪問消息。我的目標是將該消息顯示爲javascript:alert(「來自php的消息」)。任何建議,將不勝感激。這裏是我的代碼:

HTML

<form action="Upload.php" method="post" enctype="multipart/form-data"> 
    <div class="custom-button-style browse-button-style" onclick="Browse()">Browse part</div> 
    <input class="toHide" type="file" id="fileToUpload" name="fileToUpload" data-role="none"/> 
    <input class="toHide" type="submit" value="Upload Part" id="submit" name="submit" data-role="none"/> 
</form> 

PHP

<?php 
$target_dir = "upload/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 

if($imageFileType != "zip") 
{ 
    echo "The file selected doesn't have the right extension. Please select a .zip file."; 
    $uploadOk = 0; 
} 

if($uploadOk == 1) 
{ 
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
    { 
     $zip = new ZipArchive(); 
     $res = $zip->open($target_file); 
     if($res == TRUE) 
     { 
      $zip->extractTo($target_dir); 
      $zip->close(); 
      unlink($target_file); 
      echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
     } 
    } 
    else 
    { 
     echo "Sorry, there was an error uploading your file."; 
    } 
} 
else 
{ 
    echo "Sorry, your file was not uploaded."; 
} 
?> 
+0

其實你可以'回聲「「;'如果你不想創建一個Ajax請求。 –

回答

0

你coud嘗試這樣的事:

echo "<script language='JavaScript' type='text/javascript'>"; 
    echo "alert('your message');"; 
    echo "</script>"; 
+0

「警報」將顯示在客戶端? – MacGruber

+0

據我所見,這應該是這種情況。 – Silencio

+0

我測試過了。顯示的消息,但它沒有顯示在頁面上,我單擊按鈕,但在空白頁面(這是我的.php)。顯然,它顯示消息立即在PHP上,並沒有返回到HTML顯示它。有什麼辦法解決這個問題嗎? – MacGruber

相關問題