2012-04-18 49 views
0

我想將上傳的文件名添加到('.list')中。文件的名稱必須是它在服務器上傳時被調用的名稱。例如,我可以有2個文件,但有一個被稱爲mountains.png,另一個是mountains2.png。如何在JavaScript函數中傳遞一個php代碼?

但問題是,我怎麼能傳遞$ _FILES [「fileImage」] [「name」]作爲我的js函數的參數,然後追加它,因爲JavaScript函數和PHP腳本是在單獨的頁面上PHP腳本確實做了一個回調函數)?

UPDATE

下面是JavaScript代碼:

下面是形式代碼(QandATable.php)

<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startImageUpload(this);' class='imageuploadform' > 
     <p>Image File: <input name='fileImage' type='file' class='fileImage' /> 
     <input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /> 
     </p> 
     <ul class='list'></ul> 
     </form> 

下面是JavaScript函數(QandATable.php)

 function stopImageUpload(success){ 

    var nameimagefile = <?php echo $nameimagefile?>; 
     var result = ''; 
     if (success == 1){ 
     result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>'; 
     $('.listImage').append(nameimagefile + '<br/>'); 
     } 
     else { 
     result = '<span class="emsg">There was an error during file upload!</span><br/><br/>'; 
     } 

    return true; 

    } 

下面是t他的PHP腳本(imageupload.php):

$result = 0; 
    $nameimagefile = ''; 

     if(file_exists("ImageFiles/".$_FILES['fileImage']['name'])) { 
      $parts = explode(".",$_FILES['fileImage']['name']); 
      $ext = array_pop($parts); 
      $base = implode(".",$parts); 
      $n = 2; 

      while(file_exists("ImageFiles/".$base."_".$n.".".$ext)) $n++; 
      $_FILES['fileImage']['name'] = $base."_".$n.".".$ext; 

      move_uploaded_file($_FILES["fileImage"]["tmp_name"], 
      "ImageFiles/" . $_FILES["fileImage"]["name"]); 
      $result = 1; 
$nameimagefile = $_FILES["fileImage"]["name"]; 

     } 
      else 
       { 
       move_uploaded_file($_FILES["fileImage"]["tmp_name"], 
       "ImageFiles/" . $_FILES["fileImage"]["name"]); 
       $result = 1; 
$nameimagefile = $_FILES["fileImage"]["name"]; 

       } 


     ?> 

     <script language="javascript" type="text/javascript">window.top.window.stopImageUpload(<?php echo $result;?>);</script> 
+0

你必須尋找到AJAX爲 – hjpotter92 2012-04-18 11:30:11

回答

0

你可以簡單地把價值$ _FILE文件名到一個PHP變量比使用

var yourjasvariable=<?php echo $yourvariable?>; 

呼應,並使用這個js在append方法變量。 :-)

+0

只是一個簡單的問題,$ yourvariable = $ _FILES [「fileImage」] [「name」];進入php腳本頁面(imageupload.php),還是與javascript函數在同一頁面上? – user1324106 2012-04-18 11:38:31

+0

好吧。如果您使用2個不同的頁面,則應該查看Ajax/JSON以在Javascript和PHP之間交換數據。 – SativaNL 2012-04-18 11:43:20

+0

將文件名稱作爲另一個參數傳遞,如果存在多個文件,則可以使用數組。 $ yourvariable = $ _FILES [「fileImage」] [「name」];這將只存在於你的imageupload.php中。謝謝 – 2012-04-18 11:46:02

0

你可以選擇AJAX做你想做的。 用JSON編寫數據。 JSON可以從PHP和JavaScript 閱讀 - 閱讀JSON在PHP 獲取數據 - 讀取AJAX結果(JSON)從PHP

獲取數據

我會做這樣的事情(未經測試的例子)

AJAX JS部分

<form method='post' enctype='multipart/form-data' onsubmit='startAjaxImageUpload(this);' > 
     ... 
</form> 

/* 
* ajax functions 
*/ 
function startAjaxImageUpload(event){ 

    /* Collect your formdatas as json with jquery this datas will be sent to php*/ 
    var formDatas = { 
      'value1'  : $('input[test1=eid]').val(), 
      'value2'  : $('input[id=test2_id]').val(), 
      ...... 
     'value3' : $('input[id=test3_id]').val() 
     }; 

    $.ajax({ 
     cache: false, 
     url: "imageupload", 
     data: formDatas, 
     success: function(data) { 
      // data is the json Result from php => imageupload.php do what u want with them in js 
      // use the next line if u wanna see which json datas comes back from php if the ajax call wass successfull 
      // console.log("data is %o, data); 
      // .... 

     } 
     error:function(data){ 
      // error function 
      // data is the json Result from php => imageupload.php do what u want with them in js 
      // use the next line if u wanna see which json datas comes back from php if the ajax call wass successfull 
      // console.log("data is %o, data); 
      alert(damn, something went wrong); 
     } 
    }) 
} 

PHP的一部分,imageupload.php

$result = 0; 
    $nameimagefile = ''; 
    ..... 
    // if done ure work on server side and no error was found, pass the result back to starAjaxImageUpload success function 
    return $nameimagefile = $_FILES["fileImage"]["name"]; 
    }else 
    // abbort ajax, ajax error function will used 
    return false 
      } 
相關問題