Jump to content

[SOLVED] I thought this would be a simple script?


Recommended Posts

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";
						  }
						  ?>

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

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);

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 :D

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

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.