I am having a problem understanding how to make the foreach function work. I have a form, that uses check boxes. I tried loading them as an array of check boxes, but I couldn't get the foreach function to work for me. I don't properly understand all of this. Let me post my form data real quick:
<form method="post" action="interests.php" name="interests">
<table width="500" cellspacing="3" cellpadding="3" bgcolor="#FF80FF" bordercolor="#800000">
<tr>
<td><input type="checkbox" name="check1" value="1"></td>
<td><input type="checkbox" name="check2" value="2"></td>
<td><input type="checkbox" name="check3" value="3"></td>
<td><input type="checkbox" name="check4" value="4"></td>
</tr>
<tr>
<td><input type="checkbox" name="check5" value="5"></td>
<td><input type="checkbox" name="check6" value="6"></td>
<td><input type="checkbox" name="check7" value="7"></td>
<td><input type="checkbox" name="check8" value="8"></td>
</tr>
<tr>
<td colspan="2"><input type="submit"></td>
<td colspan="2"><input type="reset"></td>
</tr>
</table>
</form>
Here is my php processor so far..:
<?php
for(x=1;x>8;x++){
$loop=
echo 'Your interests have been updated into the database, and those files will now</br>';
echo 'begin showing up in the files area on your files sections.';
?>
I know, not much, because I am stuck. I am banging my head. I need to parse this out, and check for values filled, so I can input the data into a database. Seems simple enough, but for some reason I cant get it!
After your edit , i suggest to give all your checkbox name='check[]'
and in your php code you will get all values of inputs that are checked in $_POST['check']
without trying to do it manually,this code will echo all checked values :
<?php
if(!empty($_POST['check'])) {
foreach($_POST['check'] as $check) {
echo $check.'<br/>'; //echo checked value
}
}
?>
First answer :
To minimize your HTML code using php use modulus
($a % $b
Modulus Remainder of $a
divided by $b
) to show <tr>
element before every 4 inputs
Try this :
<form method="post" action="interests.php" name="interests">
<table width="500" cellspacing="3" cellpadding="3" bgcolor="#FF80FF" bordercolor="#800000">
<?php
for($i=1;$i<=8;$i++){
if (($i-1)%4==0) echo "<tr>";
echo "<td><input type='checkbox' name='check$i' value='$i'/></td>";
}
?>
<tr>
<td colspan="2"><input type="submit"></td>
<td colspan="2"><input type="reset"></td>
</tr>
</table>
</form>