Forums

This topic is locked

Autofill formfields based on checkbox

Posted 29 Jun 2006 16:17:13
1
has voted
29 Jun 2006 16:17:13 sup ert posted:
Hi.
This is what I want;
After writing personal information, you tic in the checkbox if the billing adress is the same, and billing info comes automatically in their own fields.

php? javascript?

Replies

Replied 30 Jun 2006 09:18:58
30 Jun 2006 09:18:58 sup ert replied:
I worked it out in javascript, found help here;

www.htmlcodetutorial.com/forms/_INPUT_onClick.html

sup
Replied 30 Jun 2006 09:37:58
30 Jun 2006 09:37:58 Roddy Dairion replied:
for those who might get this problem again.
In the script where it says form you have to replace this with your form name present in your tag <form>
where is says default1, default2, default3(change these to the field name in your form) then this is the field that will be used as "to be copied", i.e. all value entered here will be copied straight to field1, field2, field3(change these to the field name in your form) IF you click(checked) in the checkbox, the function copy will be executed where all data in default1-3 fields will be copied to field1-3 fields, IF you click again (unchecked) this will have the reverse effect i.e. it will remove everything in the field1-3.
<pre id=code><font face=courier size=2 id=code>
&lt;SCRIPT LANGUAGE=JavaScript&gt;
&lt;!--
function copy()
{
var default1;
default1 = document.form.default1.value;
var default2;
default2 = document.form.default2.value;
var default3;
default3 = document.form.default3.value;

if(document.form.same.checked == true)
{
document.form.field1.value= default1;
document.form.field2.value= default2;
document.form.field3.value= default3;
}
else if(document.form.same.checked == false)
{
document.form.field1.value= "";
document.form.field2.value= "";
document.form.field3.value= "";
}


}
//--&gt;
&lt;/SCRIPT&gt;

&lt;form name="form"&gt;
&lt;input type="text" id="default1"&gt;
&lt;input type="text" id="default2"&gt;
&lt;input type="text" id="default3"&gt;
Field 1: &lt;input name="field1" type="text" id="field1"&gt;
Field 2: &lt;input name="field2" type="text" id="field2"&gt;
Field 3: &lt;input name="field3" type="text" id="field3"&gt;
&lt;input type="checkbox" name="same" onClick="javascript:copy()"&gt;
&lt;/form&gt;
</font id=code></pre id=code>

Reply to this topic