从表单中获取复选框的特定值

时间:2018-02-10 20:58:09

标签: php html forms for-loop checkbox

我的表单设置如下:

<form method="post" id="FormEditEmailWarning">...
 <?php 
$q_u = mysqli_query(...)
while($r_u = mysqli_fetch_array(...)){
?>
<input name="warn_uid[]" value="<? echo $r_u['id_member']; ?>" type="checkbox" <? echo $warn; ?>>
.... some other codes....
// Then each listed user can select a category
// so another while...

$q_cat = mysqli_query(...)
while($r_cat = mysqli_fetch_array(...)){
?>
<input name="catid[]" value="<? echo $r_cat['id_cat']; ?>" type="checkbox" <? echo $warn_user_cat; ?>>
<input type="hidden" name="catid_member<? echo $r_u['id_member']; ?>[]" value="<? echo $r_u['id_member']; ?>">
<? } // END WHILE CATEGORY LISTING ?>

<? } // END WHILE USER LISTING ?>

然后当表单被提交时,我试试这个:

if(isset($_POST["warn_uid"])){

    for($i=0;$i<count($_POST["warn_uid"]);$i++){

        if(isset($_POST["warn_uid"][$i])){
            $warn_uid   = mysqli_real_escape_string($sqllink,$_POST["warn_uid"][$i]);
        }
        if( !empty($warn_uid) ){ 
            // I'M INSERTING THE USER IN THE DATABASE WORKS OK 

            // NOW I WANT TO INSERT THE SELECTED CATEGORIES FOR THAT SPECIFIC USER...
            // THIS IS WHERE I HAVE A PROBLEM

            for($j=0;$j<count($_POST["catid"]);$j++){
                if(isset($_POST["catid"][$j])){
                    $cat_id = mysqli_real_escape_string($sqllink,$_POST["catid"][$j]);
                }
                if( !empty($cat_id) ){
                    $cat_member =  mysqli_real_escape_string($sqllink,$_POST["catid_member$warn_uid"][$j]);
                }
                if($cat_member == $warn_uid){
                    // if my member is the same as the category member insert
                    // the problem is that it insert for all member the same categories

                }

            }

表格的输出是:

cat_member:1 cat_id:2 cat_member:1 cat_id:1 cat_member:1 cat_id:2 cat_member:8 cat_id:2 cat_member:8 cat_id:1 cat_member:8 cat_id:2

这并不是我想要的,因为成员1选择了第2类,成员8选择了第1类。但是这两个成员都有两个类别。

视觉上这是

0 个答案:

没有答案