Jump to content

Routing PHP Code - Help!


mylesrobinson

Recommended Posts

Hi

 

I am trying to get some code working correctly so that i can route users to a URL redirect when completing a form on our website. There are only 2 URL's to redirect to but there are a range of post codes that i need to add to the code.

 

At the minute the code works only if the letters are the same case (Caps or not caps) and when i enter more than one postcode, the form comes back with a PHP error.

 

I basically need to be able to let the user enter a postcode and the code will work for the first 3 AND 4 (sometimes) characters. For example a post code may be EH2 or EH34 and i would need the form to be able to do this.

 

Sorry for the lack of knowledge on this but i hope someone can help. The code is posted below what i currently have.

 

<?php
$agree = $_GET['agree'];
switch ($agree)
{
case "ky1":
break;
case "FK", :
break;
}
?>
Edited by mylesrobinson
Link to comment
Share on other sites

I'm not really understanding what's actually happening. Can you provide actual examples of it working and it not working? Having a hard time making a connection between $agree and your switch statement, combined with the requirement here

 

I basically need to be able to let the user enter a postcode and the code will work for the first 3 AND 4 (sometimes) characters. For example a post code may be EH2 or EH34 and i would need the form to be able to do this.

 

Are you trying to do a string search? Or exact match? Does it need to be case sensitive? Also, you didn't include the error you get back when it doesn't work.

Link to comment
Share on other sites

Hi

 

Thanks for your reply.

 

I think the $agree bit is wrong.

 

I am basically wanting redirects to one of 2 websites based upon data from the first 3 and 4 characters of the postcode when entered into the form i have set up in gravity forms. It doesn't need to be case sensitive (which i think it is in the above code).

 

I am not very experienced in PHP but i am sure that the code doesn't need altering too much from what i have already?

Link to comment
Share on other sites

You can use strtolower to convert what they typed to lowercase, then you only need to check the lowercase values. It looks like you have a stray comma in your code after "FK" though.The above code will work if you're trying to match the entire string. If you're only trying to match the first few characters then you could use a function like strpos to check if the characters exist, or substr to get the first few characters and test those, or a regular expression to say you're looking for any string that starts with something.

Link to comment
Share on other sites

Hi

 

Thanks for your reply. I will show you what bits of the postcodes i need for each of the 2 URL redirects:

 

<?php
$agree = $_GET['agree'];
switch ($agree)
{
case ("ky1", "ky2", "ky3", "ky12", "ky13", "ky14":)
break;
case ("fk1", "fk12", "fk13":)
break;
}
?>
So as you see, i have a range of data that someones postcode may start with. If it does start with any of those in the code above then they will route to that specified URL.
Link to comment
Share on other sites

You would do that like this:

case 'ky1':case 'ky2':case 'ky3':...
A switch statement, once it finds a matching case statement, will keep executing until it finds a break statement. If you want multiple cases to do the same thing then that's the way you do it.
Link to comment
Share on other sites

HiThanks for that, it works. However a lot of the time they will enter a postcode such as fk1 4eh so I would need it to only pick up on the first 3 and sometimes 4 characters. Another point is that they are not actually case sensitive so would the 'case' bit be applicable? Thanks for your help

Link to comment
Share on other sites

Both of your questions were answered previously.

 

You can use strtolower to convert what they typed to lowercase, then you only need to check the lowercase values. It looks like you have a stray comma in your code after "FK" though.The above code will work if you're trying to match the entire string. If you're only trying to match the first few characters then you could use a function like strpos to check if the characters exist, or substr to get the first few characters and test those, or a regular expression to say you're looking for any string that starts with something.

 

First, you use substr() to get the first few letters, then convert them all to lower case using strtolower(). Then you use the switch-case construct to decide what to do with the resulting value.

Link to comment
Share on other sites

Ok thanks. Sorry for the confusion. I am oblivious to where to put the code suggested though. I am very inexperienced with writing code but i know how to upload it and understand the code that i see (mostly!) but unable to figure out where i need to put the substr(), strtolower() and strpos in the code that i put in my original post. I will keep trying to work it out.

Link to comment
Share on other sites

Just take it one step at a time. You've more or less been given the flow, so just try writing the steps out in psuedo-code first, i.e.

  1. Obtain value from GET parameter
  2. Make it all lowercase
  3. Get the first three or four characters
  4. Run it through a switch case
  5. etc

Write out the steps of the program in very clear and specific steps and then start working from there. Validate EACH step before moving in. So don't go to step 3 unless you are correctly getting your GET data and it is indeed all lower case. Use echo or print statements to debug your code and make sure the values of things are exactly what they are supposed to be.

Link to comment
Share on other sites

  • 2 months later...

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

 

Link to comment
Share on other sites

You need to normalize the data first, one way to do that would be to remove the space so that all post codes do not have a space. Then you can write your code to get the first 2, 3, or 4 characters (whatever the requirements are for that).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...