如何在发布页面之前获取所选下拉列表的值

时间:2012-10-05 00:08:37

标签: php jquery codeigniter

<?php 

$options = array(
                  500  => 500,
                  1000 => 1000,
                  2500 => 2500,
                  5000 => 5000,
                );

echo form_dropdown('units',$options);

?>

$db_num = 25; # a random number
$post_num = $set_num * (units-selected) 

如果单击提交,我如何将单位下拉列表的选定值与我的$db_num相乘?我认为这可能需要javascript,但是没有吗?

在我的控制器上我尝试了这个:

$db_num = $this->input->post('db_num');
$units = $this->input->post('units');

$answer = $db_num * $units;

...

echo $answer; 

(结果是没有错误,没有插入。我做错了什么?)

3 个答案:

答案 0 :(得分:0)

类似的东西:

<form ... onsubmit="return functionwhereyouwilldothemath(this)" >
</form>

<script type="text/javascript">
function functionwhereyouwilldothemath(el) { //el = javascript form object
    var db_num = <?=$db_num?>; //yup this is legit.
     //do your math
    return true; //will cause the page to reload uppon submit.
}
</script>

答案 1 :(得分:0)

传递我的变量的数据库字段需要设置为INT - 问题已解决。

答案 2 :(得分:0)

使用php implode函数存储和爆炸显示功能

$ itm = $ _POST ['hospital_department'];                 $ comma_separated = implode(“ - ”,$ itm);

$data['hospital_department']            = $comma_separated;