2012-04-05 50 views
0

你好我嘗試使用JavaScript來發布兩個字符串到另一個PHP頁面(字符串是動態的,並會被JS程序在運行時創建)javascript post form.submit();

我想用下面的代碼打開一個新的窗口,該頁面的代碼中使用下面的代碼發佈

var title_string = "title1|title2"; 
var barcode_string = "barcode1|barcode2"; 
var path="create_labels.php"; 
var method = "post"; 
var params = "titles=" + title_string + "&barcodes=" + barcode_string; 
    // The rest of this code assumes you are not using a library. 
// It can be made less wordy if you use one. 
var form = document.createElement("form"); 
form.setAttribute("method", method); 
form.setAttribute("action", path); 
form.setAttribute("target", "_blank"); 

for(var key in params) { 
    if(params.hasOwnProperty(key)) { 
     var hiddenField = document.createElement("input"); 
     hiddenField.setAttribute("type", "hidden"); 
     hiddenField.setAttribute("name", key); 
     hiddenField.setAttribute("value", params[key]); 

     form.appendChild(hiddenField); 
    } 
} 

document.body.appendChild(form); 
form.submit(); 

爲了簡單起見,我剛纔創建兩個字符串(標題和條形碼)暫時對此我要檢索發佈的數據

IM通過form上的帖子發送。提交

該im不確定的是創建的參數,我這樣做是正確的嗎?該代碼確實打開了一個新窗口,但由於某種原因,我無法檢索發佈的數據,我認爲我發佈錯誤。

回答

0

你見過這種形式的輸出嗎?您正在爲參數字符串的每個字符創建隱藏字段。你確定要這麼做嗎?

0

params是一個字符串,而不是一個對象,因此它沒有任何鍵。你最終處理字符串的每個字符,並與該結束了:

<input type="hidden" name="0" value="t"> 
<input type="hidden" name="1" value="i"> 
<input type="hidden" name="2" value="t"> 
<input type="hidden" name="3" value="l"> 
<input type="hidden" name="4" value="e"> 
<input type="hidden" name="5" value="s"> 
<input type="hidden" name="6" value="="> 
<input type="hidden" name="7" value="t"> 
<input type="hidden" name="8" value="i"> 
<input type="hidden" name="9" value="t"> 

...等...

可能需要此bit of code將其轉換。