Change HTML DropDown Default Value with a MySQL va

2019-08-31 09:48发布

问题:

I'm working on a profile page, where a registered user can update their information. Because the user has already submitted their information, I would like their information from the database to populate my HTML form.

Within PHP, I'm creating the HTML form with the values filled in. However, I've tried creating an IF statement to determine whether an option is selected as the default value. Right now, my website is giving me a default value of the last option, Undeclared. Therefore, I'm not sure if all IF statements are evaluation as true, or if it is simply skipping to selected=selected.

Here is my HTML, which is currently embedded with 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;

?>

回答1:

You seem to be missing any tags in your code, which means that nothing is being processed. Something more along the lines of this should be used:

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


回答2:

You've mixed HTML and PHP without declaring PHP tags and you've also used 'selected' as an attribute...

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

    ....

</select>

I've done the first one, but you can follow the same pattern



回答3:

That code looks like it could use a 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>

You might find some of this reading useful to help explain some of the concepts used above:

  • arrays
  • foreach loop
  • ternary operator (?:)
  • printf()
  • htmlentities()


标签: php forms