2014-06-18 40 views
0

我無法獲得'name'和'email'onclick的POST值傳遞給我的PHP。 $ _POST在數組中沒有值。我想我需要添加變量到Javascript,但我無法得到正確的語法。請幫忙,謝謝!將數組POST到PHP與Ajax

<script> 

    function _(el){ return document.getElementById(el); 
    } 
    function uploadFile(){ 
     var file = _("file1").files[0]; 
     var formdata = new FormData(); 
     formdata.append("file1", file); 
     var ajax = new XMLHttpRequest(); 
     ajax.upload.addEventListener("progress", progressHandler, false); 
     ajax.addEventListener("load", completeHandler, false); 
     ajax.addEventListener("error", errorHandler, false); 
     ajax.addEventListener("abort", abortHandler, false); 
     ajax.open("POST", "file_upload_parser.php"); 
     ajax.send(formdata); 
    } 

    function progressHandler(event){ 
     _("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total; 
     var percent = (event.loaded/event.total) * 99; 
     _("progressBar").value = Math.round(percent); 
     _("status").innerHTML = Math.round(percent)+"% uploaded... please wait"; 
    } 
    function completeHandler(event){ 
     _("status").innerHTML = event.target.responseText; 
     _("progressBar").value = 0; 
    } 
    function errorHandler(event){ 
     _("status").innerHTML = "Upload Failed"; 
    } 
    function abortHandler(event){ 
     _("status").innerHTML = "Upload Aborted"; 
    } 

    </script> 

    </head> 
    <body> 


    <center> 
    <table width="700" border="0"> 
    <form id="upload_form" enctype="multipart/form-data" method="post"> 
     <tr> 
     <td><strong>Name:</strong></td> 
     <td><input type="text" name="name" id="name" size="35"></td> 
     </tr> 
     <tr> 
     <td><strong>Email:</strong></td> 
     <td><input type="text" name="email" id="email" size="35"></td> 
     </tr> 
     <tr> 
     <td><strong>Select File:</strong></td> 
     <td><input type="file" name="file1" id="file1"></td> 
     </tr> 
     <tr> 
     <td colspan="2"> 
     <input type="button" value="Upload File" onclick="uploadFile()"> 
     <progress id="progressBar" value="0" max="100" style="width:300px;"></progress> 
     <h3 id="status"></h3> 
     <p id="loaded_n_total"></p> 
     </td> 
     </tr> 
    </form> 
    </table> 
    </center> 
+0

將它們添加到'formdata'實例,類似你如何添加_File_ –

+0

謝謝保羅 - 你能給我一個例子嗎?該文件附加罰款,我無法獲得名稱和電子郵件字段連接到數組。謝謝, – BradM

回答

0

這是正確的答案:

formdata.append("name", document.getElementById('name').value); 
formdata.append("email", document.getElementById('email').value); 

所以整個數據塊從上述追加應該是這樣的:

function uploadFile(){ 
    var file = _("file1").files[0]; 
    var formdata = new FormData(); 
    formdata.append("file1", file); 
    formdata.append("name", document.getElementById('name').value); 
    formdata.append("email", document.getElementById('email').value); 
    var ajax = new XMLHttpRequest(); 
    ajax.upload.addEventListener("progress", progressHandler, false); 
    ajax.addEventListener("load", completeHandler, false); 
    ajax.addEventListener("error", errorHandler, false); 
    ajax.addEventListener("abort", abortHandler, false); 
    ajax.open("POST", "file_upload_parser.php"); 
    ajax.send(formdata); 
}