Page 1 of 1

checkbox value in input field

Posted: Mon Nov 06, 2006 7:47 pm
by laknal
Hi,

I am generating checkboxes for products dynamically. And I am trying to checked product value and pass that value into input hidden field.

For example:

If the user selects checkbox -1 (product id:1) and checkbox -10(product id:10), the input filed will contain value 10, otherwise the input value will be 1(by default).

Any help is appreciated.

Posted: Mon Nov 06, 2006 9:39 pm
by Cameri
For that kind of thing, PHP is not what you need.
You might want to use Javascript to accomplish this.

checkbox value in input field

Posted: Mon Nov 06, 2006 9:54 pm
by laknal
Hi,

Can you give any example for this functionality?

Thanks

Posted: Mon Nov 06, 2006 10:36 pm
by Cameri
You are in the wrong forum section if that's your new question, but I'll try to answer it anyways:

sethiddenfield.js:

Code: Select all

function setHiddenField(fieldName, number){
    var hiddenField = null;
    if (window.document.getElementById)
        hiddenField = window.document.getElementById(fieldName);
    else if (window.document.all)
        hiddenField = window.document.all[fieldName];
    else if (window.document.layers)
        hiddenField = window.document.layers[fieldName];
    if (hiddenField) {
        hiddenField.value = (number>1)?number:1;
    }
}
Let's say your HTML page with the form is as follows:

mypage.html:

Code: Select all

<html>
    <head>
        <title>Woot</title>
        <script type="text/javascript" src="sethiddenfield.js"></script>
    </head>
    <body>
        <form method="post" action="blabla.php">
            <input type="hidden" id="myHiddenField" name="myHiddenField" value="1" />
            1: <input type="checkbox" id="chk" name="chk" onselect="setHiddenField('myHiddenField',1);" value="1" /><br />
            ...<br />
            2: <input type="checkbox" id="chk" name="chk" onselect="setHiddenField('myHiddenField',10);" value="10" /><br />
        </form>
    </body>
</html>
Hope this is helpful.

Posted: Mon Nov 06, 2006 10:38 pm
by Burrito
moved to client side

checkbox value in input field

Posted: Tue Nov 07, 2006 7:37 pm
by laknal
Thank you for the help. Which forum should I post similar to this question?

Your code is helps alot. Just one more question:

I have a product recordset which has comma delimited product id's (1,5,7). I am generating product checkboxes dynamically.

And a hidden input texfield with default value 1. I have to check, if rsProduct has product_id 7 (the rsProduct may have other product id's).

If rsProduct has product id 7, then I have to change to hidden input value from 1 to 7.


Thanks