Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Thumbs down setting the default value of a list/menu depending on a GET value

    Hi all,

    ok i am passing two values via the get method to another page and i would like to know if it is possible to set the default of a menu/list box to match the value of one of the GET values.

    e.g
    my list has the following values
    entertainment
    DVD
    CD
    BOOKS
    BLU-RAY
    VIDEO GAMES

    and my GET value lets say is Books, how do i make the list value set to books when the page loads instead of its default value?

    here is my code
    page 1 => sending the values to page 2
    PHP Code:
    <a href="http://www.kernow-connect.com/price_results.php?title=<?php print $short_title ?>&amp;category=<?php print $category ?>">
    page 2 => getting the values
    PHP Code:
    if(isset($_GET['title'])){
    $searchterm htmlentities($_GET['title']);
    $inputtext htmlentities($_GET['title']);
    }
    else {
    $inputtext "Type Here To Search and Compare";
    }

    if(isset(
    $_GET['category'])){
    $category htmlentities($_GET['category']);
    }
    else {
    $category "Everything";

    page 2 => input box and listbox code
    Code:
        <div class="searchbar">
          <input name="searchbox" 
        	type="text" 
        	value="<?php print stripslashes($inputtext); ?>" 
        	size="55" 
        	onfocus="clearText(this)" 
        	onblur="clearText(this)"/>
          <label>
          <select name="category" id="category">
            <option selected="selected">Entertainment</option>
            <option value="DVD">DVD</option>
            <option value="CD">CD</option>
            <option value="Video Games">Video Games</option>
            <option value="Blu-Ray">Blu-Ray</option>
            <option value="Books">Books</option>
                            </select>
          </label>
        </div>
    hope this helps

    thanks
    Luke
    Last edited by LJackson; 05-11-2009 at 01:47 PM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    <select name="category" id="category">
    <option selected="selected">Entertainment</option>
    <option value="DVD">DVD</option>
    <option value="CD">CD</option>
    <option value="Video Games">Video Games</option>
    <option value="Blu-Ray">Blu-Ray</option>
    <option value="Books">Books</option>
    </select>
    PHP Code:
    <select name="category" id="category">
    <?php
    $options
    =array('Entertainment','DVD','CD','Video Games''Blu-Ray''Books');
    foreach(
    $options as $option){
     if(isset(
    $_GET['title']) && $_GET['title']==$option)
      echo 
    "<option value=\"{$option}\" selected=\"selected\">{$option}</option>";
     else
      echo 
    "<option value=\"{$option}\">{$option}</option>";
    }

    ?>
    </select>
    (Untested, and there may be some issue with the option having a blank space in the name.)
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    LJackson (05-11-2009)

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    thank you mate, made a little tweak but its working

    cheers
    Luke

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    hi mate,

    ok i have been given some code which enabled me to make sections within my listbox which i have tried to implement but i am getting >" on a seperate line before each of my headers and im not sure why?

    here is my code

    PHP Code:
    <select name="category" id="category">
    <?php
    $options
    =array('Everything','<optgroup label="Entertainment">','DVD','CD','Video Games','Blu-Ray','Books','<optgroup label="Computing">','Desktop','Notebook','Netbook','Prnter','Scanner','Software','Accessories','<optgroup label="Electronics">''LCD TVs','Plasma TVs','Full HD TVs','MP3 and Digital Audio Players','Digital Cameras',' Home Cinema Systems','DVD Players');
    foreach(
    $options as $option){
     if(isset(
    $_GET['category']) && $_GET['category']==$option)
      echo 
    "<option value=\"{$option}\" selected=\"selected\">{$option}</option>";
     else
      echo 
    "<option value=\"{$option}\">{$option}</option>";
    }
    ?>      </select>
    the <optgroup label= is the headers/sections any idea why this isnt working?

    thanks

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    You can't use the code as such for generating <optgroup> tags. Check your parsed output from browsers view source option.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    hi mate,

    my source code is
    Code:
    <select name="category" id="category">
    <option value="Everything">Everything</option><option value="<optgroup label="Entertainment">"><optgroup label="Entertainment"></option><option value="DVD">DVD</option><option value="CD">CD</option><option value="Video Games">Video Games</option><option value="Blu-Ray">Blu-Ray</option><option value="Books">Books</option><option value="</optgroup>"></optgroup></option><option value="<optgroup label="Computing">"><optgroup label="Computing"></option><option value="Desktop">Desktop</option><option value="Notebook">Notebook</option><option value="Netbook">Netbook</option><option value="Prnter">Prnter</option><option value="Scanner">Scanner</option><option value="Software">Software</option><option value="Accessories">Accessories</option><option value="<optgroup label="Electronics">"><optgroup label="Electronics"></option><option value="LCD TVs" selected="selected">LCD TVs</option><option value="Plasma TVs">Plasma TVs</option><option value="Full HD TVs">Full HD TVs</option><option value="MP3 and Digital Audio Players">MP3 and Digital Audio Players</option><option value="Digital Cameras">Digital Cameras</option><option value=" Home Cinema Systems"> Home Cinema Systems</option><option value="DVD Players">DVD Players</option>      </select>
    which shows the extra ">'s but why? and how do i remove them?

    thank you

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    which shows the extra ">'s but why? and how do i remove them?
    <option value="Everything">Everything</option><option value="<optgroup label="Entertainment">
    Verify the usage of optgroup tag from http://htmldog.com/reference/htmltags/optgroup/

    You'd need to use an associative array(2D), with your optgroup labels as keys and their corresponding options as the value(inside another array), like
    PHP Code:
    $optgroups=array('Entertainment'=>array('DVD','CD','Video Games''Blu-Ray''Books'),
                    
    'Computing'=>array('Desktop','Notebook','Netbook','Prnter','Scanner','Software','Accessories'),
                    
    'Electronics'=>array('LCD TVs','Plasma TVs','Full HD TVs','MP3 and Digital Audio Players','Digital Cameras',' Home Cinema Systems','DVD Players')); 
    Then use an extra loop to traverse the array to echo the options.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    hi mate,

    i have this
    PHP Code:
    $optgroups=array('Entertainment'=>array('DVD','CD','Video Games''Blu-Ray''Books'),
                    
    'Computing'=>array('Desktop','Notebook','Netbook','Prnter','Scanner','Software','Accessories'),
                    
    'Electronics'=>array('LCD TVs','Plasma TVs','Full HD TVs','MP3 and Digital Audio Players','Digital Cameras',' Home Cinema Systems','DVD Players')); 
    foreach(
    $optgroups as $option){
     if(isset(
    $_GET['category']) && $_GET['category']==$option)
      echo 
    "<option value=\"{$option}\" selected=\"selected\">{$option}</option>";
     else
      echo 
    "<option value=\"{$option}\">{$option}</option>";

    but all im getting is array listed 3 times, how do i loop through the arrays to get each of the values?

    thanks
    Luke

  • #9
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Try
    PHP Code:
    <select name="category" id="category">
    <option value="Everything">Everything</option>
    <?php
    $optgroups
    =array('Entertainment'=>array('DVD','CD','Video Games''Blu-Ray''Books'),
                    
    'Computing'=>array('Desktop','Notebook','Netbook','Prnter','Scanner','Software','Accessories'),
                    
    'Electronics'=>array('LCD TVs','Plasma TVs','Full HD TVs','MP3 and Digital Audio Players','Digital Cameras',' Home Cinema Systems','DVD Players'));
    foreach(
    $optgroups as $key=>$options){
     echo 
    "<optgroup label=\"$key\">";
          foreach(
    $options as $option){
             if(isset(
    $_GET['title']) && $_GET['title']==$option)
                  echo 
    "<option value=\"{$option}\" selected=\"selected\">{$option}</option>";
             else
                  echo 
    "<option value=\"{$option}\">{$option}</option>";
            }
     echo 
    "<optgroup>";
    }

    ?>
    </select>
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    hi mate that works well, the only problem is that now the default value is not being set depending on the item varible which is passed from the other page?

    not sure why as in your code it says if it is set then set the default value to that.

    any ideas
    Cheers

  • #11
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok sorted it replaced
    PHP Code:
     if(isset($_GET['title']) && $_GET['title']==$option
    with
    PHP Code:
     if(isset($_GET['category']) && $_GET['category']==$option

    thanks for your help mate
    appreciate it

    Luke


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •