Jump to content

Sending form to recipient based on location


mallen

Recommended Posts

I have a form that emails the content to a recipient. Now I want to direct the email to 6 different recipients based on the county they select. I got it to work with just two counties (cases). But my state has over 60 counties and I will be dividing by about 10 counties each. Is there a way to list 10 counties for each case? This is what I have.

switch ($to)
{
case 'County1':
$to = "Joe@mysite.com";
break;
case 'County2':
$to = "Bob@mysite";
break;


default:
  $to = "default@default.com";
}

Link to comment
Share on other sites

I believe you can stack cases like so:

 

<?php
$x = 5;
switch ($x) {
case 5:		
case 12:
	echo "x is 5 or 12";
	break;
default:
	echo "x is neither 5 nor 12";		
	break;
}
exit;
?>

 

You could also stash the counties in different arrays and then set $to based on a series of if statements using the in_array function:

 

http://nz.php.net/manual/en/function.in-array.php

Link to comment
Share on other sites

there is no limit to case. here's a snip from one of my scripts:

 

<?php 
switch($stl_type){
	default:$type = 'other-trucks';break;
	case 'car carrier truck':$type = 'auto-carrier';break;
	case 'van trucks / box trucks':$type = 'box-vans';break;
	case 'bucket truck / boom truck':$type = 'bucket-truck';break;
	case 'dump truck':
	case 'dump bodies only':
	case 'dump chassis truck':
	case 'dump/transfer':
	case 'flatbed-dump truck':$type = 'dump-trucks';break;
	case 'tow trucks':
	case 'roll-back tow truck':
	case 'wrecker tow truck':
	case 'tow truck':$type = 'tow-trucks ';break;
}
?>

 

notice:

 

<?php 
case 'dump truck':
	case 'dump bodies only':
	case 'dump chassis truck':
	case 'dump/transfer':
	case 'flatbed-dump truck':
$type = 'dump-trucks';
break;?>

so you can set multiple matches for an action. my statement is 3x that long, and i don't believe there is a functional limit. maybe there error is elsewhere, or syntax?

 

you could also put them in arrays:

 

<?php
$region1 = array('county1', 'county2', 'county3', 'etc';
$region2 = array('county4', 'county5', 'county6', 'etc';

if (in_array($needle, $region1) {
do_something(); // found your county in region 1
} else if (in_array($needle, $region2) {
do_something(); // found your county in region 2
}?>

 

if you do more, you may consider making them array keys and using array_key_exists, as that's faster than searching array values. but for 10 counties each region this works fine.

Link to comment
Share on other sites

Thanks for your replies.  This is what I came up with before I saw the replies. Is there any benefit to using an array over the way I did it?

 

(more text fields above this)
$county = $_POST['county'];

$to = $county;
switch ($to)
{
case ($to =='County1' || $to == 'County2' || $to == 'County3'):
$to = "email@address.com";
break;
case ($to == 'County4' || $to == 'County5' || $to == 'County6'):
$to = "email2@address.com";
break;

default:
  $to = "default@default.com";
}

Link to comment
Share on other sites

Your relationship between counties and email address should be stored in a data structure (database, array) so that all you need to do to change, add, or remove any of the relationships is to change the data that is stored in the data structure. The logic in your application should remain the same no matter how much data the is or what the actual relationship is that the data defines.

Link to comment
Share on other sites

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.