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@site.com";  
$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@site.com and 2 goes to email1@site.com

 

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
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 example@email.com 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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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