首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在PHP中显示哪些表单元素已被选中

如何在PHP中显示哪些表单元素已被选中
EN

Stack Overflow用户
提问于 2009-07-01 14:47:47
回答 3查看 1.1K关注 0票数 0

如何让此代码显示已选中的选项?

这基本上是一个编辑页面,它从数据库中提取信息并填充相关字段

我有一个下拉菜单,多个选择框,一个页面上的单选按钮以及一些元素。信息在元素中显示很好,但我不知道如何让s和单选按钮显示为选中状态,如果它们与数据库中的信息相匹配。

代码:

代码语言:javascript
复制
<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>
EN

回答 3

Stack Overflow用户

发布于 2009-07-01 14:54:50

OPTION HTML Spec

将selected=" selected“更改为just selected。看起来该属性不需要赋值。

您可能还希望检查正在输出的HTML,以确保您的赋值计算为true。

票数 0
EN

Stack Overflow用户

发布于 2009-07-01 14:56:56

你可以使用javascript来做这件事。我的示例使用jquery

首先,给每个复选框一个id,这样

代码语言:javascript
复制
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就会是

代码语言:javascript
复制
$( "brand" ).attr( "checked", true ); // this would check the brand box

所以你可以根据需要写出这些。

票数 0
EN

Stack Overflow用户

发布于 2009-07-01 15:00:05

您应该--如果可能的话--将那组无线打印代码更改为for循环。然后你可以这样做:

代码语言:javascript
复制
foreach ($possibleRadios as $key => $val)
{ 
    echo '<input type="radio" name="' . $val->name . '" value="' . $val->id . '" ' .  ($isSelected($val->id) ? 'selected="selected' : '') . ' />$val->prettyName';
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1069566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档