...

View Full Version : Resolved setting the default value of a list/menu depending on a GET value



LJackson
05-11-2009, 02:33 PM
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

<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

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

<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

abduraooft
05-11-2009, 02:41 PM
<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>

<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.)

LJackson
05-11-2009, 02:47 PM
thank you mate, made a little tweak but its working :D

cheers
Luke

LJackson
05-19-2009, 12:49 PM
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


<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

abduraooft
05-19-2009, 12:53 PM
You can't use the code as such for generating <optgroup> tags. Check your parsed output from browsers view source option.

LJackson
05-19-2009, 12:59 PM
hi mate,

my source code is

<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

abduraooft
05-19-2009, 01:21 PM
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

$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.

LJackson
05-19-2009, 02:50 PM
hi mate,

i have this

$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

abduraooft
05-19-2009, 02:59 PM
Try

<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>

LJackson
05-19-2009, 03:08 PM
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

LJackson
05-19-2009, 03:11 PM
ok sorted it replaced

if(isset($_GET['title']) && $_GET['title']==$option)

with

if(isset($_GET['category']) && $_GET['category']==$option)


thanks for your help mate
appreciate it :)

Luke



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum