1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how do I code this?

Discussion in 'Making Money' started by yoaussie2, Feb 19, 2009.

  1. yoaussie2

    yoaussie2 Registered Member

    Joined:
    Dec 31, 2008
    Messages:
    91
    Likes Received:
    6
    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!
     
  2. Arthas

    Arthas BANNED BANNED

    Joined:
    Jan 5, 2009
    Messages:
    637
    Likes Received:
    322
    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 } ?>
    
     
    • Thanks Thanks x 1
    Last edited: Feb 19, 2009
  3. yoaussie2

    yoaussie2 Registered Member

    Joined:
    Dec 31, 2008
    Messages:
    91
    Likes Received:
    6
    thanks Arthas, that was super quick! much appreciated
     
  4. Arthas

    Arthas BANNED BANNED

    Joined:
    Jan 5, 2009
    Messages:
    637
    Likes Received:
    322
    np :rolleyes:
     
  5. yoaussie2

    yoaussie2 Registered Member

    Joined:
    Dec 31, 2008
    Messages:
    91
    Likes Received:
    6
    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!
     
  6. gimme4free

    gimme4free Executive VIP Jr. VIP Premium Member

    Joined:
    Oct 22, 2008
    Messages:
    1,930
    Likes Received:
    1,945
    Home Page:
    What is on line one?
     
  7. yoaussie2

    yoaussie2 Registered Member

    Joined:
    Dec 31, 2008
    Messages:
    91
    Likes Received:
    6
    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!
     
  8. series

    series Registered Member

    Joined:
    Feb 12, 2008
    Messages:
    58
    Likes Received:
    6
    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>
    
     
  9. jimbobo2779

    jimbobo2779 Jr. VIP Jr. VIP

    Joined:
    Sep 17, 2008
    Messages:
    3,790
    Likes Received:
    2,690
    Occupation:
    Software Engineer
    Location:
    UK
    Home Page:
    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)
     
  10. yoaussie2

    yoaussie2 Registered Member

    Joined:
    Dec 31, 2008
    Messages:
    91
    Likes Received:
    6
    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>
     
  11. jimbobo2779

    jimbobo2779 Jr. VIP Jr. VIP

    Joined:
    Sep 17, 2008
    Messages:
    3,790
    Likes Received:
    2,690
    Occupation:
    Software Engineer
    Location:
    UK
    Home Page:
    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: Feb 19, 2009