编写用于选中复选框的PHP代码,通过javascript动态添加

时间:2017-06-24 18:46:05

标签: javascript php

我有一个如下代码来制作表格:

    echo("<script type='text/javascript'>

var table = document.getElementById('show_member');

// Create an empty <tr> element and add it to the 1st position of the table:
var row = table.insertRow(i);
i = i+1;
// Insert new cells (<td> elements) at the 1st and 2nd position of the new <tr> element:
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
var cell6 = row.insertCell(5);
var cell7 = row.insertCell(6);
var cell8 = row.insertCell(7);
var cell9 = row.insertCell(8);
var cell10 = row.insertCell(9);
var cell11 = row.insertCell(10);

// Add some text to the new cells:
cell1.innerHTML = '$name';
cell2.innerHTML = '$name2';
cell3.innerHTML = '$lastname';
cell4.innerHTML = '$lastname2';
cell5.innerHTML = '$mellicode';
cell6.innerHTML = '$estekhdamnum';
cell7.innerHTML = '$email';
cell8.innerHTML = '$username';
cell9.innerHTML = '$password';
cell10.innerHTML = '$id';
var checkbox = document.createElement('INPUT');
***checkbox.type = 'checkbox';
checkbox.name = 'mark[]';
checkbox.value = i-1;
cell11.appendChild(checkbox);***
</script>");

和一个按钮如下:

<div class="container">
    <input type="button" class="form-control" name = "karbar" id ="check" value="افزودن به لیست کاربران"onclick="add_karbar1()"/><br><br>
</div>

以下是我更新表格的PHP代码

<?php
if(isset($_POST['karbar'])){
    //echo "<script>alert('was selected')</script>";
    foreach($_REQUEST['mark'] as $value){
        echo "<script>alert('$value was selected');</script>";
    }
}



?>

它给了我这个错误,为什么?

  

注意:未定义的索引:在第236行的E:\ Xampp_installed \ htdocs \ card \ add_karbar.php中标记

     

警告:在第23行的E:\ Xampp_installed \ htdocs \ card \ add_karbar.php中为foreach()提供的参数无效

如何识别已选中的复选框并为每行中的每个选中复选框执行php代码?

感谢

1 个答案:

答案 0 :(得分:0)

您的问题可能是由***引起的javascript语法错误,该错误不允许脚本到达checkbox.name = 'mark[]';行。如果该行未执行,则mark中将不会有$_REQUEST变量。变化

***checkbox.type = 'checkbox';
checkbox.name = 'mark[]';
checkbox.value = i-1;
cell11.appendChild(checkbox);***

checkbox.type = 'checkbox';
checkbox.name = 'mark[]';
checkbox.value = i-1;
cell11.appendChild(checkbox);

看看它是否有效