2015-08-13 34 views
0

我的客戶端希望上傳文件而不使用<form>標籤&也沒有使用提交按鈕在PHP中。使用7個單獨的文件輸入標籤上傳7個文件使用AJAX或JQuery或JavaScript在PHP

我知道如何用PHP上傳文件,但我真的不知道如何去做,而不使用<form>標籤。

他只希望我們使用<button>標記。如何在不使用<form>標記的情況下執行此操作。我到處搜索,但我沒有找到任何解決方案。 這裏是我的HTML代碼:

file1 
<input type="file" id="file1" name="file1" accept="*.jpg"><hr> 
file2 
<input type="file" id="file2" name="file2" accept="*.jpg"><hr> 
file3 
<input type="file" id="file3" name="file3" accept="*.jpg"><hr> 
file4 
<input type="file" id="file4" name="file4" accept="*.jpg"><hr> 
file5 
<input type="file" id="file5" name="file5" accept="*.jpg"><hr> 
file6 
<input type="file" id="file6" name="file6" accept="*.jpg"><hr> 
file7 
<input type="file" id="file7" name="file7" accept="*.jpg"><hr> 
<button id='upload' name='upload'>Upload</upload> 

請幫助我,我想這非常糟糕。請不要因爲沒有完成足夠的工作而丟棄它。 謝謝。

+1

我認爲這是不可能的。 Idiotic customer ... – Webice

+0

@ksealey哈哈,iam等待他的回答,希望看到它 – Webice

+1

你將不得不將它們包裝成一個''多'部分'屬性設置的'表格'標籤,通過'AJAX'將它們發送到服務器。 – Umair

回答

0

https://blueimp.github.io/jQuery-File-Upload/

你需要使用jQuery的表單庫

首先立即下載表單jQuery的表單庫提交

的HTML代碼,如:

<div class="row"> 
     <div class="col-md-4 col-xs-12"> 
     <div class="form-group"> 
       <form id="imageform" method="post" enctype="multipart/form-data" action='ajaximage.php'> 
       Upload your image <input type="file" name="photoimg" id="photoimg" /> 
       </form> 
       <div id='preview'> 
      </div> 

和jQuery的/ js代碼如:

document.getElementById("photoimg").onchange = function() 
    { 
        $("#preview").html(''); 
       $("#preview").html('<img src="loader.gif" alt="Uploading...."/>'); 
      $("#imageform").ajaxForm({ 
         target: '#preview' 
     }).submit(); 

      }; 

和ajaximage.php像

<?php 

//include('db.php'); 
session_start(); 
$session_id='1'; //$session id 
$path = "photo/"; 
    $valid_formats = array("jpg", "png", "gif", "bmp","JPG","PNG","GIF"); 
    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") 
     {  $name = $_FILES['photoimg']['name']; 
      $size = $_FILES['photoimg']['size']; 
      if(strlen($name)) 
       { 
        list($txt, $ext) = explode(".", $name); 
        if(in_array($ext,$valid_formats)) 
        { 
        if($size<(1024*1024)) 
         { 
          $actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext; 
          $tmp = $_FILES['photoimg']['tmp_name']; 
          if(move_uploaded_file($tmp, $path.$actual_image_name)) 
           { 

            echo "<img src='photo/".$actual_image_name."' class='preview'>"; 
                     echo "<input type='hidden' name='imageName' value='".$actual_image_name."' id='imageName'>";                 

           } 
          else 
           echo "failed"; 
         } 
         else 
         echo "Image file size max 1 MB";      
         } 
         else 
         echo "Invalid file format.."; 
       } 

      else 
       echo "Please select image..!"; 

      exit; 
     } 
?> 
0

你可以使用AJAX,請按一下按鈕的功能和發送使用AJAX的數據服務器,而無需提交表單。

相關問題