将一个javascript变量传递给codeigniter中的隐藏表单

时间:2013-07-26 12:26:28

标签: javascript codeigniter hidden

我在Codeigniter中有一个页面,我可以在其中设置客户想要购买的产品数量(平方米)。当客户设置数量我必须做一些计算(关于之前设置的数量数字),我正在使用Javascript设置一些变量。现在我想在同一个(php)页面中显示这些变量并且它正在工作,但我不知道我该怎么做才能将这些变量放在隐藏输入的值中,以便我可以将它们发送到控制器。这是编码:

<input type="text"  name="productQuantity">
<input type="button" class="button" onclick="return showHide();" value="Calculate Packs">

<script type="text/javascript" language="javascript">// <![CDATA[
    function showHide() {
        var ele = document.getElementById("showHideDiv");
        if(ele.style.display == "block") {
            ele.style.display = "none";
        }
        else {
<?php $pack = explode(' ', $pro->productPackSize); ?>
                ele.style.display = "block";

                var val = document.getElementById('productQuantity').value;
                document.getElementById('val').innerHTML = val;
                var req = Math.round(val/<?php echo $pack[0]; ?>);
                document.getElementById('req').innerHTML = req;
                var total = req * <?php echo $pack[0]; ?>;
                document.getElementById('total').innerHTML = total;

            }
        }
        // ]]></script>

<div id="showHideDiv" style="display:none;"> 
    <?php echo form_open('main/add_to_cart_validation/' . $pro->productId); ?>
    To cover <strong id="val"></strong> sq.m,  you will need <strong id="req"></strong> Packs 
    which is <strong id="total"></strong> sq.m in total</a> //the ids' here are working, i can show them in the page

<input type="hidden" id="" value="" name="productPacks"> //how can i get the 'reg' variable here?
<input type="hidden" id="" value="" name="productQuantity"> //the same here as above for 'total' variable
<input type="submit" class="button" value="Add to Cart">
</form>
</div>  

感谢您的回答

3 个答案:

答案 0 :(得分:0)

通过给你的输入一个id来开始,然后你可以传递这样的值:

 document.getElementById('myField').value = total;

答案 1 :(得分:0)

这很简单。你也这样做。

var MyHiddenValue = document.getElementById('myHiddenField').value;

alert(MyHiddenValue);

WORKING FIDDLE

答案 2 :(得分:0)

在数学后,在else条件中添加这些行:

document.getElementById("productPacks").value    = val;
document.getElementById("productQuantity").value = total;