簡體   English   中英

發布已檢查的復選框值,使用jquery動態生成到php

[英]Post checked checkbox values that ate generated dynamically to php using jquery

我有一個從MySQL表值動態生成的表。 每個列出的記錄都有一個復選框

list.php的

$query="SELECT * FROM drivers ORDER BY name";
$query=$mysqli->query($query);
$queryN=mysqli_num_rows($query);
if($queryN!=0){
   while($row=mysqli_fetch_assoc($query)){
       $id=$row['id'];
       $name=$row['name'];

       $output .='<tr id="'.$id.'">
                  <td>'.$name.'</td>
                  <td><input type="checkbox" name="assign" class="assigned" value='.$id.'"/></td>
                  </tr>';
  }
}else{
       $output .='<tr>
                  <td colspan="2">No data found</td>
                  </tr>';
}

HTML文件

<input type="button" name="send" id="send" value="Send" />

現在,JQuery腳本將僅發送表中已選中復選框的值:

<script>

$(document).ready(function(){
    $('.send').click(function(){

        var checkValues = $('input[name=assign]:checked').map(function()
        {
            return $(this).val();
        }).get();

        $.ajax({
            url: 'assigndriver.php',
            type: 'post',
            data: { ids: checkValues },
            success:function(data){

            }
        });
    });
});

</script>

最后,PHP文件assigndriver.php將插入數組,其值僅為已選中的復選框:

   include "../connect_to_mysql.php";

   $id = array();

   if(isset($_POST['ids'])){

    $id  = $_POST['ids'];
        for ($i = 0; $i < count($id); $i++) {
             $id_new = $id[$i];
             $sql="INSERT INTO teste (campo) VALUES ('$id_new')";    
        }     
    }

好吧,我想知道為什么它不起作用,如果以這種方式復選框的正確值,那必須是變量$ id正確地發布到PHP文件。

謝謝

嘗試改變:

$('.send').click(

至:

$('#send').click(

因為您將id="send"而不是class="send"分配給輸入按鈕

您應該將復選框的名稱更改為name =“something []”。 所以它會知道它是一個數組,值將正確發布。 希望這對你有所幫助。

在jquery中,您使用id send調用發送按鈕,但在調用您使用的函數時 操作員對操作員進行了操作。

區別

  the difference between "." operator and "#" operator is when you calling a 
  class you should use "."  operator.

  Example:
          HTML:
               <input type="button" name="send" class="send" value="Send" />  

         JQUERY:
                $('.send').click(function(){

                     //insert your code here..
                });


  but if you are going to call an id of a html element than use "#" operetor  

  Example: 
          HTML:
               <input type="button" name="send" id="send" value="Send" 
                onclick="send_data();" />



         JQUERY:
                 function send_data(){
                 $('#send').click(function(){

                     //insert your code here..
                });}

希望它能幫到你......

將每個選中復選框的值的正確方法發布到PHP文件的正確方法是執行以下腳本:

$(document).ready(function(){
    $('#btnadd').click(function(){

        var checkValues = $('input[name="assign[]"]:checked').map(function()
        {
            return $(this).val();
        }).get();

        $.ajax({
            url: 'assigndriver.php',
            type: 'post',
            data: { ids: checkValues },
            success:function(data){

            }
        });
    });
});

另外,我們需要更改PHP文件中的動態輸出:

$output .='<td align="center"><input type="checkbox" name="assign[]" id="add_driver'.$id.'" value="'.$id.'"/></td>';

如果選中該復選框,則將正確發布值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM