我想將數組形式傳遞給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 []數組的形式,這樣我可以用這數組用於上面的頁面?
刪除[]使用VAR元素= document.getElementsByName( 'SID'); –
你最近怎麼樣?不要忘記在瀏覽器中使用網絡監視器來查看AJAX操作的請求和響應。 – halfer