samedi 10 février 2018

Getting specific values of checkboxes from a form

I have a form set up like this :

<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 ?>

Then when the form is submited, I try this :

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

 }

}

The output of the form is this :

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

Which is not exactly what I wanted, since member 1 selected category 2 and member 8 selected category 1. But both members have both categories.

Visually it's this




Aucun commentaire:

Enregistrer un commentaire