简体   繁体   English

将JavaScript变量传递给codeigniter中的隐藏形式

[英]passing a javascript variable into a hidden form in codeigniter

I have a page in Codeigniter in which i can set the product quantity (sq.m) that the client wants to buy. 我在Codeigniter中有一个页面,我可以在其中设置客户想要购买的产品数量(平方米)。 When the client sets the quantity i have to do some calculations (about the quantity number whis was set before) and i am doing that with Javascript setting some variables. 当客户端设置数量时,我必须进行一些计算(关于之前设置的数量),而我使用Javascript设置了一些变量。 Now i want to show these variables in the same (php) page and it's working, but i don't know what should i do to put these variables in the values of the hidden inputs so that i can send them to the controller. 现在,我想在同一(php)页面中显示这些变量,并且可以正常工作,但是我不知道该怎么做才能将这些变量放入隐藏输入的值中,以便可以将它们发送到控制器。 Here is the coding: 这是代码:

<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>  

Thanks for your answers 谢谢你的回答

首先给您的输入一个id,然后您可以传递如下值:

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

It is very simple.You do it the same way. 这很简单,您可以用相同的方式进行操作。

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

alert(MyHiddenValue);

WORKING FIDDLE 工作场所

Add these lines in the else condition after your math: 在数学运算之后的else条件中添加以下行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM