Forum Moderators: open

Message Too Old, No Replies

Passing Variable to hidden input?

         

MizzBia

1:35 am on Dec 16, 2008 (gmt 0)

10+ Year Member



Hi Guys,
I'm hoping there is a JavaScript wiz out there that can help me with this. I would like to know if it is possible to pass on a variable that a JS script produced to a hidden input field but without actually submitting the form. I'll explain...

I'm using a free calendar script which produces 3 select menus (or drop downs) for the date using this code:
<script>DateInput('orderdate', true, 'YYYY-MM-DD')</script>

The date that they select in the 3 select menus is saved in the YYYY-MM-DD format. How can I get that variable/data passed on to my hidden input field within the same page/form:
<input name="Date" value="NEED-THE-YYYY-MM-DD-DATE-HERE" type="hidden">

Is there like a print/write function or something else that can grab/copy the date produced from the JS calendar and add it to the hidden input so that when the form submits, the hidden input has the proper date data?

Hope I made sense. Any help would be aprpeciated

MizzBia

3:29 am on Dec 16, 2008 (gmt 0)

10+ Year Member



never mind, I just switched calendar scripts to one that is more compatible. It works now :)

MizzBia

12:51 am on Dec 17, 2008 (gmt 0)

10+ Year Member




I have run into another problem similar to this (I guess I can't avoid it). My form has two fields that will have two different names but the same value. Instead of making the user type in the field twice, I wanted to know if there was any way I could copy the value selected to a hidden input so that when the form is submitted, the two variables are sent off with the same value for both fields.

I was looking at some scripts that allow you to do this to text boxes with a check box (for example when you copy billing address to shipping address) except that my fields are a hidden input and a select menu and not text boxes. Furthermore, my input names are very bad and have periods in them so I don't know how to get it to work with the javascript code. For example, one of my fields is: air.y.leg1.departLocation

I am not able to change the field names. Does anyone know of a way I could get this to work?

daveVk

3:01 am on Dec 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You just need assign the value from one input element to the other.

hideEl.value = otherEl.value;

You can get hideEl and otherEl any number of ways, for example give both an id.

<input type="hidden" id="hide1" ... >
and use
hideEl = document.getElementById( "hide1" );

astupidname

3:31 am on Dec 17, 2008 (gmt 0)

10+ Year Member



An example of what daveVk is getting at, use onkeyup for text inputs or onchange for selects or onclick for checkboxes or radio buttons that have values:

<script type="text/javascript">

function copyThisValue(obj,hidID){
document.getElementById(hidID).value = obj.value;
}

//I just threw this function below in for demo to see the hidden values
//(after you type in the input and make a selection of course
function seeHiddenValues() {
var hidValOne = document.getElementById('copiedStuffOne').value;
var hidValTwo = document.getElementById('copiedStuffTwo').value;
alert(hidValOne+"\n"+hidValTwo);
}

</script>
</head>
<body>
<form action="" name="someForm">
<p><input type="text" size="50" onkeyup="copyThisValue(this,'copiedStuffOne');" />
<select onchange="copyThisValue(this,'copiedStuffTwo');">
<option selected="selected">Choose an option</option>
<option value="someValue">Option one</option>
<option value="someOtherValue">Option two</option>
<option value="anotherValue">Option three</option>
</select>
<input id="copiedStuffOne" type="hidden" />
<input id="copiedStuffTwo" type="hidden" />
<!-- the button below is just for demo -->
<input type="button" value="See The Hidden Values" onclick="seeHiddenValues();" /></p>
</form>

MizzBia

5:37 pm on Dec 17, 2008 (gmt 0)

10+ Year Member



Thank you both for your help! astupidname, your code worked PERFECTLY! Thank you so much for your help... I would have never gotten this to work on my own. I really appreciate it =)

lmgsimons

6:05 pm on Mar 5, 2009 (gmt 0)

10+ Year Member



thank you for this info, it has been very helpful. however, I am still struggling with one part of this.

I can use the code as is. I can add my own form fields and get it to return the proper value in the alert box. What I cannot do, is actually write to the hidden form field with the proper value.

The idea with my form is that a donor can choose a preset payment amount via a radio button or enter a custom amount for donating. then I need to pass the selected option's value to the hidden field "chargetotal" which will get passed to the cc processing gateway. I have other forms, but in those cases, I set the chargetotal value by hand.

here is the form.

<form name="donations" action="https://www.staging.yourpay.com/lpcentral/servlet/lppay" method="post">

<input name="chargetotal" id="chargetotal" type="hidden" />
<input type="hidden" name="package" value="Donation">

<label><input type="radio" name="donation" value="150.00" onchange="radio_checker();" onclick="copyThisValue(this,'chargetotal');" /> $150.00</label>
<br />
<label><input type="radio" name="donation" value="250.00" onchange="radio_checker();" onclick="copyThisValue(this,'chargetotal');" /> $250.00</label>
<br />
<label><input type="radio" name="donation" value="500.00" onchange="radio_checker();" onclick="copyThisValue(this,'chargetotal');" /> $500.00</label>
<br />
<input type="radio" name="donation" value="custom" id="custom_amount" onchange="radio_checker();" />
Other:
<input type="text" name="custom_donation" disabled="true" onkeyup="copyThisValue(this,'copiedStuffOne');" />
<br />
<p><input type="submit" value="Continue"></p>
</form>

if I set the button to type="button", etc as in the original post, I can make the alert popup and state the right value. but I need to set it so that the value of chargetotal is passed on when I submit the form. here is the javascript I am using:

<script type="text/javascript" language="javascript">

function copyThisValue(obj,hidID){
document.getElementById(hidID).value = obj.value;
}

function seeHiddenValues() {
var hidValOne = document.getElementById('chargetotal').value;
var hidValTwo = document.getElementById('copiedStuffTwo').value;
alert(hidValOne+"\n"+hidValTwo);
}

function radio_checker() {
if (document.donations.custom_amount.checked) {
document.donations.custom_donation.disabled = false;
document.donations.custom_donation.focus();
}
else {
document.donations.custom_donation.disabled = true;
}
}
</script>

I thought maybe something like this would do it, but I just can't make it work...

<script type="text/javascript" language="javascript">
var hidValOne = document.getElementById('chargetotal').value;

document.donations.chargetotal.value=hidValOne
</script>

there are three other pieces to this that I am also struggling with, and not sure if its better to address all at once or in pieces, so I list them here for reference if for nothing else:
(1) need to check the custom_donation input field and add ".00" to the number if it is missing (formatting)
(2) need to check the custom_donation input field and make sure it is no less than $5.00 (can't accept donations less than $5.00)
(3) add an option for making the donation monthly (check box?), which if checked would add 5 additional hidden fields:

<INPUT name="submode" value="periodic" type="hidden">
<INPUT name="periodicity" value="m1" type="hidden">
<INPUT name="installments" value="12" type="hidden">
<INPUT name="threshold" value="3" type="hidden">
<input type="hidden" name="startdate">

your help and advice is most appreciated.