Click to See Complete Forum and Search --> : merge multiple input fields values into new
peter01
02-01-2003, 01:04 PM
Hi,
Newbie question:
Let's say I have 2 input fields:
<input type="text" name="field1">
<input type="text" name="field2">
How can the values that a user enters for both fields be combined in one string and be passed as a new field, say field3, to the formmailer cgi script (formmail.pl by Matt Wright)?
I would prefer to do this on the form with javascript, not in the cgi.
Thanks in advance,
peter
Charles
02-01-2003, 01:10 PM
If you do this with JavaScript then what will happen to the one in ten users that do not use JavaScript?
Zach Elfers
02-01-2003, 01:23 PM
In JavaScript:
<script language="JavaScript" type="text/JavaScript">
<!--
function combine(form) {
form.field3.value = form.field1.value + form.field2.value;
}
//-->
</script>
...
<form>
<input type="text" name="field1">
<p>
<input type="text" name="field2">
</p>
<input type="button" value="Combine" onClick="combine(this.form);">
<p>
<input type="text" name="field3">
</form>
That should do what you want.
peter01
02-01-2003, 02:04 PM
Hi,
Many thanks. Can it also be done automatically, without the user interaction (i.e. without the button that combines the fields)?
regards, peter
Zach Elfers
02-01-2003, 02:06 PM
Yes:
<input type="text" name="field1">
<input type="text" name="field2">
<input type="text" name="field3" value="this.form.field1.value + this.form.field2.value;">
peter01
02-01-2003, 03:05 PM
Hi,
I tried your suggetion, but what is passed now to the form-mailer is the actual string:
this.form.field1.value + this.form.field2.value;
but not the values that the user entered.
peter01
02-01-2003, 03:25 PM
Works great!! Thanks.
Charles
02-01-2003, 03:54 PM
Originally posted by Dave Clark
Grief, Charles... What will happen to the nine in ten users that aren't helped by such non-answers?
Dave A method that fails as often as JavaScript fails can hardly be said to work and we don't make the workld any better of a place by encouraging web authors to make pages that needlessly exclude persons with certain disabilities. And Peter knows the answer to the problem - make a few simple changes to the CGI script.
peter01
02-01-2003, 04:33 PM
Hi,
I don't want to interfere with Charles' and Dave's diversion here, but to set the record straight: Dave's solution involves only one simple handler in the form tag. And it works like a charm. :)
Zach Elfers
02-01-2003, 04:46 PM
Originally posted by Dave Clark
Zach, I finally decided to see if your binary sig actually said anything. Very cute. ;)
I applied the following script to it:
var str = "", bin = "0100110101111001001000000110111001100001011011010110010100100000011010010111001100100000010110100110 0001011000110110100000100001";
for (i=0; i<bin.length; i+=8) str += String.fromCharCode(parseInt(bin.substr(i,8),2));
alert(str);
Dave
lol:D
jiser
05-25-2006, 05:52 PM
Hello,
I tried to insert below code into the body of the text, but unfortunately the fields of field 1 and 2 aren't automately merged into field three..... Could someone tell me why??
Thanks!!
Michael
<input type="text" name="field1">
<input type="text" name="field2">
<input type="text" name="field3" value="this.form.field1.value + this.form.field2.value;">
jiser
05-25-2006, 06:12 PM
Is it perhaps the best way to insert in field a en b: onchange="combine(form)";
plus in top of the form code:
<script>
function combine(form) {
form.field3.value = form.field1.value + form.field2.value;
}
</script>
????
jiser
05-26-2006, 12:58 PM
Anybody???