我有这个带有复选框表和提交按钮的 html 表单。单击按钮时,我会调用 ajax_function,我想在其中将 booking_seats 数组的值传递给另一个 php 页面。我想知道到目前为止我所做的是否正确,所以我可以继续。这是 ajax 函数:
<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);
}
});
}
这是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>