I am trying to come up with a better way to account for bad entries on a postcode form.
The form checks the postcode against a database and sends them to the right store. But it only works on the first 3 or 4 characters of the UK postcode.
I have looked through the forum, but can't find an exact equivalent of what I am doing.
This is the code I am up to - but clearly the structure is wrong, somewhow.
<?php
if(isset($_POST['Submit1'])){
if (strlen($pcodesrch) >4) {
$pcodesrch = strtoupper(trim($postcode));
$pcodesrch = trim($_POST['pcodesrch']);
$pcodesrch = str_replace(" ","",$pcodesrch);
$postcode = substr($pcodesrch,0,strlen($pcodesrch)-3);
// Conditional statement
if(!empty($pcodesrch)){
// Connect to database
$eg_objConn1 = mysql_connect("localhost", "xxxxx", "xxxx");
mysql_select_db("xxxxxx", $eg_objConn1);
$pcode = mysql_real_escape_string($postcode);
// Get Record Set
$eg_recResult1 = mysql_query("SELECT pcode, urlref FROM destinate
WHERE pcode='".$pcode."'", $eg_objConn1);
$num_rows = mysql_num_rows($eg_recResult1);
if($num_rows){
$eg_Result1 = mysql_fetch_array($eg_recResult1, MYSQL_ASSOC);
// Go to page
header("Location: ".$eg_Result1['urlref']."");
exit();
}
}
echo "We did not recognise your postcode, did you enter the postcode correctly?";
exit();
} elseif (strlen($pcodesrch) <4) {
if(!empty($pcodesrch)){
// Connect to database
$eg_objConn1 = mysql_connect("localhost", "xxxxxx", "xxxxx");
mysql_select_db("xxxxxxx", $eg_objConn1);
$pcode = mysql_real_escape_string($pcodesrch);
// Get Record Set
$eg_recResult1 = mysql_query("SELECT pcode, urlref FROM destinate
WHERE pcode='".$pcode."'", $eg_objConn1);
$num_rows = mysql_num_rows($eg_recResult1);
if($num_rows){
$eg_Result1 = mysql_fetch_array($eg_recResult1, MYSQL_ASSOC);
// Go to page
header("Location: ".$eg_Result1['urlref']."");
exit();
}
}
echo "We did not recognise your postcode, did you enter the postcode correctly?";
exit();
}
?>
What am I doing wrong?