Onclick事件更新localhost数据库

时间:2013-01-29 16:56:14

标签: php javascript ajax database onclick

我有一个表格,表格有5行8列。此表代表音乐厅的可用座位。单击提交按钮后,将调用一个javascript函数,此时此函数为:

    <script>
    function submit_tickets(form){
      var count=0;
      var booked_seats=new Array();
      var c = document.getElementById(form).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++;
    //create an array with the booked seats' ID
    }
    }


//alert(booked_seats.length);
    } </script>

调用此函数时,我希望同时更新localhost数据库的表(包含所有40个seatID的表),具体取决于预订的席位。 我怎么能这样做,你能帮助我吗?

2 个答案:

答案 0 :(得分:0)

如果您愿意我可以在原生javascript中编写示例,但是现在我将尝试使用jquery来完成。

function submit_tickets(form){
    var seats=[];
    $(form).find("input:checked").each(function(){
        seats.push($(this).val());//i hope you do have a value for your input tags, and if not, then push $(this).attr('id') - but highly recommend you to follow w3c   

    });
    $.post("your/url/to/server",{seats:seats},function(data){
          console.log(data);//some info from server
    })
}

答案 1 :(得分:0)

我相信您需要使用PHP调用php文件,其中数据库更新完成。您可以考虑使用jquery ajax,如http://api.jquery.com/jQuery.ajax/

相关问题