如何在表行中保存多克隆输入

时间:2019-07-16 22:49:38

标签: php jquery html mysql

我在表格行中有表单输入,该行在添加新行按钮时被克隆。我试图在单击“保存”按钮时将所有行保存在MySQL中。

通过ajax将其发送到php时,我只能得到一个值,这是克隆前的默认值。

hmtl代码:

<table role="table" class="striped table_view rowfy" id="user_table"> 
    <tbody role="rowgroup">
    <tr id="template" role="row"> 

    <td role="cell">
    <select name="module" class="browser-default mselect select module">
    <option value="" class="validate" disabled selected>Select User Menu Module</option>  
    <?php echo modules(); ?>
    </select>
    </td>
    <td role="cell">
    <select name="create[]" class="browser-default mselect select create">
    <option value="" class="validate" disabled selected>Create</option>
    <option value="yes">No</option>
    <option value="no">Yes</option>
    </select>
    </td>
    <td role="cell">
    <select name="edit[]" class="browser-default mselect select edit">
    <option value="" class="validate" disabled selected>Edit</option>
    <option value="yes">No</option>
    <option value="no">Yes</option>
    </select>
    </td>
    <td role="cell">
    <select name="delete[]" class="browser-default mselect select delete">
    <option value=""  disabled selected>Delete</option>
    <option value="yes">No</option>
    <option value="no">Yes</option>
    </select>
    </td>
    <td role="cell">
    <select name="view[]" class="browser-default mselect select view">
    <option value=""  disabled selected>View</option>
    <option value="yes">No</option>
    <option value="no">Yes</option>
    </select>

    </td>

    </tr>
    </tbody>
    </table>

jquery:

$(document).ready(function() {
    var template = $('#template'),
        id = 0;

$("#add-line").click(function() {
    if(!template.is(':visible'))
    {
        template.show();
        return;
    }
    var row = template.clone().append('<td><button class="btn btn-small btn btn-floating '
  + ($(this).is(":last-child") ?
    'rowfy-addrow remove red">-' :
    'rowfy-deleterow remove waves-effect waves-light red">-') 
  +'</button></td>');
    //template.find(".mselect").val("");
    row.attr('id', 'row_' + (++id));
    template.after(row);
});

$('.form-fields').on('click', '.remove', function(){
    var row = $(this).closest('tr');
    if(row.attr('id') == 'template')
    {
        row.hide();
    }
    else
    {
        row.remove();
    }
});



});

php代码:

if(!empty($_POST['module'])){
$rowCount = count($_POST['module']);

for($i = 0; $i < $rowCount; $i++)
{       
$module = clean($_POST['module'][$i]);
$edit = clean($_POST['edit'][$i]);
$view = clean($_POST['view'][$i]);
$create = clean($_POST['create'][$i]);
$del = clean($_POST['delete'][$i]);

echo $rowCount;   
$sql = "INSERT INTO role_rights(rr_rolecode,rr_modulecode,rr_create,rr_edit,rr_delete,rr_view) 
VALUES('$role', '$module','$create','$edit','$del','$view')";

$suc =  mysqli_query($mysqli, $sql);  

}
}

下面是正在发送的数据的标题格式:

module: PAGES
create[]: yes
edit[]: no
delete[]: yes
view[]: no
module: CATEGORIES
create[]: no
edit[]: no
delete[]: yes
view[]: no
module: ATTRIBUTES
create[]: no
edit[]: no
delete[]: no
view[]: no

1 个答案:

答案 0 :(得分:1)

谢谢大家,我已经弄清楚了问题所在。

我没有在HTML模块中添加[]

之前:

<select name="module" class="browser-default mselect select module">
    <option value="" class="validate" disabled selected>Select User Menu Module</option>  
    <?php echo modules(); ?>
    </select>

之后

<select name="module[]" class="browser-default mselect select module">
    <option value="" class="validate" disabled selected>Select User Menu Module</option>  
    <?php echo modules(); ?>
    </select>