【问题标题】:Change HTML DropDown Default Value with a MySQL value使用 MySQL 值更改 HTML DropDown 默认值
【发布时间】:2011-11-28 17:38:10
【问题描述】:

我正在处理个人资料页面,注册用户可以在其中更新他们的信息。因为用户已经提交了他们的信息,我希望他们的信息从数据库中填充到我的 HTML 表单中。

在 PHP 中,我正在创建填充了值的 HTML 表单。但是,我尝试创建一个 IF 语句来确定是否选择了一个选项作为默认值。现在,我的网站给了我最后一个选项的默认值,未声明。因此,我不确定是否所有 IF 语句都评估为 true,或者它是否只是跳到 selected=selected。

这是我的 HTML,目前嵌入了 PHP(<?php ?>):

<?php

// Connect to MySQL
$db = mysql_connect("", "xxx", "xxx");

if (!$db)
{
    exit("Error - Could not connect to MySQL");
}

$er = mysql_select_db("cs329e_fall11_nemo008", $db);

if (!$er)
{
    exit("Error - Could not select the database");
}

$query = "SELECT *
FROM tblMembers
WHERE Username = 'fzr11017' ";

$result = mysql_query($query);

if (!$result)
{
    print("Error - The query could not be executed");
    $error = mysql_error();
    print("<p>:" . $error . "</p>");
    exit;
}

$row = mysql_fetch_array($result);

print <<<FORM

<form id="frmRegister" action="update.php" method="post" onsubmit="return Validate()" >
<table>
    <tr>
        <th><br /></th>
        <td><br /></td>
    </tr>
    <tr>
        <th align="left">Username:</th>
        <td><input type="text" name="Username" maxlength="10" value=$row[Username] readonly="readonly"/></td>
    </tr>
    <tr>
        <th align="left">First Name:</th>
        <td><input type="text" name="FirstName" value=$row[FirstName] readonly="readonly" /></td>
    </tr>
    <tr>
        <th align="left">Last Name:</th>
        <td><input type="text" name="LastName" value=$row[LastName] readonly="readonly" /></td>
    </tr>
    <tr>
        <th align="left">Email Address:</th>
        <td><input type="text" name="Email" value=$row[Email] /></td>
    </tr>
    <tr>
        <th align="left">Phone Number:</th>
        <td><input type="text" name="Phone" maxlength="10" value=$row[Phone] /></td>
    </tr>
    <tr>
        <th align="left">Year:</th>
        <td>
            <select name="Year" >
                <option if(strcmp($row[Year], 'Freshman') == 0){ selected="selected"} >Freshman</option>
                <option if(strcmp($row[Year], 'Sophomore') == 0){ selected="selected"} >Sophomore</option>
                <option if(strcmp($row[Year], 'Junior') == 0){ selected="selected"} >Junior</option>
                <option if(strcmp($row[Year], 'Senior') == 0){ selected="selected"} >Senior</option>
            </select>
        </td>
    </tr>
    <tr>
        <th align="left">Primary Major:</th>
        <td>
            <select name="Major">
                <option if($row[Major] == Accounting){ selected="selected"}>Accounting</option>
                <option if($row[Major] == Business Honors Program){ selected="selected"}>Business Honors Program</option>
                <option if($row[Major] == Engineering Route to Business){ selected="selected"}>Engineering Route to Business</option>
                <option if($row[Major] == Finance){ selected="selected"}>Finance</option>
                <option if($row[Major] == International Business){ selected="selected"}>International Business</option>
                <option if($row[Major] == Management){ selected="selected"}>Management</option>
                <option if($row[Major] == Management Information Systems){ selected="selected"}>Management Information Systems</option>
                <option if($row[Major] == Marketing){ selected="selected"}>Marketing</option>
                <option if($row[Major] == MPA){ selected="selected"}>MPA</option>
                <option if($row[Major] == Supply Chain Management){ selected="selected"}>Supply Chain Management</option>
                <option if($row[Major] == Undeclared){ selected="selected"}>Undeclared</option>
            </select>
        </td>
    </tr>
    <tr>
        <th><br /></th>
        <td><br /></td>
    </tr>
    <tr>
        <td align="center"><input type="submit" name="btnSubmit" value="Submit" /></td>
        <td align="center"><input type="reset" value="Reset" /></td>
    </tr>
</table>
</form>
FORM;

?>

【问题讨论】:

    标签: php forms


    【解决方案1】:

    您在没有声明 PHP 标记的情况下混合了 HTML 和 PHP,并且还使用了 'selected' 作为属性...

    <select name="Major">
        <option <?php echo ($row['Major'] == "Accounting") ? " selected" : ""; ?>>Accounting</option>
    
        ....
    
    </select>
    

    我已经完成了第一个,但你可以遵循相同的模式

    【讨论】:

      【解决方案2】:

      该代码看起来可以使用towel

      <select name="Major">
      <?php
        $options = array(
            'Accounting'
          , 'Business Honors Program'
          , 'Engineering Route to Business'
          , 'Finance'
          , 'International Business'
          , 'Management'
          , 'Management Information Systems'
          , 'Marketing'
          , 'MPA'
          , 'Supply Chain Management'
          , 'Undeclared'
        );
      
        foreach( $options as $option )
        {
          printf(
            "<option%s>%s</option>\n"
              , ($row['Major'] == $option ? ' selected="selected"' : '')
              , htmlentities($option)
          );
        }
      ?>
      </select>
      

      您可能会发现本文中的一些内容有助于解释上面使用的一些概念:

      【讨论】:

        【解决方案3】:

        您的代码中似乎缺少任何标记,这意味着没有处理任何内容。应该使用更多类似的东西:

        <option <?php if($row["Major"] == "Accounting"){ echo "selected"; } ?>>Accounting</option>
        

        【讨论】:

        • 我的解决方案的一个小变种
        • 目前,整个表单都在 PHP 标记下,这就是为什么我的变量 $row[Major] 是从 MYSQL 调用的。我是否需要放置嵌套的 PHP 标记才能使 IF 语句起作用?
        • HTML 和 PHP 是两个完全不同的域。不得将 HTML 放在 PHP 标记中,反之亦然。
        • 这是有道理的。我所做的是开始使用 PHP 并打印
        • 这种解决方案在下拉菜单中是否可行?
        猜你喜欢
        • 2019-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-12
        • 2021-11-19
        • 2014-05-16
        • 1970-01-01
        • 2010-12-18
        相关资源
        最近更新 更多