2012-05-10 43 views
1

我卡在這裏。我一直在嘗試上傳一些照片,同時將相冊的ID(表單中隱藏的值)傳遞給處理上傳的相同的php腳本。但我不知道如何通過專輯ID單獨這是代碼。傳遞數據到一個php腳本與AJAX

的js

input.addEventListener("change", function (evt) { 
     document.getElementById("response").innerHTML = "<img src='../assets/admin/images/loading.gif' />" 
     var i = 0, len = this.files.length, img, reader, file; 

    for (; i < len; i++) { 
     file = this.files[i]; 

     if (!!file.type.match(/image.*/)) { 
      if (window.FileReader) { 
       reader = new FileReader(); 
       reader.onloadend = function (e) { 
        showUploadedItem(e.target.result, file.fileName); 
       }; 
       reader.readAsDataURL(file); 
      } 
      if (formdata) { 
       formdata.append("images[]", file); 
      } 
     } 
    } 

    if (formdata) { 
     $.ajax({ 
      url: "../assets/admin/ajaxupload/upload.php", 
      type: "POST", 
      data: formdata, 
      processData: false, 
      contentType: false, 
      success: function (res) { 
       document.getElementById("response").innerHTML = res; 
      } 
     }); 
    } 

PHP

//how do i retrieve the given album id value that was passed. 

foreach ($_FILES["images"]["error"] as $key => $error) { 
    if ($error == UPLOAD_ERR_OK) { 
     $name = $_FILES["images"]["name"][$key]; 
     move_uploaded_file($_FILES["images"]["tmp_name"][$key], "../../uploads/pics/" .$_FILES['images']['name'][$key]); 


    } 
} 
echo "<p>Successfully Uploaded Images</p>"; 

請我需要一個答覆儘快感謝。

+1

*請我需要一個答覆儘快感謝。 *這不是它如何工作的朋友。 –

+0

並且這個其他的「相冊ID」是否坐在變量中? – 2012-05-10 09:50:36

+0

是的,它在php代碼中。 –

回答

1

(對不起,我沒有大的時間來解釋,但希望幫助)

在你的Ajax PHP文件

$errors = array(); // initialize empty error array 
if (sizeof($errors) == 0) { 
     ... 
     if ($securimage->check($captcha) == false) { 
      $errors['captcha_error'] = 'wrong code'; 
     } 
} 
    if (sizeof($errors) == 0) { 
     // no errors, send the form 
     $return = array('error' => 0, 'message' => 'OK'); 
     die(json_encode($return)); 
    } else { 
     $errmsg = ''; 
     foreach ($errors as $key => $error) { 
      // set up error messages to display with each field 
      $errmsg .= " - {$error}\n"; 
     } 
     $return = array('error' => 1, 'message' => $errmsg); 
     die(json_encode($return)); 
    } 

您的JS

url: 'ajax.php', 
     type: "POST", 
     data: $('#formID').serialize(), 
     success: function(msg) { 
      try { 
       //     alert("Data Saved: " + msg); 
       json = jQuery.parseJSON(msg); 
       //      alert(json.error) 
       if (json.error == 0) { 
       } catch(e) { 
       alert("Sorry, there was an error parsing the json"); 
      } 
     }, 
     error: function(msg) { 
      alert("Ajax request failed"); 
     }