how do I code this?

yoaussie2

Registered Member
Joined
Dec 31, 2008
Messages
91
Reaction score
7
hi guys,

I need to create a simple form that allows the user to enter their zip code and when they hit submit, it returns a custom page (the page is the same for everyone no matter what their zip code), but I need some level of error checking, can I get the form to lookup if zip code exists and if not, return an error message?

thanks in advance!
 
Code:
<?php
if(isset($_REQUEST["thezip"]){
  $thezip = $_REQUEST["thezip"];
}

$zips = file('http://www.census.gov/tiger/tms/gazetteer/zips.txt');

if(strpos($zips,$thezip){
?>
your zip has been submitted! your zip is <?php echo $thezip; ?>
<?php }
else{
?>
Error: your zip (<?php echo $thezip; ?> is invalid.
<?php } ?>
 
Last edited:
thanks Arthas, that was super quick! much appreciated
 
hi again,

ok, I copied the code into the body of my html page, saved the page as test.php and when I browse to the page, I get:

Parse error: syntax error, unexpected '{' in /home/clickthr/public_html/mysite.com/test.php on line 1

Please forgive the noob!!!

Do you know what I've done wrong?

cheers heaps!
 
hey gimmie4free

here's what I've got:

HTML:
<html>
<head>
	<title>Untitled</title>
</head>

<body>
<?php
if(isset($_REQUEST["thezip"]){
  $thezip = $_REQUEST["thezip"];
}

$zips = file('http://www.census.gov/tiger/tms/gazetteer/zips.txt');

if(strpos($zips,$thezip){
?>
your zip has been submitted! your zip is <?php echo $thezip; ?>
<?php }
else{
?>
Error: your zip (<?php echo $thezip; ?> is invalid.
<?php } ?>

</body>
</html>

I apologise for the font colour too, really showing my newbie colours tonight!
 
closing brackets are missing, try
Code:
<html>
<head>
	<title>Untitled</title>
</head>

<body>
<?php
if(isset($_REQUEST["thezip"])[COLOR="red"][B])[/B][/COLOR]{
  $thezip = $_REQUEST["thezip"];
}

$zips = file('http://www.census.gov/tiger/tms/gazetteer/zips.txt');

if(strpos($zips,$thezip)[COLOR="Red"][B])[/B][/COLOR]{
?>
your zip has been submitted! your zip is <?php echo $thezip; ?>
<?php }
else{
?>
Error: your zip (<?php echo $thezip; ?> is invalid.
<?php } ?>

</body>
</html>
 
you could try changing file('http...... to file_get_contents('http.......

That might do the trick

Edit: Also what he said ^^^^ (he answered before I got the chance)
 
ok,

I have this error now: Parse error: syntax error, unexpected '{' in /home/clickthr/public_html/mysitename.com/test.php on line 1

Code:
<html>
<head>
	<title>Untitled</title>
</head>

<body>
<?php
if(isset($_REQUEST["thezip"])){
  $thezip = $_REQUEST["thezip"];
}

$zips = file_get_contents('http://www.census.gov/tiger/tms/gazetteer/zips.txt');

if(strpos($zips,$thezip)){
?>
your zip has been submitted! your zip is <?php echo $thezip; ?>
<?php }
else{
?>
Error: your zip (<?php echo $thezip; ?> is invalid.
<?php } ?>


</body>
</html>
 
I have just copied your code to my test server and the code is fine (well a couple of bad ideas iin it but it works). Are you sure that is the whole code?

Im going home for dinner, will be back and check this post afterwards in case you have any more questions.

P.S The following code will load up slightly quicker than the original version
Code:
<html>
<head>
	<title>Untitled</title>
</head>

<body>
<?php
if(isset($_REQUEST["thezip"])){
	$thezip = $_REQUEST["thezip"];
	$zips = file_get_contents('http://www.census.gov/tiger/tms/gazetteer/zips.txt');
}

if(strpos($zips,$thezip)){
?>
your zip has been submitted! your zip is <?php echo $thezip; ?>
<?php 
} else {
?>
Error: your zip (<?php echo $thezip; ?>) is invalid.
<?php } ?>
</body>
</html>
 
Last edited:
Back
Top