boontoony Posted December 2, 2008 Share Posted December 2, 2008 Hi there, I am trying to make a php form for a website, and I need a little help. My form will have fields for name, email, address, phone, enquiry and State (that you live in) and maybe more. Here is where I need the help : The 'state' will be a drop down menu, and depending on what state is chosen, the forms data will be sent to a different email address. There will be 7 'states', so 7 email addresses. Here is the code I have been working off of. I'm pretty new to this, so i would really appreciate it being spelt out for me. <?php if(isset($_POST['submit'])) { $to = "[email protected]"; $subject = "Enquiry"; $name_field = $_POST['name']; $email_field = $_POST['email']; $address_field = $_POST['address']; $ph_field = $_POST['phone']; $state_field = $_POST['state']; $enquiry_field = $_POST['enquiry']; foreach($_POST['check'] as $value) { $check_msg .= "Checked: $value\n"; } $body = "From: $name_field\n Address: $address_field\n Email: $email_field\n Contact Phone Number: $ph_field\n \n State: $state_field\n Enquiry: $enquiry_field \n"; echo "Thank you blah blah blah"; mail($to, $subject, $body); } else { echo "Sorry, an Error blah blah"; } ?> Thanks for your time. Link to comment https://forums.phpfreaks.com/topic/135108-solved-mail-form-sending-to-different-email-addresses/ Share on other sites More sharing options...
ratcateme Posted December 2, 2008 Share Posted December 2, 2008 have a look at a switch statement like this <?php if(isset($_POST['submit'])) { $to = "[email protected]"; $subject = "Enquiry"; $name_field = $_POST['name']; $email_field = $_POST['email']; $address_field = $_POST['address']; $ph_field = $_POST['phone']; $state_field = $_POST['state']; $enquiry_field = $_POST['enquiry']; switch($state_field){ case 'ca': $to = "[email protected]"; break; case 'ny': $to = "[email protected]"; break; } foreach($_POST['check'] as $value) { $check_msg .= "Checked: $value\n"; } $body = "From: $name_field\n Address: $address_field\n Email: $email_field\n Contact Phone Number: $ph_field\n \n State: $state_field\n Enquiry: $enquiry_field \n"; echo "Thank you blah blah blah"; mail($to, $subject, $body); } else { echo "Sorry, an Error blah blah"; } ?> but i would personally use a database for that so you can add more states and easily change the emails. Scott. Link to comment https://forums.phpfreaks.com/topic/135108-solved-mail-form-sending-to-different-email-addresses/#findComment-703736 Share on other sites More sharing options...
boontoony Posted December 2, 2008 Author Share Posted December 2, 2008 Thanks for the quick response! Works a treat. Another thing - How do I make the email address, that the person enters in the email field, show up as the "from" email address when say "[email protected]" receives the email with the data? i have tested it on my server, and the from email address is just the name of my server. Link to comment https://forums.phpfreaks.com/topic/135108-solved-mail-form-sending-to-different-email-addresses/#findComment-703765 Share on other sites More sharing options...
elite_prodigy Posted December 2, 2008 Share Posted December 2, 2008 Add it in the headers section. Look here: http://www.php.net/mail Link to comment https://forums.phpfreaks.com/topic/135108-solved-mail-form-sending-to-different-email-addresses/#findComment-703780 Share on other sites More sharing options...
boontoony Posted December 2, 2008 Author Share Posted December 2, 2008 Thank you! Link to comment https://forums.phpfreaks.com/topic/135108-solved-mail-form-sending-to-different-email-addresses/#findComment-703811 Share on other sites More sharing options...
boontoony Posted December 2, 2008 Author Share Posted December 2, 2008 Another question, Just say whatever State is chosen in the 'State' drop down menu, determines what towns are listed on the 'Town' drop down menu. so if i chose state '3a' the Town menu would list 'sophie', 'milly' and 'dixie' opposed to choosing state '2a' which would list 'bill', 'bob' and 'fred'. <?php if(isset($_POST['submit'])) { $header = "From: ". $name_field . " <" . $email_field . ">\r\n"; $subject = "Test"; $name_field = $_POST['name']; $email_field = $_POST['email']; $message = $_POST['message']; $state_field = $_POST['drop_down']; $town_field = $_POST['drop_down2']; $ph_field = $_POST['phone']; switch($state_field){ case '1a': $to = "[email protected]"; break; case '2a': $to = "[email protected]"; break; case '3a': $to = "[email protected]"; break; case '4a': $to = "[email protected]"; break; case '5a': $to = "[email protected]"; break; case '6a': $to = "[email protected]"; break; case '7a': $to = "[email protected]"; break; } $body = "From: $name_field\n E-Mail: $email_field\n Phone: $ph_field\n Address:\n $message\n State: $state_field\n Town: $town_field\n"; echo "Your e-mail has been sent!"; mail($to, $subject, $body, $header); } else { echo "Sorry, there was an ERROR."; } ?> <form method="POST" action="mail.php"> Name: <input name="name" type="text" class="contactfields" size="40"> E-Mail: <input name="email" type="text" class="contactfields" size="40"> Phone: <input name="phone" type="text" class="contactfields" size="30"> Message: <textarea name="message" cols="40" rows="9" wrap="virtual" class="contactfields"></textarea> State: <select name="drop_down" size="1" class="contactfields"> <option selected="selected">1a</option> <option>2a</option> <option>3a</option> <option>4a</option> <option>5a</option> <option>6a</option> </select> Town: <select name="drop_down2" size="1" class="contactfields"> <option>bob</option> <option>bill</option> <option>fred</option> </select> <input name="submit" type="submit" class="contactfields" value="Submit"> </form> Thanks again. Link to comment https://forums.phpfreaks.com/topic/135108-solved-mail-form-sending-to-different-email-addresses/#findComment-703825 Share on other sites More sharing options...
boontoony Posted December 2, 2008 Author Share Posted December 2, 2008 and any tips on validating certain fields would be appreciated. Link to comment https://forums.phpfreaks.com/topic/135108-solved-mail-form-sending-to-different-email-addresses/#findComment-704384 Share on other sites More sharing options...
ratcateme Posted December 3, 2008 Share Posted December 3, 2008 you need javascript to do that it could be done with ajax or just normal javascript. i don't know much javascript but i might be able to help a little. Scott. Link to comment https://forums.phpfreaks.com/topic/135108-solved-mail-form-sending-to-different-email-addresses/#findComment-704557 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.