0

我在理解如何使 foreach 函数工作时遇到问题。我有一个表格,它使用复选框。我尝试将它们加载为复选框数组,但我无法让 foreach 函数为我工作。我不完全理解这一切。让我快速发布我的表单数据:

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

到目前为止,这是我的 php 处理器..:

<?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.';
?>

我知道,不多,因为我被困住了。我在敲我的头。我需要将其解析出来,并检查填充的值,以便将数据输入数据库。看起来很简单,但由于某种原因我无法理解!

4

1 回答 1

1

编辑后,我建议您提供所有复选框name='check[]',并且在您的 php 代码中,您将获得签入的所有输入值,$_POST['check']而无需尝试手动执行,此代码将回显所有检查值:

<?php
if(!empty($_POST['check'])) {
    foreach($_POST['check'] as $check) {
            echo $check.'<br/>'; //echo checked value
    }
}
?>

第一个答案:

要使用 php 最小化您的 HTML 代码,请使用modulus(除以的$a % $b模余数)在每 4 个输入之前显示元素$a$b<tr>

尝试这个 :

<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>
于 2013-09-03T21:46:02.010 回答