redarrow Posted July 6, 2012 Share Posted July 6, 2012 This code allow a user to use a drop box and select a name then the code compares two arrays and then if a match sends a email to the user via the named array key. any body do it in a shorter way cheers. <html> <title></title> <?php if($_POST['submit']){ $switch_array=array("john"=>"john@hwat_ever.com", "bob"=>"bob@what_ever.com","petter"=>"petter@what_ever.com", "paul"=>"paul@what_ever.com","kevin"=>"kevin@what_ever.com", "chris"=>"chris@what_ever.com","tom"=>"tom@what_ever.com"); foreach($switch_array as $key=>$posted_match){ if($_POST['posted_people']=="$key"){ $to = ".$posted_match."; $subject = 'the subject'; $message = 'hello'; $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); if(mail($to, $subject, $message, $headers)){ echo "A email was sent to ".$posted_match.""; break; }else{ echo("Sorry no email sent out"); break; } break; } } } ?> <?php $people_array=array("john","bob","petter","paul","kevin","chris","tom");?> <center> <form method="POST" action=""> <select name="posted_people"> <?php foreach($people_array as $people){ echo"<option value='$people'>$people</option>";} ?> </select> <br /><br /> <input type="submit" name="submit" value="submit"> </form> </center> </html> Link to comment https://forums.phpfreaks.com/topic/265289-match-two-arrays-then-send-the-key-value-as-a-email-help/ Share on other sites More sharing options...
requinix Posted July 6, 2012 Share Posted July 6, 2012 if (isset($switch_array[(string)$_POST['posted_people']])) { $email = $switch_array[(string)$_POST['posted_people']]; // send to $email } else { // not found } Link to comment https://forums.phpfreaks.com/topic/265289-match-two-arrays-then-send-the-key-value-as-a-email-help/#findComment-1359574 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.