Connecting Tech Pros Worldwide Forums | Help | Site Map

either or script

PC-Dad
Guest
 
Posts: n/a
#1: Jul 23 '05
I'd like to make a form in which the user would input her zip code.
When they submit then the script would compare to the list of zips that
I service. If their zip is included re-direct to page A or if not
included re-direct to page B.

Would anyone be able to assist me here?

Thanks;

Scott Solar is
PC-Dad.com


kaeli
Guest
 
Posts: n/a
#2: Jul 23 '05

re: either or script


In article <1112979116.885440.284070@g14g2000cwa.googlegroups .com>,
scott.solar@gmail.com enlightened us with...[color=blue]
> I'd like to make a form in which the user would input her zip code.
> When they submit then the script would compare to the list of zips that
> I service. If their zip is included re-direct to page A or if not
> included re-direct to page B.
>
> Would anyone be able to assist me here?[/color]

This is best done on the server-side.
Is this jscript and .net?
If not, this group is not really the appropriate place for it, but I might be
able to help you. What scripting language are you using on the server?

--
--
~kaeli~
Experience is something you don't get until just after you
need it.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

J Wynia
Guest
 
Posts: n/a
#3: Jul 23 '05

re: either or script


PC-Dad wrote:[color=blue]
> I'd like to make a form in which the user would input her zip code.
> When they submit then the script would compare to the list of zips that
> I service. If their zip is included re-direct to page A or if not
> included re-direct to page B.
>
> Would anyone be able to assist me here?
>
> Thanks;
>
> Scott Solar is
> PC-Dad.com
>[/color]

Here's a sample HTML document that switches the form's "action" to a
different page if the "zip" field is filled in. You can modify this
greatly to do whatever you want by changing the stuff in the condition
part of the "if" statement. Of course, once you get more than a few
conditions, you'll need something more complex than the if, but this
will work for most basic cases.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<script language="JavaScript">
<!--
function check_zip(){
$zip = testform.zip.value;
if(($zip == null) || ($zip == ""))
{
testform.action = "form_processor1.php";
} else {
testform.action = "form_processor2.php";
}
}
//-->
</script>
</head>

<body>
<form name="testform" onsubmit="check_zip();" action="">
<input type="text" name="address">
<input type="text" name="zip">
<input type="submit">
</form>
</body>
</html>


J Wynia
Myriad Intellect, Inc.
www.myriadintellect.com
Lee
Guest
 
Posts: n/a
#4: Jul 23 '05

re: either or script


PC-Dad said:[color=blue]
>
>I'd like to make a form in which the user would input her zip code.
>When they submit then the script would compare to the list of zips that
>I service. If their zip is included re-direct to page A or if not
>included re-direct to page B.
>
>Would anyone be able to assist me here?[/color]

It would probably be better to do that on the server side, if possible,
because you wouldn't have any practical limit to the number of zip codes
or other factors that you use to decide which page to show.

Here's one example that allows you to use an "x" as a wildcard character
and number options in square brackets. The entry "8080[79]" matches
either a 7 or a 9 in the last position. "876x[123]" would match any digit
in the 4th position, and any of 1, 2, or 3 in the 5th, etc.

Note that any invalid entry is treated as a zip code that isn't in the
area.

<html>
<head>
<script type="text/javascript">

zipList= [ "12345", "23456", "345xx", "4567x",
"56789", "56780", "8080[79]", "7xx01"
];

function inArea(zip) {
for(var i=0;i<zipList.length;i++) {
if ((new RegExp("^\\s*"
+zipList[i].replace(/x/g,"\\d")
+"\\s*$").test(zip))) {
return true;
}
}
return false;
}
function redirect(zip) {
if(inArea(zip)) {
location="http://www.google.com";
} else {
location="http://maps.google.com/maps?q="+zip+"&spn=0.1,0.1";
}
}
</script>
</head>
<body>
<form onsubmit="return false">
Enter 5-digit U.S. zip code: <input name="zip"><br>
<input type="button" value="test" onclick="redirect(this.form.zip.value)">
</form>
</html>

otto
Guest
 
Posts: n/a
#5: Jul 23 '05

re: either or script


What about populating the list of zips from an xml file called through
XMLHttpRequest? That would keep things client side.

Lee
Guest
 
Posts: n/a
#6: Jul 23 '05

re: either or script


otto said:[color=blue]
>
>What about populating the list of zips from an xml file called through
>XMLHttpRequest? That would keep things client side.[/color]

I'm not sure what advantage you're suggesting.
In both cases, the data is sent from the server to the client.
In the case of XML, the data is in a more verbose format, but
it might be easier to import into a database in the future.

Randy Webb
Guest
 
Posts: n/a
#7: Jul 23 '05

re: either or script


J Wynia wrote:[color=blue]
> PC-Dad wrote:
>[color=green]
>> I'd like to make a form in which the user would input her zip code.
>> When they submit then the script would compare to the list of zips that
>> I service. If their zip is included re-direct to page A or if not
>> included re-direct to page B.
>>
>> Would anyone be able to assist me here?
>>
>> Thanks;
>>
>> Scott Solar is
>> PC-Dad.com
>>[/color]
>
> Here's a sample HTML document that switches the form's "action" to a
> different page if the "zip" field is filled in. You can modify this
> greatly to do whatever you want by changing the stuff in the condition
> part of the "if" statement. Of course, once you get more than a few
> conditions, you'll need something more complex than the if, but this
> will work for most basic cases.
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <html>
> <head>
> <title> New Document </title>
> <script language="JavaScript">[/color]

language attribute is deprecated, use the type attribute instead.
[color=blue]
> <!--[/color]

Hiding scripts is an obsolete practice.
[color=blue]
> function check_zip(){
> $zip = testform.zip.value;[/color]

There is very good reason why you do not see that shortcut notation very
often in this newsgroup. IE creates a global scope to the form object
that is neamed testform, no other browser (AFAIK) do that. So the above
line would be more cross-browser as:

document.forms['testform'].elements['zip'].value;
[color=blue]
> if(($zip == null) || ($zip == ""))
> {
> testform.action = "form_processor1.php";[/color]

same here with the global-ness of testform.
[color=blue]
> } else {
> testform.action = "form_processor2.php";[/color]

ditto.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
otto
Guest
 
Posts: n/a
#8: Jul 23 '05

re: either or script



Lee wrote:[color=blue]
> otto said:[color=green]
> >
> >What about populating the list of zips from an xml file called[/color][/color]
through[color=blue][color=green]
> >XMLHttpRequest? That would keep things client side.[/color]
>
> I'm not sure what advantage you're suggesting.
> In both cases, the data is sent from the server to the client.
> In the case of XML, the data is in a more verbose format, but
> it might be easier to import into a database in the future.[/color]

Just the advantage of keeping data separate from logic. You make a good
point either way the data has to be downloaded. I was assuming we were
talking about hundreds or thousands of numbers. In which case it may be
beneficial to download the data using XMLHttpRequest as a seperate
asynchronous http request. That way the user wouldn't have to wait
forever for the interface to download. The interface would download
first and then the data would download while the user is busy filling
out the form. Just an idea.

PC-Dad
Guest
 
Posts: n/a
#9: Jul 23 '05

re: either or script


Thanks to everyone!

This is what I needed. I do home compueter repairs and such but may be
marketing other services outside of my home range. The zips I service
will get the current pages but outside my reach they can still buy my
other stuff.

Thanks again t everyone who helped.

Be strong and proser.

Scott Solar is PC-Dad.com

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#10: Jul 23 '05

re: either or script


PC-Dad wrote:
[color=blue]
> I'd like to make a form in which the user would input her zip code.
> When they submit then the script would compare to the list of zips
> that I service. If their zip is included re-direct to page A or if
> not included re-direct to page B.
>
> Would anyone be able to assist me here?[/color]

Yes.

We had the ZIP code issue previously. All you have to do is create two
RegExps or objects and redirect accordingly. Be sure to include a
server-side alternative in case client-side scripting is unsupported.


PointedEars
Closed Thread