我想将数组表单传递给JavaScript,然后传递给AJAX。这是表格:
<form>
<?php
$c=$_POST['sweater'];
foreach ($c as $sid){
echo $sid;
$query1="select * from `usersweater` where `Sweaterid`='$sid'";
$result1=mysql_query($query1);
$row1=mysql_fetch_assoc($result1);
$sweaternikname=$row1['SNickname'];
$sweaterpic=$row1['Sweaterpic'];
echo $sweaterpic;
?>
<div style=" ">
<ul class="sweaters">
<li> <h4><?php echo $sweaternikname; ?></h4> <img src="upload/<?php echo $sweaterpic; ?>"> </li>
</ul>
</div>
<!-------requester's own sweater details--------------->
<input type="hidden" name="sid[]" value="<?php echo $sid;?>">
<?php } ?>
<div style="float:right; margin-right:10px;">
<input type="submit" name="next" onclick="ajaxFunction()" value="NEXT" class="btn woo_btn btn-primary">
<input type="button" name="cancel" value="CANCEL" class="btn woo_btn btn-primary">
</div>
</form>
这是脚本:
<script language="javascript" type="text/javascript">
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[]');
}
ajaxRequest.open("POST","Usercloset2.php" ,true);
ajaxRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajaxRequest.send(elements);
}
</script>
如何以数组的形式发送sid [],以便我可以使用上一页中使用的这个数组?
答案 0 :(得分: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();
}
答案 1 :(得分:-1)
首先,您必须将变量声明为数组类型,如下所示
var elements=[];
然后使用下面的代码
从输入字段中获取值 elements=document.getElementsByName('sid');
然后将其传递给你。
希望这能解决你的问题