0

I am trying to get dropdown menu to set a value to a php variables on option selected. I can print the menu dynamically with the code below:

<?php
            $option_to_preselect = $package_name;
            $ddown_options = $package_name;
            $arrlength = count($ddown_options);

            print '<select name = "package" id = "pack_select" class="select-submit2">';

            for ($i=1; $i < $arrlength; $i++)
            { 
                     if ($ddown_options[$i]==$option_to_preselect) 
                    { 
                        print '    <option value ="' . $ddown_options[$i] . '"' . ' selected ="selected">' . $ddown_options[$i] . '</option>';
                        $packageID = $i;
                        $packagePrice = $packages_price[$i];
                    }   else {
                                print '    <option value="' . $ddown_options[$i] . '">' . $ddown_options[$i] . '</option>';
                        $packageID = $i;
                        $packagePrice = $packages_price[$i];
                             }
            }  
             print '</select>';

  ?>

Then when the user selects an option, the variables $packageID and $packagePrice should obtain the id and the amount of the selected package.

The idea is to pass the values in a href where the href is:

href="<?php echo site_url('package_purchase/'.$lang_code.'/'.$packageID.'/'.$packagePrice); ?>"

The output of the $package_name is:

    array (size=4)
  '' => string '' (length=0)
  1 => string 'Free' (length=4)
  2 => string 'Basic' (length=5)
  3 => string 'Premium' (length=7)

and the result of the code looks like:

<select name="package" id="pack_select" class="select-submit2">
<option value="Free">Free</option>
<option value="Basic">Basic</option>
<option value="Premium">Premium</option>
</select>
Jsn
  • 73
  • 10
  • I forgot to mention that I've tried many things in the web, but I just can't get it working. And what is now working is the variables. They do not change their values on select. – Jsn Jan 23 '16 at 09:56
  • You're overwriting the values of $packageID & $packagePrice in your "else" statement – ahmad Jan 23 '16 at 12:25
  • Not exactly the same but has some components that might be of use. Add an onchange event to the select dropdown http://stackoverflow.com/questions/34361452/many-elements-of-a-listbox-to-another/34367418#34367418 – Steve Jan 24 '16 at 01:07

0 Answers0