如何使用jquery& amp;将所选的复选框表行值插入到数据库中腓

时间:2016-10-28 09:02:52

标签: php jquery

请帮帮我。提前致谢。实际上我正在尝试使用jquery将选定的复选框行插入到数据库中,但我不知道如何插入到DB中。我的jquery代码正在选择行但是如何检查行是否通过ajax请求。我的表有一个带有复选框的数据库表中的动态行。因此,当我选中复选框并单击提交按钮时,表行应插入到数据库表中。

My code as below:
test.php

<?php
$con = mysql_connect("localhost","root","");
if(!$con)
{
   die("Could not connect:".mysql_error());

}
mysql_select_db("cakephp",$con); 
$sql = mysql_query("select * from carts where userid=13"); 

?>
<html>
<head>
  <title></title>
  <script src="jquery.js"></script>
  <script src="jquery.validate.min.js"></script>
  <script src="jquery-ui.min.js"></script>
  <style type="text/css">  
     .row_selected {background-color: #BFCBD7;} 
    </style>
  <script>

  $(document).ready(function(){

    $('.chk').on('change', function() {
        //alert('Hai');
        var thisCheckbox = $(this);
        var thisRow = thisCheckbox.closest('tr'); 
        if ( thisCheckbox.is(':checked') ) {
            thisRow.addClass('row_selected');
        } else {
            thisRow.removeClass('row_selected'); 
        };
    });

    $('#orderSave').on('click', function(row,tr) {

    //e.preventDefault();
    //var toPost = $('.row_selected input').serialize();                 
    /* Now post and insert into database */
    //$.post('/invl_exams/ordercompletion', toPost, function(data) {
        //alert('Success!');
    //});


    // Here i am creating the array to hold each selected row
    var TableData = new Array();       

    var priVal = $(tr).find('td:eq(6)').text();    
    var priceVal = priVal.replace('$',''); 


    TableData[row] = {

             //column names: corresponding row data  
              "userid" : $('#userid').val(),
              "username" : $('#hiddenUser').val(),
              "date" : $(tr).find('td:eq(3)').text(),   
              "exam" : $(tr).find('td:eq(4)').text(),
              "venue" : $(tr).find('td:eq(5)').text(),
              "price" : priceVal,
              "total_orders" : $(tr).find('td:eq(7)').text(), 
              "amount" : $(tr).find('td:eq(8)').text(),
              "orders_completion" : $(tr).find('td:eq(9)').text()    

          } 


      TableData = JSON.stringify(TableData);
      //console.log(TableData);

      $.ajax({ 

       type : "POST",    
       url : "testsubmit.php",                  
       cache : "false",
       data :  {data:TableData},            
       success : function(result){  

         console.log(result);                               

       } 


      }); 



  });


  });



  </script>

</head>
<body>
<table border="1">
  <tr>
   <th>S.No</th>
   <th>Exam Name</th>
   <th>Venue</th>
   <th>Date</th>
   <th>Price</th>
   <th>No of Orders</th>
   <th>Amount</th>
   <th>Checks</th> 
  </tr>
  <?php
   $i=1;
   while($row = mysql_fetch_array($sql)) 
   {
  ?>
  <tr>
    <td><?php echo $i++;?></td>
    <td style="display:none" id="userid">13</td> 
    <td style="display:none" id="hiddenUser">Gautham</td>    
    <td><?php echo $row['exam_name'];?></td>
    <td><?php echo $row['venue'];?></td>
   <td><?php echo $row['date'];?></td>
   <td><?php echo $row['price'];?></td>
   <td><?php echo $row['noOf_orders'];?></td>
   <td><?php echo $row['amount'];?></td>
   <td><input type="checkbox" name="chk" class="chk" value="1"></td>  
  </tr>
  <?php
   }
  ?>
  <tr>
    <td colspan="7">&nbsp;</td>
    <td><button type="button" class="btn btn-success btn-xs" id="orderSave">Submit</button></td>
  </tr>
</table>
</body>
</html>

testsubmit.php ( In this file i was checking the post request using print_r($_POST) ) 

<?php
$con = mysql_connect("localhost","root","");
if(!$con)
{
   die("Could not connect:".mysql_error());

}
mysql_select_db("cakephp",$con);

//if(isset($_POST['toPost']))
//{
  print_r($_POST);      

//}


?>

1 个答案:

答案 0 :(得分:0)

你上次td内的数据是输入所以你必须

改变
"orders_completion" : $(tr).find('td:eq(9)').text() 

"orders_completion" : $(tr).find('.chk')[0].checked