通过复选框从模式中获取表中的数据

时间:2015-09-08 09:44:55

标签: javascript php jquery

我在将模式div中的表格中的数据插入表格元素时遇到问题,我想根据复选框选中的数据显示内容,当我点击"添加"按钮。

这是我的代码:

模态的表格代码:

<div class="row">
    <div class="modal fade" id="mod_search" role="dialog">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Members</h4>
                </div>

                <div class="modal-body">

                    <table class="table table-hover table-managed">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>Name</th>
                            <th>Speciality</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        $sql_search = "SELECT * FROM `personal_profile`";
                        $sql_run = mysql_query($sql_search);
                        while($m = mysql_fetch_array($sql_run))
                        {

                        ?>
                        <tr>
                            <td><input type="checkbox" name="member_select[]"></td>
                            <td><?php echo @$m['lastname'].', '.@$m['firstname'].' '.@$m['middlename'];?></td>
                            <td><?php echo @$m['speciality'];?></td>
                        </tr>
                        <?php
                        }
                        ?>
                        </tbody>
                    </table>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-success" name="add_button" id="button_add">Add</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
</div>

所选数据代码表:

<div class="row">
    <div class="form-group">
        <div class="col-md-8 col-md-offset-2">

            <table class="table table-managed table-hover">
                <thead>
                <tr>
                    <th>No.</th>
                    <th>Name</th>
                    <th>Speciality</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <?php
                    $sql_select="SELECT * FROM `event_attendees` WHERE `id`='".@$_REQUEST['member_select']."'";
                    $sql_run=mysql_query($sql_select);

                    while($row=mysql_fetch_array($sql_run))
                    {

                    }
                    ?>
                    <td></td>
                    <td id="name_row"></td>
                    <td id="speciality_row"></td>
                </tr>
                </tbody>
            </table>

        </div>
    </div>
</div> 
希望你能帮助我。我一直在互联网上看,但没有什么与我的问题完全相关。

以下是图片:

第一张图片是您点击&#34;搜索&#34;时显示的模式。如果&#34;会员&#34;从原始页面(第2张图像)中选择单选按钮。

Modal enter image description here

2 个答案:

答案 0 :(得分:0)

如果我已经理解你正在尝试做什么,在你点击'添加'时调用的页面中 - 我认为这是原始页面,图像2 - 在显示页面之前你需要在你的php中使用以下内容:

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

    $items=$_POST['member_select'];
    foreach($items AS $id){
        /*  do the following with your chosen SQL DB approach 
           (I would recommend looking at PDO):*/
        INSERT INTO `event_attendees` (name, specialism) 
            SELECT name, specialism FROM `personal_profile` WHERE `id`= $id
    }
}

错误检查等。

稍微有效的替代方案是:

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

    $items=$_POST['member_select'];
    $list='';
    foreach($items AS $id) $list.=$id.',';
    rtrim($list,',');
    /*  do the following with your chosen SQL DB approach 
        (I would recommend looking at PDO):*/
    INSERT INTO `event_attendees` (name, specialism) 
          SELECT name, specialism FROM `personal_profile` WHERE `id` IN ($list)          
}

答案 1 :(得分:0)

这就是我得到它的方式。 :)

//set the function
$("#add_button").click(function(){

/* check for your table where to send the data from the current table then
use the selector for the checkboxes when clicked */
    $('#table_select input[type="checkbox"]:checked').each(function(){

        var getRow = $(this).parents('tr'); //variable for the entire row
        var value = (getRow.find('td:eq(2)').html()); //data for names
        var value1 = (getRow.find('td:eq(3)').html()); //data for specialty
        var value3 = (getRow.find('td:eq(1)').html()); //data for prc


        $('#attendee_table tr:last').append('<tr><td></td><td><input type="text" class="form-control" name="prc[' + attendees_id + ']" id="prc" value="' + value3 + '" readonly></td><td><input type="text" class="form-control" value="' + value + '" id="names" name="name[' + attendees_id + ']" readonly></td><td><input type="text" class="form-control" value="' + value1 + '" id="specialties" name="specialty[' + attendees_id + ']" readonly></td><td><input type="button" class="btn btn-warning" name="delete_btn1[' + attendees_id + ']" id="delete_btn1" data-id="' + attendees_id + '" onclick="deleteMem(this)" value="Delete"></td></tr>');
      /* then append your row (include the variables to the value of the textboxes. 
I use textboxes for it may able to save the data in the database later. :) */

        $("#mod_search").modal('hide');

    });


});