0

I have a page in Codeigniter in which i can set the product quantity (sq.m) that the client wants to buy. 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. 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. 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

1

3 Answers 3

0

Add these lines in the else condition after your math:

document.getElementById("productPacks").value    = val;
document.getElementById("productQuantity").value = total;
Sign up to request clarification or add additional context in comments.

3 Comments

thanks again for your answers, it's working nw but i have another problem which is when the var 'total' is '10.4' for example, and my hidden is: <input type="hidden" id="total2" value="" name="productQuantity"> it's stored in the hidden input as '104', maybe anyone knows what should i do to solve that out?
Accept the answer it it worked for you, and for the other query see this link jsfiddle.net/3GneR
Write document.getElementById("productQuantity").value = new String(total); instead of document.getElementById("productQuantity").value = total;. Hope it helps.
0

Starty by giving your inputs an id and then you can pass the values like this :

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

Comments

0

It is very simple.You do it the same way.

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

alert(MyHiddenValue);

WORKING FIDDLE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.