fri3ndly Posted September 27, 2007 Share Posted September 27, 2007 I thought it would be easily achievable but nomatter what I try I cant seem to get this to work. Basically I am trying to make an area where a customer can enter their postcode, and it will tell them whether they are in my clients region of work or not. Eg: 1. Customer enters 'BR2 6TH' 2. Looks up 'BR2' in a string 3. Returns True or false See... sounds easy.... where am I going wrong?? <?php $postcode=$_POST ['postcode']; $postcode=strtoupper($postcode); $postcode=stripslashes($postcode); $areas='BR1'OR'BR2'OR'BR3'; if ($postcode==$areas){ echo ("$postcode is in area!!!"); } elseif($postcode==""){ echo ""; }else{ echo "Sorry, the postcode $postcode is outside of our work region"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70911-solved-i-thought-this-would-be-a-simple-script/ Share on other sites More sharing options...
HuggieBear Posted September 27, 2007 Share Posted September 27, 2007 Try this... <?php // Set the areas you cover $areas = array('BR1','BR2','BR3'); // Get the postcode from the form $postcode = strtoupper($_POST['postcode']); // Check the areas against the postcode entered if (in_array(substr($postcode, 0, 3), $areas)){ echo "$postcode is in area!!!"; } else { echo "Sorry, the postcode $postcode is outside of our work region"; } ?> This is basic and will need lots more validation. Regards Huggue Quote Link to comment https://forums.phpfreaks.com/topic/70911-solved-i-thought-this-would-be-a-simple-script/#findComment-356466 Share on other sites More sharing options...
fri3ndly Posted September 27, 2007 Author Share Posted September 27, 2007 Thank you... I can see what you have done and it looks liek it should work, but nothing is displaying at all when submit is clicked now? Quote Link to comment https://forums.phpfreaks.com/topic/70911-solved-i-thought-this-would-be-a-simple-script/#findComment-356474 Share on other sites More sharing options...
HuggieBear Posted September 27, 2007 Share Posted September 27, 2007 Check you've copied and pasted it correctly, as I've just checked it and it works. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70911-solved-i-thought-this-would-be-a-simple-script/#findComment-356478 Share on other sites More sharing options...
mattal999 Posted September 27, 2007 Share Posted September 27, 2007 i tried this, but it needs to change a postcode like "BR1 TWO" to just "BR1": check.php <?php $postcode = $_POST['postcode']; $postcode = strtoupper($postcode); $postcode = stripslashes($postcode); $area1='BR1'; $area2='BR2'; $area3='BR3'; if ($postcode == $area1) { echo ("$postcode is in area!!!"); } else if ($postcode == $area2) { echo ("$postcode is in area!!!"); } else if ($postcode == $area3) { echo ("$postcode is in area!!!"); } else if($postcode == "") { echo "Invalid Postcode"; } else { echo "Sorry, the postcode $postcode is outside of our work region"; } ?> and you need to do is make a page: index <form action='check.php' method='POST'> <input type='text' name='postcode' size="20"><br><input type='submit' value='Check'> EDIT: edit check.php: $postcode = $_POST['postcode']; $postcode = strtoupper($postcode); $postcode = stripslashes($postcode); $postcode = substr($postcode, 0, 3); Quote Link to comment https://forums.phpfreaks.com/topic/70911-solved-i-thought-this-would-be-a-simple-script/#findComment-356480 Share on other sites More sharing options...
fri3ndly Posted September 27, 2007 Author Share Posted September 27, 2007 Check you've copied and pasted it correctly, as I've just checked it and it works. Regards Huggie Working fine! I must have pasted it incorrectly. Thanks mate @ mattal999 - Thanks too, but this script would be mental as I have about 40 postcodes to chech Quote Link to comment https://forums.phpfreaks.com/topic/70911-solved-i-thought-this-would-be-a-simple-script/#findComment-356484 Share on other sites More sharing options...
mattal999 Posted September 27, 2007 Share Posted September 27, 2007 ok, cheers mate Quote Link to comment https://forums.phpfreaks.com/topic/70911-solved-i-thought-this-would-be-a-simple-script/#findComment-356486 Share on other sites More sharing options...
HuggieBear Posted September 27, 2007 Share Posted September 27, 2007 Check you've copied and pasted it correctly, as I've just checked it and it works. Working fine! I must have pasted it incorrectly. Thanks mate Don't forget, you'll need to do some validation on this first, as if I entered my postcode as BR21 then it'd tell me it's in your area, even though it's actually not as it's only looking at the first three characters. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70911-solved-i-thought-this-would-be-a-simple-script/#findComment-356500 Share on other sites More sharing options...
fri3ndly Posted September 28, 2007 Author Share Posted September 28, 2007 OK Thanks.....So this all works now (kind of): <?php // Set the areas you cover $areas = array('BR1','BR2','BR3','BR4','BR5','BR6','BR7','BR8','DA1','DA4','DA5','DA6','DA7','DA8','DA14','DA15','DA16','DA17','DA18','SE1','SE2','SE3','SE4','SE5','SE6','SE7','SE8','SE9','SE10','SE11','SE12','SE13','SE14','SE15','SE16','SE17','SE18','SE19','SE20','SE21','SE22','SE23','SE24','SE25','SE26','SE27','SE28','SW2','SW4','SW8','SW9','SW11','SW12','SW16','SW17','SW18','CR0','CR2','CR4','CR7','CR8'); // Get the postcode from the form and change to Upper case $postcode = strtoupper($_POST['postcode']); // Check the areas against the postcode entered if (in_array(substr($postcode, 0, 4), $areas)){ echo "Great news!<br><br><strong>$postcode</strong> is within our working area."; } else { echo "Sorry!<br><br><strong>$postcode</strong> is outside of our working area."; } ?> but the problem is like you said some postcodes have 4 first letters and some have three, so if somebody was to put 'BR1 4AD' it would say it was not in the area because it would read it as 'BR14'. Does there need to be another if function telling the script that if the postcode has 3 letters only read 3 and if it has 4 then to read 4>? I wouldnt know where to start with this lol. If someone could help me that would be great Thanks Quote Link to comment https://forums.phpfreaks.com/topic/70911-solved-i-thought-this-would-be-a-simple-script/#findComment-357026 Share on other sites More sharing options...
HuggieBear Posted September 28, 2007 Share Posted September 28, 2007 I'll take a look at this a little later this morning if I get the chance. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70911-solved-i-thought-this-would-be-a-simple-script/#findComment-357031 Share on other sites More sharing options...
fri3ndly Posted September 28, 2007 Author Share Posted September 28, 2007 Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/70911-solved-i-thought-this-would-be-a-simple-script/#findComment-357034 Share on other sites More sharing options...
HuggieBear Posted September 28, 2007 Share Posted September 28, 2007 Friendly, as promised, working code based on the UK Government Data Standards Catalogue entry for Postcodes. <?php // Get the postcode from the url $postcode = $_GET['postcode']; // Split out any spaces $postcode = str_replace(" ", "", $postcode); // Specify what our areas actually are $areas = array('BR1','BR2','BR3','BR4','BR5','BR6','BR7','BR8','DA1','DA4','DA5','DA6','DA7','DA8','DA14','DA15','DA16','DA17','DA18','SE1','SE2','SE3','SE4','SE5','SE6','SE7','SE8','SE9','SE10','SE11','SE12','SE13','SE14','SE15','SE16','SE17','SE18','SE19','SE20','SE21','SE22','SE23','SE24','SE25','SE26','SE27','SE28','SW2','SW4','SW8','SW9','SW11','SW12','SW16','SW17','SW18','CR0','CR2','CR4','CR7','CR8'); // Use length to help check the different formats for UK postcodes $char = strlen($postcode); if (($char == 5) && preg_match('/([a-z]\d)\d[a-z]{2}/', $postcode, $matches)){ // S1 2AB if (in_array(strtoupper($matches[1]), $areas)){ $match = 1; } } elseif (($char == 6) && preg_match('/([a-z]{2}\d)\d[a-z]{2}/i', $postcode, $matches)){ // BR1 2AB if (in_array(strtoupper($matches[1]), $areas)){ $match = 1; } } elseif (($char == 6) && preg_match('/([a-z]{1}\d{2})\d[a-z]{2}/i', $postcode, $matches)){ // S12 3AB if (in_array(strtoupper($matches[1]), $areas)){ $match = 1; } } elseif (($char == 7) && preg_match('/([a-z]{2}\d{2})\d[a-z]{2}/i', $postcode, $matches)){ // BR12 3AB if (in_array(strtoupper($matches[1]), $areas)){ $match = 1; } } elseif (($char == 7) && preg_match('/([a-z]{2}\d[a-z])\d[a-z]{2}/i', $postcode, $matches)){ // SW1W 2AB if (in_array(strtoupper($matches[1]), $areas)){ $match = 1; } } else { echo "Sorry, your postcode appears to be invalid"; exit(); } // Echo the output to the screen if ($match){ echo "Congratulations, " . strtoupper($matches[1]) . " is in our area<br>\n"; } else { echo "Sorry, " . strtoupper($matches[1]) . " isn't in our area<br>\n"; } ?> Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70911-solved-i-thought-this-would-be-a-simple-script/#findComment-357048 Share on other sites More sharing options...
fri3ndly Posted September 28, 2007 Author Share Posted September 28, 2007 Hi Huggiebear Looks good! I replaced it with my PHP text and it has changed the display and is not working. Lol, I also tried it a few times to make sure I didnt paste it incorrectly. I'l go through thie code to see if I can see anything that might be causing it Quote Link to comment https://forums.phpfreaks.com/topic/70911-solved-i-thought-this-would-be-a-simple-script/#findComment-357059 Share on other sites More sharing options...
fri3ndly Posted September 28, 2007 Author Share Posted September 28, 2007 Think Iv sorted it. I was using Post instead of GET Quote Link to comment https://forums.phpfreaks.com/topic/70911-solved-i-thought-this-would-be-a-simple-script/#findComment-357061 Share on other sites More sharing options...
HuggieBear Posted September 28, 2007 Share Posted September 28, 2007 No problem, I wrote it as GET to save me creating a form when developing it. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70911-solved-i-thought-this-would-be-a-simple-script/#findComment-357062 Share on other sites More sharing options...
fri3ndly Posted September 28, 2007 Author Share Posted September 28, 2007 Cool no probs, thanks mate! Quote Link to comment https://forums.phpfreaks.com/topic/70911-solved-i-thought-this-would-be-a-simple-script/#findComment-357065 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.