0

Below is the code for a test I have been running.

<!DOCTYPE HTML">
<html>
<head>
      <title>Test Page</title>
</head>
<body>
      <form class="form-horizontal" method = "POST">
            <fieldset>
                   <table  class = "table table-striped table-sm">                 
                        <thead>
                            <tr> 
                                 <th style="width: 2%"> </th>
                                 <th style="width: 18%">Name</th>
                                 <th style="width: 6%"> W1 </th>
                                 <th style="width: 6%"> W1 </th>
                                 <th style="width: 6%"> W1 </th>
                                 <th style="width: 6%"> W1 </th>
                                 <th style="width: 6%"> W1 </th>
                                 <th style="width: 6%"> W1 </th>
                            </tr>
                        </thead>
                        <tbody>       
                               <tr>
                                   <td><input type="hidden" name="memberid[]" value="1063" /></td> 
                                   <td>Charlie Farnsworth</td> 
                                   <td align="center"><input type="checkbox" name="test[]" value = "0"  ></td>  
                                   <td align="center"><input type="checkbox" name="test[]"  value = "1" ></td>  
                                   <td align="center"><input type="checkbox" name="test[]"  value = "2" ></td>  
                                   <td align="center"><input type="checkbox" name="test[]"  value = "3" ></td>  
                                   <td align="center"><input type="checkbox" name="test[]"  value = "4" ></td>  
                                   <td align="center"><input type="checkbox" name="test[]"  value = "5" ></td>  
                               </tr>

                        </tbody>       
                   </table>
            </fieldset>
            <div align="center">
                <br /><br />
                <input type="submit" id= "submit" name = "submit" value="Save changes">
                <br /><br />
            </div>
      </form>
</body>
</html>

The PHP code is

<?php 
if (isset($_POST)) {
   $return = '000000';
   if (!empty($_POST['test'])) { 
     for ($x = 0; $x < 6; $x++) {
        if(IsChecked('test',$x)){
          $return=substr_replace($return,"1",$x,1);          } 
        }
     } 
     $return = implode(',',str_split($return));
     echo $_POST['memberid'].'<br/>';
     echo $return.'<br/><br/>';
 }
 unset($_POST);
}

function IsChecked($chkname,$value) {
    if(!empty($_POST[$chkname])){
        foreach($_POST[$chkname] as $chkval){
            if($chkval == $value){
                return true;
            }
        }
    }
    return false;
}

?>                 

This is to allow me to build a string of 1s and 0s based on the checkbox states . This will be used to update a SQL table record having an id field of the value of the hidden input.

This works fine for a single row of data, but I am at a loss of how to do it if there are several rows of data.

I would have thought a foreach construct might work, but so far have been unsuccessful.

miken32
  • 42,008
  • 16
  • 111
  • 154
OldGuy
  • 9
  • 1
  • 6

1 Answers1

0

When you build your table, include the memberid value for each checkbox

<tr>
<td><input type="hidden" name="memberid[]" value="1063" /></td> 
<td>Charlie Farnsworth</td> 
<td align="center"><input type="checkbox" name="test[1063][]" value = "0"  ></td>  
<td align="center"><input type="checkbox" name="test[1063][]"  value = "1" ></td>  
<td align="center"><input type="checkbox" name="test[1063][]"  value = "2" ></td>  
<td align="center"><input type="checkbox" name="test[1063][]"  value = "3" ></td>  
<td align="center"><input type="checkbox" name="test[1063][]"  value = "4" ></td>  
<td align="center"><input type="checkbox" name="test[1063][]"  value = "5" ></td>  
</tr>

then refer it by

foreach ($_POST['memberid'] as $memberid){
    foreach ($_POST['test'][$memberid] as $checkedValue){
          // do your stuff...
    }
}
Raymond Tey
  • 464
  • 3
  • 11
  • I have tried that - inserting it after the if (isset($_POST) { line, but am getting a "Parse error: syntax error, unexpected ')', expecting '(' " on the "foreach ($_POST['memberid'] as memberid){" line. – OldGuy Oct 24 '18 at 08:46
  • The thanks for reply line disappeared from previous comment, so I have added it again! Have just opened file in Eclipse and it shows a slightly different error: syntax error, unexpected ')', expecting '[' – OldGuy Oct 24 '18 at 09:38
  • Solved the problem as follows: foreach ($_POST['memberid'] as $memberid){ but now getting errors on the foreach lines: Undefined index: memberid on first line and Invalid argument supplied for foreach() on second one. – OldGuy Oct 24 '18 at 10:03
  • Hi... edited to add $ making it a variable. Try do a print _r($checkedValue) to see the return structure – Raymond Tey Oct 24 '18 at 22:41
  • 1
    Sorted: Used the `` HTML format as @Raymond said, and the PHP code is:`if (isset($_POST)) { foreach ($_POST['memberid'] as $key=>$memberid){ $return = '000000'; foreach ($_POST['test'][$memberid] as $checkedValue){ $return=substr_replace($return,"1",$checkedValue,1); } $return = implode(',',str_split($return)); echo $memberid.'
    '; echo $return.'

    '; } unset($_POST); }`
    – OldGuy Oct 25 '18 at 08:37