#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Drop down menu and hyperlinks in PHP


    Trying to hyperlink items in drop down menu. Menu items pulled from table fine. Hyperlinks won't work. Thanks.

    PHP Code:
    <?php

       $query 
    "SELECT catid, name from categories ORDER BY catid ASC";
       
    $result mysql_query($query);
       echo 
    "<tr><td>\n";

       echo 
    "<select>";
       
       while(
    $row=mysql_fetch_array($resultMYSQL_ASSOC))
       {
          
    $catid $row['catid'];
          
    $name $row['name'];

          echo 
    '<option onClick=\"'index.php?content=buyproducts&cat=$catid'\">$name</option>';

          echo 
    '<option value=\"'.$catid.'\">'.$name.'</option>';
       }
          echo 
    "</select>";
          echo 
    "</td></tr>\n";
          echo 
    "</tr>\n";

    ?>
    Last edited by requinix; August 16th, 2013 at 10:29 PM. Reason: [php] is for code, [phpnet] is for linking to the manual
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    186
    Rep Power
    84
    You can't make a hyperlink in a SELECT control. The look/layout of a SELECT is up to the browser designer. All you can give is the text to be displayed.
    I think what you really want is an onChange handler on the select control.
    The javascript for the onChange event will read the value of the OPTION selected and build the correct string for a window.open call.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0
    I agree. I just don't mnow the best way to handle it. Is javascript the answer or is there a better way with only php? And could you direct me to some sample code? Thx

    Originally Posted by BarryG
    You can't make a hyperlink in a SELECT control. The look/layout of a SELECT is up to the browser designer. All you can give is the text to be displayed.
    I think what you really want is an onChange handler on the select control.
    The javascript for the onChange event will read the value of the OPTION selected and build the correct string for a window.open call.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    There is no way to do this with PHP, since PHP is strictly server-side. You need to do it with javascript. See this for some ideas:

    http://stackoverflow.com/questions/1599140/how-to-create-a-dropdown-list-hyperlink-without-the-go-button

    Comments on this post

    • BarryG agrees

IMN logo majestic logo threadwatch logo seochat tools logo