如何选择用户选中的复选框如果有几个同名的PHP

时间:2014-02-22 19:56:06

标签: php html checkbox

我想让它如此选择框用户选择保持选择包装验证失败以后在我的表单中其余的表单工作完美但我似乎无法想象如何为选择框执行此操作而不更改所有名称可以某人请帮助我创建了这个,其中只包含我的表单复选框以及稍后我将要执行的操作,以便您可以看到我需要的内容

    <form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="desert_l">
           <label for="hnumber"><div class="bookerror"></div>Desert Choice </label>
          </div>
<div id="desert_f">
  <table width="275">
              <tr>
                <td width="113">
                <label>
<input type="checkbox" name="desgroup[]" value="Vanila Cheesecake"<?php if (isset($dessgroup) && ($dessgroup == "Vanila Cheesecake"))
 {
    echo ' checked="checked""';
  }
 ?>  />

                  Vanilla Cheesecake</label></td>
                <td width="94"><input type="checkbox" name="desgroup[]" value="Carrot Cake"<?php if (isset($dessgroup) && ($dessgroup == "Carrot Cake"))
 {
    echo ' checked="checked""';
  }
 ?> />
Carrot Cake</td>
                <td width="52"><input type="checkbox" name="desgroup[]" value="Jelly"<?php if (isset($dessgroup) && ($dessgroup == "Jelly"))
 {
    echo ' checked="checked""';
  }
 ?> />
Jelly</td>
              </tr>
              <tr>
                <td><label>
                  <input type="checkbox" name="desgroup[]" value="Fruit Cheesecake"<?php if (isset($dessgroup) && ($dessgroup == "Fruit Cheesecake"))
 {
    echo ' checked="checked""';
  }
 ?> >
                  Fruit Cheesecake</label></td>
                <td><input type="checkbox" name="desgroup[]" value="Fruit Flan"<?php if (isset($dessgroup) && ($dessgroup == "Fruit Flan"))
 {
    echo ' checked="checked""';
  }
 ?> />
Fruit Flan</td>
                <td>&nbsp;</td>

              </tr>
              <tr>
                <td><label>
                  <input type="checkbox" name="desgroup[]" value="Fruit Platter"<?php if (isset($dessgroup) && ($dessgroup == "Fruit Platter"))
 {
    echo ' checked="checked""';
  }
 ?> />
Fruit Platter</label></td>
                <td><input type="checkbox" name="desgroup[]" value="Chocolate Cake"<?php if (isset($dessgroup) && ($dessgroup == "Chocolate Cake"))
 {
    echo ' checked="checked""';
  }
 ?>/>
Chocolate Cake</td>
           <td>&nbsp;</td>
              </tr>
  </table>
  <p>
    <input type="submit" name="submit" id="submit" value="Submit" />
    </p>
  <p><span class="bookerror">
<?php
$dessgroup = $_POST['desgroup'] ;
if (isset ($_POST['submit']))
{
  if (isset ($dessgroup)) 
  {
     echo "<h2>Desert Choice</h2>";

  // Show Deserts Selected
    for($i=0; $i < count($dessgroup); $i++)
    {
    echo $dessgroup[$i]."</br>";
    }}
   else 
   {
       echo "You Must Select At Least 1 Desert" ;
   }

}
    ?> 
  </span></p>
  <p>Please Choose The Number Specified In Your Menu Choice<br />
</p>
  </div>
  </form>

2 个答案:

答案 0 :(得分:0)

而不是

<input type="checkbox" name="desgroup[]" value="Vanila Cheesecake"<?php if (isset($dessgroup) && ($dessgroup == "Vanila Cheesecake"))
 {
    echo ' checked="checked""';
  }
 ?>  />

<input type="checkbox" name="desgroup[]" value="Vanila Cheesecake"<?php if (isset($_POST['desgroup']) && is_array($_POST['desgroup']) && in_array("Vanila Cheesecake", $_POST['desgroup']))
 {
    echo ' checked="checked""';
  }
 ?>  />

如果您因此使用数组,它会为您节省大量的打字。

$arr = array('possible value 1', 'possible value 2');
foreach ($arr as $val) {
    ?>
    <input type="checkbox" name="desgroup[]" value="<?php echo $val; ?>"<?php if (isset($_POST['desgroup']) && is_array($_POST['desgroup']) && in_array($val, $_POST['desgroup']))
     {
        echo ' checked="checked""';
      }
     ?>  />
    <?php
}

答案 1 :(得分:0)

试试这个

<input type="checkbox" name="desgroup[]" value="Vanila Cheesecake" <?php echo ((isset($dessgroup) && ($dessgroup == "Vanila Cheesecake")))?'checked="checked"':''; ?> />