2009-07-01 21 views
0

如何獲取此代碼以顯示哪個選項已被選中?如何顯示PHP中已經選擇了哪些表單元素

這基本上是一個編輯頁面,它是拉動從數據庫信息和填充相關領域

我有一個下拉菜單,多選框,並在頁面上的單選按鈕的一些元素一起。信息正在顯示在元素中,但我無法弄清楚如何讓s和單選按鈕顯示選中的內容,如果它們與數據庫中的信息匹配的話。

代碼:向剛選擇選定= 「選擇」

<select name="client"> 


    <option value="empty">Change Client...</option> 
<?php 
       $result2 = mysql_query("SELECT name FROM clients") or die("Database query failed: " . mysql_error());  

while($row = mysql_fetch_assoc($result2)) { 
    $clientlist = $row['name']; 
    $clientname = htmlspecialchars($row['name']); 

    if ($_POST['client'] == $clientlist) 
    { 

    echo '<option value="' . $clientlist . '" selected="selected" >' . $clientname . '</option>' . '\n'; 
    } 
    else{ 
    echo '<option value="' . $clientlist . '" >' . $clientname . '</option>' . '\n'; 
} 
} 


?> 
</select> 

    </p> 
<p class="subheadsmall">Core Classification</p> 

<?php 

switch ($niche) { 
    case "brand": 
     echo '<input type="radio" name="niche" value="Brand" checked="checked" />Brand'; 
     echo '<input type="radio" name="niche" value="Marketing" />Marketing'; 
     echo '<input type="radio" name="niche" value="Communication" />Communication'; 
     break; 
    case "marketing": 
     echo '<input type="radio" name="niche" value="Brand" />Brand'; 
     echo '<input type="radio" name="niche" value="Marketing" checked="checked" />Marketing'; 
     echo '<input type="radio" name="niche" value="Communication" />Communication'; 
     break; 
    case "communication": 
     echo '<input type="radio" name="niche" value="Brand" />Brand'; 
     echo '<input type="radio" name="niche" value="Marketing" />Marketing'; 
     echo '<input type="radio" name="niche" value="Communication" checked="checked" />Communication'; 
     break; 
    default; 
     echo '<input type="radio" name="niche" value="Brand" />Brand'; 
     echo '<input type="radio" name="niche" value="Marketing" />Marketing'; 
     echo '<input type="radio" name="niche" value="Communication" />Communication'; 
    break; 
} 

?> 

<p class="subheadsmall">Strategies</p> 

<p class="sidebargrey"> 

<?php 

      $result = mysql_query("SELECT strategies FROM studies WHERE id = '$id'; 
       if (!$result) { 
        die("Database query failed: " . mysql_error()); 
       } 

while($row = mysql_fetch_array($result)) { 
    $strategyname = $row['strategies']; 


    echo $strategyname.'<br />'; 
} 

?> 
     <p class="subheadsmall">Add a strategy... (hold down command key to select more than one)</p> 

<select name="strategies[]" multiple="multiple"> 
    <?php 

      $result = mysql_query("SELECT * FROM strategies"); 
       if (!$result) { 
        die("Database query failed: " . mysql_error()); 
       } 

while($row = mysql_fetch_array($result)) { 
    $strategylist = $row['name']; 
    $strategyname = htmlspecialchars($row['name']); 
$pagelink = str_replace(" ","_",$strategylist); 

    echo '<option value="<a href=&quot;strategies.php?strategy=' . $pagelink . '&quot;>'.$strategyname.'</a>" >' . $strategyname . '</option>' . '\n'; 
} 

?> 
    </p> 

回答

0

OPTION HTML Spec

變化。看起來這個屬性不需要分配。

您可能還想檢查輸出的HTML,以確保您的任務正在評估爲true。

0

您可以使用javascript來做到這一點。我的例子使用jQuery的

首先給每個複選框的ID,因此

echo '<input type="radio" name="niche" id="brand" value="Brand" />Brand'; 
echo '<input type="radio" name="niche" id="marketing" value="Marketing" />Marketing'; 
echo '<input type="radio" name="niche" id="communication" value="Communication" />Communication'; 

那麼你的JS是

$("brand").attr("checked", true); // this would check the brand box 

所以,你可以只寫這些需要。

+0

萬一OP不知道,這個JS需要jQuery的。 – 2009-07-01 15:07:07

0

您應該 - 如果可能 - 將該組無線打印改爲for循環。然後,你可以做這樣的事情:

foreach ($possibleRadios as $key => $val) 
{ 
    echo '<input type="radio" name="' . $val->name . '" value="' . $val->id . '" ' . ($isSelected($val->id) ? 'selected="selected' : '') . ' />$val->prettyName'; 
} 
相關問題