2013-02-01 35 views
0

我有一個複選框和提交按鈕表格的html表單。 當按鈕被點擊時,我打電話和ajax_function在那裏我想傳遞 的數據到另一個php頁面。我不知道如果我做了什麼,到目前爲止是正確的,所以我可以proceed.Here是阿賈克斯finction:使用JSON傳遞數組值的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> 

回答

0

您可以刪除整個部分

var ajaxRequest; 
... 
ajaxRequest.onreadystatechange = function(){ 
... 
} 

因爲直接的背後,你已經有了一個jQuery AJAX調用

$.ajax(...); 

否則,代碼似乎罰款,只要你的PHP頁面預期輸入作爲JSON。

此外,爲什麼你在一邊使用jQuery.ajax(),但收集你的輸入參數與純JavaScript的另一邊?