2014-02-22 47 views
1

我想使它選擇框用戶選擇保持選擇包裝驗證失敗後在我的形式其餘的表單工作完美,但我似乎無法數字如何對於選擇框做到這一點不改變所有的名字能有人幫我創造了這個包含只是我的表單複選框,我將在稍後做出相應的動作,所以你可以看到我需要如何選擇用戶選中的複選框如果幾個具有相同的名稱php

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

您可以使用[** **會話(http://www.php.net/manual/en/features.sessions.php)對於這一點,那麼只需使用條件語句並對其進行回顯即可。 –

回答

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

三江源我使用然後使用此第2方法來呼應輸出做了之後 如果(isset($ dessgroup)){ \t \t \t \t \t \t的foreach($ dessgroup爲$沙漠) \t \t \t { \t \t \t \t echo $ deserts,','; \t \t \t} \t \t \t \t \t \t} – user3341569

0

試試這個

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