Jump to content

Contact form help.


Braveheartt

Recommended Posts

Hello! I'm brand new to PHP. I've made a simple contact form in HTML and PHP. It works perfectly. Now I'm having a little trouble with being able to choose who to send the completed form to! I have removed/changed all the links and email addresses on purpose. Here we go:

 

This is my mail.php code:

 

<?php 
if(isset($_POST['submit'])) { 
$to = "[email protected]";  
$name_field = $_POST['name']; 
$email_field = $_POST['email']; 
$message_cat = $_POST['message_cat'];
$message = $_POST['message']; 
$subject = "$message_cat";
$option = $_POST['radio'];

  
$body = "From:\n $name_field\n\n Email Address:\n $email_field\n\n Department: $option\n Message category:\n $message_cat\n\n Message:\n\n $message"; 
  
mail($to, $subject, $body); 
header('Location:*removed*); 
} else { 
echo "Oops! You shouldn't be here!"; 
} 
?> 

 

Alright, that works perfectly with the accompanying HTML form. Now I made two radio buttons in my form:

 

<input type="radio" value="1" name="radio"> Department 1

<br>

<input type="radio" value="2" name="radio"> Department 2

 

Now the hard confusing bit, how do I get the &to variable to change to depending on which radio button is pressed? For example radio button 1 goes to [email protected] and 2 goes to [email protected]

 

Also where do I put the email addresses? I would rather the radio buttons be called form example "Department" and the email is assigned to it but hidden. How?

 

 

Also an off-topic question, in the variable $body what do I use to make certain parts bold, underlined etc? How?

 

 

Thanks!

Brave.

Link to comment
https://forums.phpfreaks.com/topic/96423-contact-form-help/
Share on other sites

Thank you for the quick and excellent reply Orio! It's working perfectly!!

 

I have a couple more questions for anyone:

 

1. How can I make the output fields in the email bold, underlined etc. (For example the /n for new line)

 

2. At the moment the "from" email header has my website email address, how could I change that to read the "name" field in the email? This is how it looks: "From [email protected] Date&Time ##/## Subject". Hopefully you'll understand that one  :D.

 

3. Is there anyway I could track the person who submitted the form's IP? And then show it in the email. Handy for banning spammers...

 

 

Thanks again!

Link to comment
https://forums.phpfreaks.com/topic/96423-contact-form-help/#findComment-493557
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.