checkbox value in input field

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
laknal
Forum Newbie
Posts: 21
Joined: Wed Oct 11, 2006 7:38 pm

checkbox value in input field

Post 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.
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post by Cameri »

For that kind of thing, PHP is not what you need.
You might want to use Javascript to accomplish this.
laknal
Forum Newbie
Posts: 21
Joined: Wed Oct 11, 2006 7:38 pm

checkbox value in input field

Post by laknal »

Hi,

Can you give any example for this functionality?

Thanks
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post 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.
Last edited by Cameri on Mon Nov 06, 2006 10:38 pm, edited 1 time in total.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

moved to client side
laknal
Forum Newbie
Posts: 21
Joined: Wed Oct 11, 2006 7:38 pm

checkbox value in input field

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