Copy one text field to another


I am looking to take the information submitted into field A and add it to the end of the value of field B

Text Field A = Zip
Hidden Field B - redirect =<<<ZIP>>>

<form method=“post” >
<input type=“hidden” name=“redirect” value="http://mydomain?l=&lt;&lt;&lt;ZIP&gt;&gt;&gt; "/>
<input type=“text” name=“Zip” value=‘’ ">

When they put in their zip code, I need the script to add that information at the end of the redirect URL

Hope that explains what I’m trying to do…
Any help would be appreciated

  1. Add id=“” attributes to make the js easier
  2. Add an onSubmit function
  3. Just in case there are other elements in the form, add an onChange function too.

<script type=“text/javascript”>
function myFunction() {
var zip = document.getElementById(‘zipID’).value;
var redirect = document.getElementById(‘redirectID’).value;
document.getElementById(‘redirectID’).value = redirect + zip;
return true;
<form method=“post” onSubmit=“myFunction()”>
<input type=“hidden” id=“redirectID” name=“redirect” value=“http://mydomain?l=” />
<input type=“text” id=“zipID” name=“Zip” value=“” onChange=“myFunction()”>

This stuff is a lot easier with jquery.

I don’t see the point of this, because that code isn’t actually going to redirect the browser, all it’s going to do is create a new form field with information you already have.

You might as well do it on the server side. If you want the form to redirect to that location, then you need to set the “action” attribute to that:

<form method="post" action="">
  <input type="text" id="zip" name="Zip">
  <input type="submit">

var zip = document.getElementById('zip');
zip.onchange = function() {
  this.parentNode.action = '?l=' + this.value;

That’s all you need really. You should also check to see that the zip is a valid one:

zip.parentNode.onsubmit = function() {
  var zipval = parseInt(zip.value);
  if (zipval > 9999 && zipval < 99999) return true;
  else {
    alert('Please enter a valid Zip code');
    return false;

Bear in mind that I have not looked up how US zip codes work so that’s just an example.