2013-05-27 96 views
0

我無法將額外的信息附加到formData對象,所以它可以在PHP中用於添加到SQL數據庫。不幸的是它依賴於AJAX,所以測試你需要在服務器或本地主機上運行它,但我已經包含一個簡單的PHP文件,將需要被稱爲'StackedTest.php'添加新值到formData對象時遇到問題

我一直在關注該示例MDN,但經過很多時間尋找答案我沒有得到任何地方。

MDN formData object

感謝您的幫助。

<html> 

<head> 

    <script type="text/javascript"> 
     var returned; 
     var fd; 

     function ajaxCall(sends){ 

      fd = new FormData(); 
      var images = document.getElementById('images'); 

      fd.append('file', images.files[0]); 


      /** 
      ** 
      ** This doesn't seem to be working, I am trying to append the value of the select box ** to the FILES array 
      ** so I can use it in php to add to an SQL database. 
      ** 
      **/ 

      fd.append('catagory', document.getElementById('cat').value); 

       var ajax=new XMLHttpRequest(); 
       ajax.onreadystatechange=function(){ 
       if (ajax.readyState==4 && ajax.status==200){ 
        document.getElementById("response").innerHTML = ajax.response; 
        fd = null; 
        } 
       } 
       ajax.open("POST","Stackedtest.php",true); 
       ajax.send(fd); 
      } 


    </script> 

</head> 

<body> 

<div id="content"> 

    <div> 
     <form method="post" enctype="multipart/form-data" action="?"> 
      <select name="cat" id="cat"><br> 
       <option value="opt1">option 1</option> 
       <option value="opt2">option 2</option> 
       <option value="opt3">option 3</option> 
      </select><br> 
      <input type="file" name="images[]" id="images" multiple /><br> 
      <button type="button" id="btn" onclick="ajaxCall()">Upload Files!</button> 
     </form> 

     <p id="response">Response text here</p> 

    </div> 

</div> <!-- END CONTENT --> 

</body> 

</html> 

PHP腳本

​​

回答

2

類被作爲使用後的超級全球$ _ POST

<?php 

if(isset($_FILES['file'])){ 
    print_r($_FILES['file']); 
    print_r($_POST['catagory']); 
} 

?> 

編輯後的數據,以便訪問:文件超全球是也通過郵寄發送,但僅限於上傳到PHP腳本的項目

+0

非常感謝您的工作。 – synthet1c

相關問題