Jump to content

DYNAMIC LISTBOX HELP!!!


grantp22

Recommended Posts

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

=========================================

 

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

============================================================================

 

<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

Link to comment
https://forums.phpfreaks.com/topic/175799-dynamic-listbox-help/
Share on other sites

Hi grantp22,

 

You're not closing your option tags, so add it to the end of each tag, i.e.:

 

<option value="image1" <? if($form->value("product1") == 'image1'){ echo 'selected="selected"'; } ?>>iPhone 3G S - 16 Go</option>

 

Also, I would recommend you not use short tags <? as some hosting does not support it, so change your code to read:

 

<option value="image1" <?php if($form->value("product1") == 'image1'){ echo 'selected="selected"'; } ?>>iPhone 3G S - 16 Go</option>

 

Hope this helps.

Link to comment
https://forums.phpfreaks.com/topic/175799-dynamic-listbox-help/#findComment-926368
Share on other sites

No, bricktop, that doesn't work, it has something to does with the single and double quotes:

 

The problem is here somewhere!!

 

=======

ORIGINAL

=======

 

value="image1" <? if($form->value("product1") == 'image1'){ echo 'selected="selected"'; } ?>>iPhone 3G S - 16 Go

 

======================

WHERE I THINK PROBLEM LIES

======================

 

value="image1" No quote for opening php <? if($form->value("product1") == 'image1'){ echo 'selected="selected"'; } ?> No closing quote for php >iPhone 3G S - 16 Go

 

I just can't figure out how to arrange the quotes to include the value="image1" and the php

 

Link to comment
https://forums.phpfreaks.com/topic/175799-dynamic-listbox-help/#findComment-926383
Share on other sites

Ok, my apologies, I did have it right as you guys all said, I found an error elsewhere in my code!

 

Only one thing, I really was expecting the onchange event to fire when the form is reloaded, I have images that are assosiated with the listbox, that get changed when the user selects an item.

 

Selecting an item will fire a function as seen in my first post, is there anyway to make this event fire automatically when the form is reloaded?

 

BTW, I really appreciate the feedback you guys have given...

 

Thanks

Grant

Link to comment
https://forums.phpfreaks.com/topic/175799-dynamic-listbox-help/#findComment-926925
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.