I have had my PHP working for a while now, but it has come to the point where i need it altering.
Basically, at the minute, the code works only when a user inputs a space in their postcode (eg: WF8 5TD) and will not work if it was entered like this: (eg: WF85TD)
I want the code to work so that it picks up on the first 2, 3 or 4 characters without the space in the postcode being an issue- or any other way so that if the user does not enter a space in their postcode, the form will still route to what page i want it to. The code i have is below:
<?php
//get the postcode
$agree = isset($_GET['agree']) ? trim($_GET['agree']) : '';
//convert postcode to uppercase
$agree = strtoupper($agree);
//find the position of the space in the postcode
$pos = strpos($agree, ' ');
//split the string so we are only looking at the chars up to the space
//we're splitting counting from 0 to $pos
$route = substr($agree, 0, $pos);
switch ($route) {
case "LA10":
case "LA11":
case "LA12":
case "LA13":
case "LA14":
case "LA15":
case "LA16":
case "LA17":
case "LA18":
case "LA19":
case "LA20":
case "LA21":
case "LA22":
header("Location: http://www.website.co.uk/abacus");
break;
It should be mentioned that i have email routing also which works as i want the above to. A snippet of the email routing code is below:
/* add_filter("gform_pre_submission", "before_email");
function before_email($form){
echo "<pre>";
print_r($form);
echo "</pre>"; exit;
$email["to"] = "xhtmlpoint@gmail.com";
$email["message"] = "This is my new message.";
$email["subject"] = "This is a new subject.";
return $email;
} */
add_filter('gform_notification', 'change_notification_email', 10, 3);
function change_notification_email( $notification, $form, $entry ) {
// /*echo "<pre>";
// print_r($entry[7]);
// echo "</pre>"; exit;*/
$postcode = strtoupper(ereg_replace("[^A-Za-z0-9]", "", trim($entry[7])));
$lenght = strlen($postcode);
if($lenght < 5 OR $lenght == 4){
$regex = '/^([A-Z]{1,2})([0-9][0-9A-Z]?)$/';
if (preg_match($regex, $postcode, $part)) {
$code = array(
'outer' => $part[1] . $part[2]
);
}else{
$code = NULL;
}
}else{
$regex = '/^([A-Z]{1,2})([0-9][0-9A-Z]?)s*([0-9])([A-Z]{2})$/';
if (preg_match($regex, $postcode, $part)) {
$code = array(
'outer' => $part[1] . $part[2]
);
}else{
$code = NULL;
}
}
if(is_null($code))
{
$notification['toType'] = "email";
$notification['to'] = "enquiries@hansongarages.co.uk";
}
else{
//There is no concept of admin notifications anymore, so we will need to target notifications based on other criteria, such as name
$abacus = array("LA10", "LA11", "LA12", "LA13", "LA14", "LA15", "LA16", "LA17", "LA18", "LA19", "LA20", "LA21", "LA22");
if(in_array($code['outer'], $abacus)){
// toType can be routing or email
$notification['toType'] = "email";
$notification['to'] = "exampleoftheemail@tiscali.co.uk";