【问题标题】:Post checked checkbox values that ate generated dynamically to php using jquery使用 jquery 将动态生成的复选框值发布到 php
【发布时间】:2014-02-11 03:34:08
【问题描述】:

我确实有一个从 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 文件。

谢谢

【问题讨论】:

    标签: php jquery html mysqli


    【解决方案1】:

    尝试改变:

    $('.send').click(
    

    到:

    $('#send').click(
    

    因为您将 id="send" 而不是 class="send" 分配给您的输入按钮

    【讨论】:

    • 谢谢!我做了更改,但仍未发布值!
    【解决方案2】:

    您应该将复选框的名称更改为 name="something[]"。所以它会知道它是一个数组,并且值会正确发布。希望对您有所帮助。

    【讨论】:

    • 对不起各位!还是行不通。没有值被插入到数据库中。
    【解决方案3】:

    将每个选中复选框的值发布到 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>';
    

    如果选中复选框,则值将正确发布。

    【讨论】:

      【解决方案4】:

      在 jquery 中,您使用 id send 调用发送按钮,但在调用函数时,您使用 . 运算符 insted of # 运算符。

      区别

        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..
                      });}
      

      希望对你有帮助……

      【讨论】:

      • 即使将类替换为 id="send" 和 $("#send").click(... 值仍然无法发布并插入到数据库中。
      • ok 将你的 LIST.PHP 代码更改为 while($row=mysqli_fetch_assoc($query)) 为 while($row=mysqli_fetch_array($query))...希望它能工作。如果它的工作投票帖子...玩得开心
      • 你得到结果了吗?
      • 在assigndriver.php代码中调用mysql_query($sql);它肯定会起作用
      猜你喜欢
      • 1970-01-01
      • 2013-03-26
      • 2014-05-30
      • 2016-02-17
      • 1970-01-01
      • 2013-11-09
      • 2011-03-24
      • 2015-03-22
      • 1970-01-01
      相关资源
      最近更新 更多