Sixmax Posted September 11, 2008 Share Posted September 11, 2008 Hey I was wondering how to make an email contact form in PHP. I have tried to write one in HTML, but I can't do that if I want some functions. This is what I want of functions: A contact form where people on my homepage can write a message to different persons, like: 1) Name. 2) Email. 3) A field where people can choose whom to send the message to (like a drop down box). 4) Subject. 5) Message. 6) A submit button where (if the form is filled out correctly) people get an pop-up that the message is send correctly. I am totally new to PHP, so if anybody got a link to a guide, or maybe got a premade code, it would be very nice. Thanks in advance. Sixmax Quote Link to comment https://forums.phpfreaks.com/topic/123740-making-a-php-email-contact-form/ Share on other sites More sharing options...
BlueSkyIS Posted September 11, 2008 Share Posted September 11, 2008 http://www.google.com/search?rls=en-us&q=php+email+contact+form&ie=UTF-8&oe=UTF-8 Quote Link to comment https://forums.phpfreaks.com/topic/123740-making-a-php-email-contact-form/#findComment-638973 Share on other sites More sharing options...
sam_h Posted September 11, 2008 Share Posted September 11, 2008 i also found this tut very useful when applying CSS styles to my forms http://www.jankoatwarpspeed.com/post/2008/07/27/Enhance-your-input-fields-with-simple-CSS-tricks.aspx Quote Link to comment https://forums.phpfreaks.com/topic/123740-making-a-php-email-contact-form/#findComment-639003 Share on other sites More sharing options...
Sixmax Posted September 11, 2008 Author Share Posted September 11, 2008 Great links! But I still have problems making the drop down box where the writer can choose receiver. Any suggestions? Sixmax Quote Link to comment https://forums.phpfreaks.com/topic/123740-making-a-php-email-contact-form/#findComment-639093 Share on other sites More sharing options...
Sixmax Posted September 11, 2008 Author Share Posted September 11, 2008 Okay - now i have made the HTML look: <form method="post" action="contact.php"> <table border="0" cellpadding="0" cellspacing="0" align="left"> <tr> <td><h3 style="border-bottom:1px solid black;">Contact</h3></td> </tr> <tr> <td> <label for="Name" style="display:block;margin-bottom:5px;">Name:</label><input type="text" name="Name" id="Name" value="" maxlength="" style="width:200px;"><div style="clear:left;height:20px;"> </div> <label for="Email" style="display:block;margin-bottom:5px;">Email:</label><input type="text" name="Email" id="Email" value="" maxlength="" style="width:200px;"><div style="clear:left;height:20px;"> </div> <label style="display:block;margin-bottom:5px;" for="Receiver">Receiver:</label><select name="Receiver" id="Receiver"><option value="[email protected]">Person 1</option><option value="[email protected]">Person 2</option><option value="[email protected]">Person 3</option><option value="[email protected]">Person 4</option></select><div style="clear:left;height:20px;"> </div> <label for="Name" style="display:block;margin-bottom:5px;">Subject:</label><input type="text" name="Name" id="Name" value="" maxlength="" style="width:200px;"><div style="clear:left;height:20px;"> </div> <label for="Message" style="display:block;margin-bottom:5px;">Message:</label><textarea name="Message" id="Message" style="width:200px;height:200px;"></textarea><div style="clear:left;height:20px;"> </div> </td> <tr> <td align="left"> <input type="submit" value=" Submit "> <input type="reset" value=" Reset "> </td> </tr> </table> </form> The question is now - how to create the contact.php so that if "Person 2" is chosen at the drop down box, then the mail is sent to the correct email (in this case [email protected] - just example emails). I have seen a lot of free copy-pasteable forms on the net, but they only handle the possibility of one receiver - not to choose between different receivers. Hope that some PHP freak got the answer :-) Sixmax Quote Link to comment https://forums.phpfreaks.com/topic/123740-making-a-php-email-contact-form/#findComment-639154 Share on other sites More sharing options...
ngreenwood6 Posted September 11, 2008 Share Posted September 11, 2008 You would do an if/else statement: ex. <?php $contact1 = "[email protected]"; $contact2 = "[email protected]"; $contact3 = "[email protected]"; if($contact1) { mail(to, subject, message) } elseif($contact2) { mail(to, subject, message) } else { mail(to, subject, message)//this mails $contact3 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/123740-making-a-php-email-contact-form/#findComment-639236 Share on other sites More sharing options...
Sixmax Posted September 11, 2008 Author Share Posted September 11, 2008 So i would like to rename the emails i have written in options in HTML to "contact1", "contact2" and so on, and just write their emails in the PHP? Quote Link to comment https://forums.phpfreaks.com/topic/123740-making-a-php-email-contact-form/#findComment-639295 Share on other sites More sharing options...
ngreenwood6 Posted September 11, 2008 Share Posted September 11, 2008 This is what it would look like for you: <?php $get_name = $_POST['name']; $get_email = $_POST['email']; $get_receiver = $_POST['receiver']; $get_subject = $_POST['subject']; $get_message = $_POST['message']; $to1 = "[email protected]"; $to2 = "[email protected]"; $to3 = "[email protected]"; $to4 = "[email protected]"; $subject = $get_subject; $message = $get_message; $header = 'MIME-Version: 1.0' . "\r\n"; $header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $header .= "From: $get_email"; if($get_receiver == $to1) { mail($to1, $subject, $message, $header) } elseif($get_receiver == $to2) { mail($to2, $subject, $message, $header) } elseif($get_receiver == $to3) { mail($to3, $subject, $message, $header) } else { mail($to4, $subject, $message, $header) ?> Modify for your needs but you should get it from here. Quote Link to comment https://forums.phpfreaks.com/topic/123740-making-a-php-email-contact-form/#findComment-639374 Share on other sites More sharing options...
kenrbnsn Posted September 11, 2008 Share Posted September 11, 2008 You shouldn't put the email addresses into your form, since they will be screen scraped, instead do something like this: <form method="post" action="contact.php"> <table border="0" cellpadding="0" cellspacing="0" align="left"> <tr> <td><h3 style="border-bottom:1px solid black;">Contact</h3></td> </tr> <tr> <td> <label for="Name" style="display:block;margin-bottom:5px;">Name:</label><input type="text" name="Name" id="Name" value="" maxlength="" style="width:200px;"><div style="clear:left;height:20px;"> </div> <label for="Email" style="display:block;margin-bottom:5px;">Email:</label><input type="text" name="Email" id="Email" value="" maxlength="" style="width:200px;"><div style="clear:left;height:20px;"> </div> <label style="display:block;margin-bottom:5px;" for="Receiver">Receiver:</label><select name="Receiver" id="Receiver"><option value="1">Person 1</option><option value="2">Person 2</option><option value="3">Person 3</option><option value="4">Person 4</option></select><div style="clear:left;height:20px;"> </div> <label for="Name" style="display:block;margin-bottom:5px;">Subject:</label><input type="text" name="Name" id="Name" value="" maxlength="" style="width:200px;"><div style="clear:left;height:20px;"> </div> <label for="Message" style="display:block;margin-bottom:5px;">Message:</label><textarea name="Message" id="Message" style="width:200px;height:200px;"></textarea><div style="clear:left;height:20px;"> </div> </td> <tr> <td align="left"> <input type="submit" value=" Submit "> <input type="reset" value=" Reset "> </td> </tr> </table> </form> and in the processing script: <?php $emails = array('','[email protected]','[email protected]','[email protected]','[email protected]'); if ($_POST['Receiver'] > 0 && $_POST['Receiver'] < 5) $to = $emails[$_POST['Receiver']]; else die('Bad Receiver recieved: ' . htmlentities($_POST['Receiver'])); // // rest of your code // ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/123740-making-a-php-email-contact-form/#findComment-639429 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.