Page 1 of 1

DYNAMIC LISTBOX HELP

Posted: Mon Sep 28, 2009 7:54 am
by grantp22
Hi,

Can somebody please give me some advise on how to get the correct syntax for what I am trying to do, if it's even possible the way I have it...

This is what I am trying to do! I have a listbox with products the user can select, the user selects the item he wants, then hits the submit button, the value of the selected item gets saved to a mysql database as either "image1", "image2", etc, etc. When the form is reloaded, I would like to have the listbox value read back the written value of either "image1", "image2", etc from the database, and then use it to automatically select the correct listbox item when the form is loaded next time.

The onChange="loadproduct1(this) event for this listbox just calls a function which changes the images according to the selected item in the listbox, those image paths are then saved to the database as well! I have tried several ways of trying to manipulate the syntax I have below, but I just can't seem to find the correct syntax. If anybody knows or has seen something similar to what I am trying to do, could you please give me a hand on this - I am running out out ideas!

=========================================
This is what I am using, a normal listbox
=========================================

Code: Select all

<select size="1" name="product1" onChange="loadproduct1(this)" id="product1">
   <option value="Image1">iPhone 3G S - 16 Go                                    
   <option value="Image2">HTC Hero
   <option value="Image3">Canon EOS 500D Kit
   <option value="Image4">PlayStation 3
</select>

============================================================================
Below is what I would like to use, but I can't figure out the correct syntax..
============================================================================

Code: Select all

<select size="1" name="product1" onChange="loadproduct1(this)" id="product1">
   <option value="image1" <? if($form->value("product1") == 'image1'){ echo 'selected="selected"'; } ?>>iPhone 3G S - 16 Go
   <option value="image2" <? if($form->value("product1") == 'image2'){ echo 'selected="selected"'; } ?>>HTC Hero
   <option value="image3" <? if($form->value("product1") == 'image3'){ echo 'selected="selected"'; } ?>>Canon EOS 500D Kit
   <option value="image4" <? if($form->value("product1") == 'image4'){ echo 'selected="selected"'; } ?>>PlayStation 3
</select>
Any help on this will be very much appreciated...

Thanks
Grant

Re: DYNAMIC LISTBOX HELP

Posted: Mon Sep 28, 2009 8:19 am
by papa
I usually loop my values and check if it matches.

Something like this:

Code: Select all

 
 
<select size="1" name="product1" onChange="loadproduct1(this)" id="product1">
<?php
$data = array('image1'=>'iPhone 3G S - 16', 'image2'=>'HTC Hero', 'image3'=>'Canon EOS 500D', 'image4'=>'PlayStation 3');
 
foreach($data as $id => $d) 
{
  $selected = "";
  if($form->value("product1") == $id){ $selected = " selected"; }
 
  echo "<option value=\"$id\"$selected>$d</option>\n";
}
?>
</select>