Creating sticky select form field using $_GET[] details.

Posted on 2006-05-01
Last Modified: 2008-02-01
Probably quite simple but I cant seem to get this working.

I just want the select field to pre-select the $_GET[s_display] value when this is present.

Any help is much appriciated.


<select name="s_display">';
   $display_number = array (2,5,10,20,30,50,100);
    foreach ($display_number as $key => $value) {
      echo "<option value=\"$key\"";
      if (isset($_GET['s_display']))  {
      echo "selected=\"selected\"";
      echo ">$value</option>\n";
Question by:aegirbrands
    LVL 11

    Accepted Solution

    Hello aegirbrands,

    if you post this then one ove the options will be selected but s_display will be set in ANY case where an ANY option is selected so you must issolate this to maje this work for you. Also if this a form which is POSTed then the $_GET will yield  nothing useful.  

    <select name="s_display">
    $display_number = array (2,5,10,20,30,50,100);
    foreach ($display_number as $key => $value) {
    // lets use request instead of get as I am not sure where the form vars are coming from.
      $s_display = (isset($_REQUEST['s_display']))?$_REQUEST['s_display']:"";
      echo "<option value=\"$key\"";
      if ( $s_display != "" and   $s_display == $value   )  {
        echo " selected=\"selected\"";
      echo ">$value</option>\n";

    Joseph Melnick


    Author Comment

    Thank you, just what I needed.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now