murray price Posted July 24, 2012 Share Posted July 24, 2012 Hi, I am trying to put together a script that allows visitors to my site to enter a postcode in a search box and then if the postcode matches one that is on a list the visitor is sent to a success page if not a fail page. This is the script I have so far: <?php $postcode_area = "kt18"; if(isset ($_POST["postcode_area"])){ if($_POST["postcode_area"] == $postcode_area){ header("Location: ../area-covered.htm"); exit(); } else{ header("Location: ../area-not-covered.htm"); exit(); } } ?> This works fine if the user enters the one postcode shown 'KT18' but I have about 100 I want to include for the script. Can anybody help me we out with this? I kinda know that I need to do a for each loop and explode it but I am just not advanced enough yet to pull this one off! Thanks for any help Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 24, 2012 Share Posted July 24, 2012 1. Use code tags. 2. What format is the list of 100 post codes now? 3. in_array Quote Link to comment Share on other sites More sharing options...
murray price Posted July 24, 2012 Author Share Posted July 24, 2012 It is just list like: kt18 sm3 sw11 etc The person will only be entering the first block of their postcode and their are about a 100 of these. Thanks Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 24, 2012 Share Posted July 24, 2012 So it's in a text file? Put it in an array, and use in_array. Or import it into a database. Quote Link to comment Share on other sites More sharing options...
murray price Posted July 24, 2012 Author Share Posted July 24, 2012 I've got no idea how to do that Quote Link to comment Share on other sites More sharing options...
xyph Posted July 24, 2012 Share Posted July 24, 2012 <?php $valid = array('kt18','sm3','sw11','etc'); $lowercase_postal = strtolower($_POST['postal_code']); if( in_array($lowercase_postal, $valid) ) { echo 'Found'; } else { echo 'Not found'; } ?> Quote Link to comment Share on other sites More sharing options...
murray price Posted July 24, 2012 Author Share Posted July 24, 2012 Hi, Thanks. I have changed the echo's to header to redirect to a new page but when I add any search now it just directs to the fail page. <?php $valid = array('kt18','sm3','sw11','etc'); $lowercase_postal = strtolower($_POST['postal_code']); if( in_array($lowercase_postal, $valid) ) { header("Location: ../area-covered.htm"); exit(); } else { header("Location: ../area-not-covered.htm"); exit(); } ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 24, 2012 Share Posted July 24, 2012 You searched for one of those three that you added? In your original code you called the input "postcode_area" now you have "postal_code" Quote Link to comment Share on other sites More sharing options...
murray price Posted July 24, 2012 Author Share Posted July 24, 2012 Its ALIVE! Thanks for all your help... <?php $valid = array('kt18','sm3','sw11','etc'); $lowercase_postal = strtolower($_POST['postcode_area']); if( in_array($lowercase_postal, $valid) ) { header("Location: ../area-covered.htm"); exit(); } else { header("Location: ../area-not-covered.htm"); exit(); } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.