Hello
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 = http://mydomain.com?&=<<<ZIP>>> ;
<form method=“post” >
<input type=“hidden” name=“redirect” value="http://mydomain?l=<<<ZIP>>> ; "/>
<input type=“text” name=“Zip” value=‘’ ">
</form>
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
Add id=“” attributes to make the js easier
Add an onSubmit function
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;
}
</script>
<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()”>
</form>
This stuff is a lot easier with jquery.
Raffles
February 21, 2010, 12:39am
3
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">
</form>
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.