Javascript Ajax使用JSON传递数组值
我有一个带有复选框表和提交按钮的html表单。 单击按钮时,我调用和ajax_函数,在其中传递 将已预订的座位数组添加到另一个php页面。我不知道到目前为止我所做的是否正确,以便继续。以下是ajax功能:Javascript Ajax使用JSON传递数组值,javascript,ajax,arrays,json,parameters,Javascript,Ajax,Arrays,Json,Parameters,我有一个带有复选框表和提交按钮的html表单。 单击按钮时,我调用和ajax_函数,在其中传递 将已预订的座位数组添加到另一个php页面。我不知道到目前为止我所做的是否正确,以便继续。以下是ajax功能: <html> <script type="text/javascript" src="jquery.min.js"> function ajax_function(){ var count=0; var booked_seats=
<html>
<script type="text/javascript" src="jquery.min.js">
function ajax_function(){
var count=0;
var booked_seats=new Array(); // an array with all the checked 'seats'
var t1=parseInt(document.getElementById("basic_ticket").value)
var t2=parseInt(document.getElementById("red_ticket").value)
var total_amount=t1+t2;
var c = document.getElementById('myform').getElementsByTagName('input');
for (var i = 0; i < c.length; i++) {
if (c[i].type == 'checkbox' && c[i].checked==true){
booked_seats[count]=c[i].id;
count++;
}
}
var ajaxRequest;
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.myform.time.value = ajaxRequest.responseText;
}
}
$.ajax({
url: "book_seats_sh1.php",
data: JSON.stringify({ nameParameter: booked_seats }), //my array
success: function(noOfResults) {
alert(noOfResults);
}
});
函数ajax_function(){
var计数=0;
var booked_seats=new Array();//包含所有选中“seats”的数组
var t1=parseInt(document.getElementById(“基本票据”).value)
var t2=parseInt(document.getElementById(“red_ticket”).value)
var总金额=t1+t2;
var c=document.getElementById('myform').getElementsByTagName('input');
对于(变量i=0;i
}
下面是html的主体:
<body>
<form id="myform" action="book_seats_sh1.php" method="post">
<table width="212" border="1">
<tr>
<td bgcolor="#FF9966"><input type="checkbox" name="Seat_choice[]" id="Y1" /></td>
<td bgcolor="#FF9966"><input type="checkbox" name="Seat_choice[]" id="Y2" /></td>
<td bgcolor="#FF9966"><input type="checkbox" name="Seat_choice[]" id="Y3" /></td>
<td bgcolor="#FF9966"><input type="checkbox" name="Seat_choice[]" id="Y4" /></td>
<td bgcolor="#FF9966"><input type="checkbox" name="Seat_choice[]" id="Y5" /></td>
<td bgcolor="#FF9966"><input type="checkbox" name="Seat_choice[]" id="Y6" /></td>
<td bgcolor="#FF9966"><input type="checkbox" name="Seat_choice[]" id="Y7" /></td>
<td bgcolor="#FF9966"><input type="checkbox" name="Seat_choice[]" id="Y8" /></td>
</tr>
<tr>
<td bgcolor="#FF9966"><input type="checkbox" name="Seat_choice[]" id="Y9" /></td>
<td bgcolor="#FF9966"><input type="checkbox" name="Seat_choice[]" id="Y10" /></td>
<td bgcolor="#FF9966"><input type="checkbox" name="Seat_choice[]" id="Y11" /></td>
<td bgcolor="#FF9966"><input type="checkbox" name="Seat_choice[]" id="Y12" /></td>
<td bgcolor="#FF9966"><input type="checkbox" name="Seat_choice[]" id="Y13" /></td>
<td bgcolor="#FF9966"><input type="checkbox" name="Seat_choice[]" id="Y14" /></td>
<td bgcolor="#FF9966"><input type="checkbox" name="Seat_choice[]" id="Y15" /></td>
<td bgcolor="#FF9966"><input type="checkbox" name="Seat_choice[]" id="Y16" /></td>
</tr>
<tr>
(etc.)
</table>
<input type="button" name="Book_tickets" id="Book_tickets" value="Κράτηση"
onclick="ajax_function()"/>
</form>
</body>
</html>
(等等)
您可以移除整个零件
var ajaxRequest;
...
ajaxRequest.onreadystatechange = function(){
...
}
因为就在后面,您已经有了一个jqueryajax调用
$.ajax(...);
否则,只要您的PHP页面希望输入为JSON,代码就可以了
另外,为什么一边使用jQuery.ajax()
,另一边使用纯javascript收集输入参数