Anyone know how to program this

Discussion in 'PHP' started by Lukas, May 16, 2007.

  1. #1
    I need to add a line to a form-mail script.

    I basically want it to route to xyz's email if address if "state" field equals FL, NY
    and if it equals SC or NC then send to wxyz..and so on...any tips..?

    if you need to see the form-mail script...it is here
    http://www.dbmasters.net/index.php?id=4

    open to suggestion on anther script too..

    thanks
     
    Lukas, May 16, 2007 IP
  2. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Depending on what you want to do, you could use:

    <?php
    // Post state variable
    	$state = $_POST['state'];
    
    // Assign a numerical value to each state via an array (FL = 1, SC = 3, etc.)
    	$states_arr = array("FL", "NY", "SC", "NC");
    
    // Perform the desired action based on the numerical value mentioned above
    	switch ($states_arr[$state]){
    		case 0:
    		case 1:
    			/* Mail to xyz */
    			break;
    		case 2:
    		case 3:
    			/* Mail to wxyz */
    			break;
    		default:
    			/* Default Action */
    			break;
    	}
    ?>
    PHP:
    Or you could use the following:

    <?php
    // Post state variable
    	$state = $_POST['state'];
    
    // Assign states to different arrays for each e-mail address
    	$xyz_arr = array("FL", "NY");
    	$wxyz_arr = array("SC", "NC");
    
    // Perform the desired action based on the array $state is in
    	if ( in_array($state, $xyz_arr) ){
    		/* Mail to xyz */
    	} else if ( in_array($state, $wxyz_arr) ){
    		/* Mail to wxyz */
    	} else {
    		/* Default Action */
    	}
    ?>
    PHP:
     
    dp-user-1, May 16, 2007 IP
  3. Lukas

    Lukas Well-Known Member

    Messages:
    1,299
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    130
    #3
    thanks I will try that soon and update all.
     
    Lukas, May 17, 2007 IP
  4. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Correction to one of my comments: it should read, "FL = 0, SC = 2."
     
    dp-user-1, May 17, 2007 IP
  5. Lukas

    Lukas Well-Known Member

    Messages:
    1,299
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    130
    #5
    thanks for the suggestions.

    is this the easiest way ?, if for example, I have 2 people each from 20 states ( total 40) to send info to:

    so I would then have 40 email addresses to input ?

    I wish it was as easy as grouping into arrays
     
    Lukas, May 20, 2007 IP
  6. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Take a look at this and tell me if you like it:
    <?php
    // Post state variable
    	$state = $_POST['state'];
    	
    // Assign states to different e-mail addresses using arrays
    	$states_arr = array(
    		array("FL", "NY", "xyz@email.com"), 
    		array("SC", "NC", "wxyz@email.com")
    	);
    	
    // If $state is in one of the above arrays, set the e-mail to its last element
    	foreach ($states_arr as $test){
    		if ( in_array($state, $test) ){
    			$email = end($test);
    			break;
    		} else { // If not, e-mail to the default address
    			$email = "default@email.com";
    		}
    	}
    	
    // Mail based on the state's array
    	$subject = "Subject";
    	$message = "Blah, blah, blah!";
    
    	mail($email, $subject, $message);
    ?>
    PHP:
     
    dp-user-1, May 20, 2007 IP