我正在尝试获取下拉菜单,将值设置为选中的选项上的php变量。我可以使用以下代码动态打印菜单:
<?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>';
?>
然后当用户选择一个选项时,变量$ packageID和$ packagePrice应该获取所选包的id和数量。
我们的想法是将href中的值传递给href:
href="<?php echo site_url('package_purchase/'.$lang_code.'/'.$packageID.'/'.$packagePrice); ?>"
$ package_name的输出是:
array (size=4)
'' => string '' (length=0)
1 => string 'Free' (length=4)
2 => string 'Basic' (length=5)
3 => string 'Premium' (length=7)
代码的结果如下:
<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>