Jump to content


Photo

capturing output from dynamic list/menu


  • Please log in to reply
3 replies to this topic

#1 poppy

poppy
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 07 May 2006 - 04:52 PM

I have a list/menu dropdown on my php page. It is dynamically populated from a mysql DB.
I need to capture and store seperately both the 'label' and the 'value' of the item chosen from the list so they can be used on a subsequent page - any ideas how this can best be done ?

current code reads as follows

name="dropdown">
<?php
do {
?>
<option value="<?php echo $row_rsswf['ItemValue']?>"><?php echo $row_rsswf['ItemName']?></option>

<?php
} while ($row_rsswf = mysql_fetch_assoc($rsswf));
$rows = mysql_num_rows($rsswf);
if($rows > 0) {
mysql_data_seek($rsswf, 0);
$row_rsswf = mysql_fetch_assoc($rsswf);
}
?>
</select>

many thanks

p

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 07 May 2006 - 05:19 PM

When this form is submitted (and assuming the method is 'post'), the selected item will be found in the $_POST array as:
$picked = $_POST['dropdown']; // get SELECTed value
Since that would only pass the value as your select options are presently written, you need to restructure your option value to include both the value and name of the selection. For example:
<option value="<?php echo $row_rsswf['ItemValue']. "|'. $row_rsswf['ItemName']?>"><?php echo $row_rsswf['ItemName']?></option>

Then you can separate the two parts by 'exploding' the value of $picked into its components. That will make them available on the page to which the form is submitted. If you want them on other pages, storing them as session variables is probably simplest.
Legend has it that reading the manual never killed anyone.
My site

#3 poppy

poppy
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 09 May 2006 - 03:51 PM

cheers andy

i have exploded the result as you suggest

<?php
$picked = $_POST['dropdown']; // get selected value and name from dropdown
list($optionvalue, $optionname) = explode("|", $picked); //assign values in the listed order
?>

it works - great !
but, what i have subsequently realised though ( after many painful hours... ) is that what i need to do is to pass these two new values to another place within the code of the samepage, not the subsequent one as i had previously explained.
that is to say that there is a php code block in the header of my page ( from an ecart template ) that needs to pick up these new values as column bindings before moving to the subsequent ( cart ) page.

this code block was working before by gathering either the 'label name' or the 'value' from the 'dropdown' in the following way
<rest of code block.....
$Options = "".$_POST["dropdown"] .""; // column binding
.....more code>

and succesfully passing it on.

now however if do this with one of the new values for starters
<rest of code block.....
$Options = "".$optionvalue.""; // column binding
......more code>

it passes nothing on !

what stupid mistake am i making this time??

p

#4 poppy

poppy
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 31 May 2006 - 01:20 PM

sorted it thanks for your help

had the explode in the wrong place

learning.

p




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users