donglie7778 2016-10-28 09:02
浏览 69

如何使用jquery&Php将选定的复选框表行值插入到数据库中

Please help me. Thanks in advance. Actually I am trying to insert selected checkbox rows into database using jquery but i don't know how to insert into DB.My jquery code is selecting the row but how to check whether the row is passing through ajax request. My table has a dynamic row from the database table with checkboxes. so when i checked the checkboxes and click the submit button then the table row should insert into the database table.

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条回答 默认 最新

  • dongmo8943 2016-10-28 10:23
    关注

    your data inside last td is an input so you must

    change from

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

    to

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

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私