2015-04-27 143 views
2

我想將數組形式傳遞給JavaScript,然後傳遞給AJAX。這裏是形式:如何將數組值從表單傳遞到JavaScript到AJAX?

<form> 
 
\t <?php 
 
\t  $c=$_POST['sweater']; 
 
\t \t foreach ($c as $sid){ 
 
\t \t \t echo $sid; 
 
\t \t \t $query1="select * from `usersweater` where `Sweaterid`='$sid'"; 
 
\t \t \t $result1=mysql_query($query1); 
 
\t \t \t $row1=mysql_fetch_assoc($result1); 
 
\t \t \t $sweaternikname=$row1['SNickname']; 
 
\t \t \t $sweaterpic=$row1['Sweaterpic']; 
 
\t \t \t echo $sweaterpic; 
 
\t ?> 
 

 
\t <div style=" "> 
 
\t \t <ul class="sweaters"> 
 
\t \t \t <li> <h4><?php echo $sweaternikname; ?></h4> <img src="upload/<?php echo $sweaterpic; ?>"> </li> 
 
\t \t </ul> 
 
\t </div> 
 
\t 
 

 
\t \t \t \t \t <!-------requester's own sweater details---------------> 
 
\t <input type="hidden" name="sid[]" value="<?php echo $sid;?>"> 
 
\t <?php } ?> 
 
     <div style="float:right; margin-right:10px;"> 
 
\t \t <input type="submit" name="next" onclick="ajaxFunction()" value="NEXT" class="btn woo_btn btn-primary"> 
 
\t \t <input type="button" name="cancel" value="CANCEL" class="btn woo_btn btn-primary"> 
 
\t \t </div> 
 
</form>

下面是腳本:

<script language="javascript" type="text/javascript"> 
 
function ajaxFunction(){ 
 
\t var ajaxRequest; // The variable that makes Ajax possible! 
 
\t try{ 
 
\t \t ajaxRequest = new XMLHttpRequest(); 
 
\t } catch (e){ 
 

 
\t \t try{ 
 
\t \t \t ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
 
\t \t } catch (e) { 
 
\t \t \t try{ 
 
\t \t \t \t ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
 
\t \t \t } catch (e){ 
 

 
\t \t \t \t alert("Your browser broke!"); 
 
\t \t \t \t return false; 
 
\t \t \t } 
 
\t \t } 
 
\t } 
 
\t ajaxRequest.onreadystatechange = function(){ 
 
\t \t if(ajaxRequest.readyState == 4){ 
 
\t \t \t var ajaxDisplay = document.getElementById('ajaxDiv'); 
 
\t \t \t ajaxDisplay.innerHTML = ajaxRequest.responseText; 
 
\t \t } 
 
\t } 
 
var elements = document.getElementsByName('sid[]'); 
 
} 
 
ajaxRequest.open("POST","Usercloset2.php" ,true); 
 
ajaxRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
 
ajaxRequest.send(elements); 
 
} 
 
</script>

如何發送SID []數組的形式,這樣我可以用這數組用於上面的頁面?

+0

刪除[]使用VAR元素= document.getElementsByName( 'SID'); –

+0

你最近怎麼樣?不要忘記在瀏覽器中使用網絡監視器來查看AJAX操作的請求和響應。 – halfer

回答

1

您可以創建一個查詢字符串,並將其發送類似

function ajaxFunction() { 
    var ajaxRequest; // The variable that makes Ajax possible! 
    try { 
     ajaxRequest = new XMLHttpRequest(); 
    } catch (e) { 

     try { 
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
      try { 
       ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (e) { 

       alert("Your browser broke!"); 
       return false; 
      } 
     } 
    } 
    ajaxRequest.onreadystatechange = function() { 
     if (ajaxRequest.readyState == 4) { 
      var ajaxDisplay = document.getElementById('ajaxDiv'); 
      ajaxDisplay.innerHTML = ajaxRequest.responseText; 
     } 
    } 
    var elements = document.getElementsByName('sid[]'); 
    var params = []; 
    for (var i = 0; i < elements.length; i++) { 
     params.push(elements[i].name + '=' + encodeURIComponent(elements[i].value)) 
    } 

    ajaxRequest.open("POST", "Usercloset2.php?" + params.join('&'), true); 
    ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
    ajaxRequest.send(); 
} 
+0

如何服用對方?我認爲$ _POST ['params']; ?? –

+0

yes'$ _POST ['sid']' –

+0

不,它沒有得到迴應 –

-1

首先,你必須聲明變量爲數組類型如下圖所示

var elements=[];

然後從輸入獲得的價值使用下面的代碼

elements=document.getElementsByName('sid');

然後在你的url中傳遞它。

希望這將SOVE您的問題

+0

'var elements = [];'給變量賦值一個數組。它不會使變量「數組類型」。 'elements = document.getElementsByName('sid');'用NodeList覆蓋數組,使得原始數組賦值毫無意義。 *然後將它傳遞給你url * - 這個字符串化爲'「[object NodeList]」,這不是很有用。 – Quentin