Hi,

I have been trying to get a select menu to work for hours, and everything is working fine but for one little bit, everytime I select from the jump menu and load the same page again, the menu displays the first result that comes from the database instead of the user selection from the drop down.

Is there a way to get the selected option to display (using the code below)? The main portfolio.php page holds the url, and the following code is in an included file called dropdown.php

<select onChange="MM_jumpMenu('parent',this,0)" name="dropdown" id="dropdown"> 
    <?php
    while ($result = $query ->fetch_object()) {
    $dbid=$result->id;
    $portfolioname= $result->name;
    ?>

<option value="portfolios.php?pselect=<?php echo $portfolioname; ?>&albumid=<?php echo $dbid; ?>"><?php echo $portfolioname;   ?></option><?php
        }

    ?></select></span>

Any suggestions are much appereciated, and please let me know if you need further detail.

Thanks

Dani AI

Generated

Good call about adding a selected attribute. The actual bug in 's final snippet is the comparison against the whole $_GET array β€” that will never reliably match a single option. The fix is to compare the specific query key the page receives (for example albumid or pselect) to the same value used for each <option>, escape output, and cast types when appropriate.

A robust pattern is to use the numeric ID as the option value, do the redirect in the onchange, and mark the matching option with selected. Example:

<select id="dropdown" onchange="if(this.value) window.location.href='portfolios.php?albumid='+encodeURIComponent(this.value);">
<?php
while ($row = $query->fetch_object()) {
    $id   = (int)$row->id;
    $name = htmlspecialchars($row->name, ENT_QUOTES, 'UTF-8');
    $sel  = (isset($_GET['albumid']) && (int)$_GET['albumid'] === $id) ? ' selected' : '';
    echo "<option value=\"{$id}\"{$sel}>{$name}</option>\n";
}
?>
</select>

Notes and troubleshooting:

  • Prefer comparing $_GET['albumid'] (an integer) instead of the name string to avoid encoding/case issues. Cast values with (int) for safety.
  • If the URL must include the name, use rawurlencode() when building the link and compare the decoded GET value to a consistent canonical form.
  • Always use htmlspecialchars() for visible text to prevent XSS.
  • If the selection still shows wrong option, view the page source after navigation to confirm the selected attribute is present and that the expected query string is in the address bar.

Recommended Answers

All 2 Replies

If in your while loop you know which one is the one you want to display add 'selected' to the <option> like so:

<option value="..." selected>

For which you can use the short if-else:

<option value="..."<?php echo ($selected) ? " selected" : ""; ?>>

Thanks, got it working eventually!

<?php
while ($result = $query ->fetch_object()) {
$dbid=$result->id;
$portfolioname= $result->name;
?>

<option value="portfolios.php?pselect=<?php echo $portfolioname; ?>&albumid=<?php echo $dbid; ?>" <?php if ($_GET == $portfolioname){ echo "selected";} ?>>

<?php echo $portfolioname; ?></option>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.