Forum Moderators: open
I currently redirect visitors to my affiliate partners based on two variable contained in drop-down boxes: state of residence, followed by a yes/no question.
If you're male in California, you select "California", and selct "yes" to the question "are you male?" (Okay, silly example, but it holds).
Now my partners are requesting that I pass zip code data to them. So now, my form, instead of a drop-down box, will have an input field for the zip code, followed by the YES/NO drop down.
Does anyone have a suggestion for a commercial script that accomplishes something like this? Or what type of custom programming that would be required?
It sounds like you had some code that, if the user chose 'California' and 'Yes', they would be sent to your affiliate partners site.
If so, you can use something like this, I suppose, using the zip ranges for california (90000-96699) as the check:
<script type="text/javascript">
<!--
fuction checkResponse(){
var cZip=document.getElementById('txtZip').value
var cMale=document.getElementById('maleYN').options[document.getElementById('maleYN').selectedIndex].value
//here we see if zip is in range and male
if(cZip.substring(0,5)>="90000" && cZip.subString(0,5)<="96699" && cMale=="Yes"){
document.forms[0].action="affiliateurl.com"
document.forms[0].submit()
}
}
//-->
</script>
<body>
<form method="post" action="">
State:<input type="text" id="txtZip" name="txtZip" /><br />
Are you male?:<select id="maleYN" name="maleYN">
<option value="Yes">Yes</option>
<option value="No">No</option>
</select><br />
<button onclick="checkResponse()">OK</button>
</form>
You would also probably want to set up a validation function to make sure that the user inputs a valid ZIP code.
ajkimoto
I don't see a shorter solution here - or maybe it's because I've had too much freshly brewed Czech beer?:()
Regular expressions would indeed be the way to validate US zips. Something like this would work:
function checkzip(){
//handles both zip and zip+4
if(!document.getElementById('txtZip').value.match (/([0-9]{5}-[0-9]{4}¦[0-9]{5}$)/)){
alert("Invalid ZIP");
}else{
alert('ZIP is OK')
}
}
I think that notsosmart was only sending certain zip ranges to the affiliate, but it is still a good idea to validate the zip before allowing the individual to submit the form.
ajkimoto
I think it's not only the *california* zip range that was involved here - and after the validating that only integer numerals have been typed in we should go through *every* state's zip range to forward users to appropriate state's page. Affilate sites very often have specified landing pages for every state.
Or maybe it's just more premium Czech beer :()
What do you think of Czech beer Ajkimoto?
Are you from Japan btw? Then maybe you know CSSO band?
If they do need to handle multiple different affiliates in different zip ranges, it would probably make sense to use an array of zip range objects, each with a beginning and ending zip code. Something like:
function zipObject(zBegin,zEnd,zAffiliate){
this.bzip=zBegin
this.ezip=zEnd
this.affiliate=zAffiliate
}
var affiliateArray = new Array()
affiliateArray[0] = new zipObject('07001','08989','NewJerseyAffiliate.htm')
affiliateArray[1]= new zipObject('15001','19640','PennsylvaniaAffiliate.htm')
etc.
And then cycle through the array to find the correct affiliate.
ajkimoto
Would it be possible to
1. Have a function which checked which state a zip is from
Then
2. Another function which would check for the url for that state depending on the answer to the YES/NO question?
And finally
3. Another funtion which would append the originally enetered zip code to this url?
The programmer has told me that I would have to build a DB of 42K urls with zip codes already appended, times two (one for YES, one for NO, for each zip code in the land...) BTW, this would likely be a perl script.
But if a JS script could do this dynamically, only working with 100 urls (two for each zip code range) then that might be a heck of a lot easier to accomplish...
You should be able to do all three steps in one function.
<script type="text/javascript">
<!--
//This object will hold the data (begin zip, end zip,state, yes url, no url)
function zipObject(zBegin,zEnd,cState,yURL,nURL){
this.bzip=zBegin
this.ezip=zEnd
this.state=cState
this.yesURL=yURL
this.noURL=nURL
}
//create an array, one for each zip range of interest (two demo elements are created here
var zArray = new Array()
zArray[0] = new zipObject('07001','08989','NJ','site1_yes.htm','site1_no.htm')
zArray[1]= new zipObject('15001','19640','PA','site2_yes.htm','site2_no.htm')
function checkZip(oZip,oMale){
//initialize url variable
var TempURL=''
//assign current zip code
var curZip=oZip.value
//cycle through array
for (i=0;i<zArray.length;i++){
if(curZip>=zArray[i].bzip && curZip<=zArray[i].ezip){
//set URL
tempURL=(oMale.options[oMale.selectedIndex].value=='Yes'?zArray[i].yesURL:zArray[i].noURL)
//append current zip
tempURL=tempURL+'?zip='+curZip
i=zArray.length
}
}
//go to URL
window.location=tempURL
}
//-->
</script>
</head>
<body>
<form>
Zip: <input id="txtZip" name="txtZip" type="text" value="" />
<br />
Are you male? <select id="selMale" name="selMale">
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
<br />
<input type="button" onclick="checkZip(document.getElementById('txtZip'),document.getElementById('selMale'))" value="Go" />
</form>
</body>
</html>
You will just need to add an array element to zArray for each zip range (some states have more than one zip range, lie Virginia).
ajkimoto