Welcome to WebmasterWorld Guest from 18.206.48.142

Forum Moderators: open

Message Too Old, No Replies

Please suggest a script/methodology

Need a script that redirects to a specific url based on ZIP and 1 variable

     
11:50 pm on Apr 4, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 13, 2003
posts:193
votes: 0


Hello, I would really appreciate a suggestions on how to approach this problem.

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?

6:00 am on Apr 5, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 14, 2004
posts:1181
votes: 0


You need to identify the scripting language you want to use. Also, it would be helpful to know (since some scripting languages can work on both) whether the script should be server side (probably a better choice) or client side.
3:33 pm on Apr 5, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 8, 2004
posts:162
votes: 0


notsosmart,

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

5:24 pm on Apr 5, 2004 (gmt 0)

New User

10+ Year Member

joined:Mar 12, 2004
posts:16
votes: 0


A good solution might probably be some sort of regular expression with all known zip codes in there to check validity of input data first. Then if the user data matches one of the known US zips (functions is supposed to return boolean true or false) - you might proceed with expanding it making subconditions like
if it's ****-xxx-xxx => forward that chap to california page and there you go until all 50 states are there.

I don't see a shorter solution here - or maybe it's because I've had too much freshly brewed Czech beer?:()

5:37 pm on Apr 5, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 8, 2004
posts:162
votes: 0


WebFisher,

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

5:53 pm on Apr 5, 2004 (gmt 0)

New User

10+ Year Member

joined:Mar 12, 2004
posts:16
votes: 0


Hi 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?

6:06 pm on Apr 5, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 8, 2004
posts:162
votes: 0


Webfisher,

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

6:09 pm on Apr 5, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 13, 2003
posts:193
votes: 0


Thank you all...
I am talking to a programmer who can do custom work for me, but I think he and I are talking at cross-purposes.

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...

7:06 pm on Apr 5, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 8, 2004
posts:162
votes: 0


notsosmart,

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:&nbsp;<input id="txtZip" name="txtZip" type="text" value="" />
<br />
Are you male?&nbsp;<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

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members