简体   繁体   English

复选框 jquery 使用 ajax 发送数据并保存在 php 中未插入

[英]checkbox jquery using ajax to send data and save in php not inserting

I tried to serialize rather to push the elements value but still I get the array with inserting each value 9 times in database.我尝试序列化而不是推送元素值,但我仍然通过在数据库中插入每个值 9 次来获得数组。 please anyone to help me figure out my problem this is what I am trying to请任何人帮助我找出我的问题这就是我想要做的

The HTML: HTML:

    <form name="chkl" id="chkl" method="post">  
                
<input type="checkbox" class="get_value" name="chk1[ ]" value="<?php echo $rows['ID'];?>">&nbsp<?php echo $rows['DESCRIPTION'];?></input><br>

<input type="submit" name="Submit" id="chk1" value="Submit"> 

JS: JS:

<script>
 $('#chk1').click(function(){  
$('input[type=checkbox]').each( function() {
          $.post('fee_checked_save.php', $( ".get_value" ).serialize(), function(data){
          if(data == 1){
             $('#result').html(data);  
          }
      });
});
});
</script>

PHP: PHP:

<?php  
    
  $checkbox1 = $_POST['chk1'];
    if(isset($_POST['chk1']))  
    {  
    for ($i=0; $i<count ($checkbox1);$i++) {  
    $query = "INSERT INTO fee_checked(FEE_ID) VALUES ('".$checkbox1[$i]. "')";  
   $result = mysqli_query($conn, $query);
    }  
    echo "Record is inserted";  
    }  
    ?>  

Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks ahead of time!提前谢谢!

You are sending all checkbox value but, i think you need to only send checkbox which is checked so use :checked then you have use $( ".get_value" ).serialize() this is targetting all class that's why you are seeing mutliple insert instead change that to $(this).serialize() .您正在发送所有复选框值,但是,我认为您只需要发送已选中的复选框,因此请使用:checked checked您使用$( ".get_value" ).serialize()这是针对所有 class 这就是您看到多重插入的原因而是将其更改为$(this).serialize() I have added other way as well instead of sending checkbox ony by one you can use $(this).closest("form").serialize() to send whole form datas at onces.我还添加了其他方式,而不是一个一个地发送复选框,您可以使用$(this).closest("form").serialize()发送整个表单数据。

Demo Code :演示代码

 $('#chk1').click(function(e) { e.preventDefault() //send only input which is checked $('input[type=checkbox]:checked').each(function() { console.log($(this).serialize()) //use `this` here,, $.post('fee_checked_save.php', $(this).serialize(), function(data) { if (data == 1) { $('#result').html(data); } }); }); }); //prefer way: $('#chk1').click(function(e) { e.preventDefault() console.log("from second way.." + $(this).closest("form").serialize()) $.post('fee_checked_save.php', $(this).closest("form").serialize(), function(data) { //some codes }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form name="chkl" id="chkl" method="post"> <input type="checkbox" class="get_value" name="chk1[]" value="1">&nbsp; Soemthings.. <input type="checkbox" class="get_value" name="chk1[]" value="2">&nbsp; Soemthings.. <input type="submit" name="Submit" id="chk1" value="Submit"> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM