Contact Form - Email problem Watch

MagicToaster1
Badges: 0
Rep:
?
#1
Report Thread starter 8 years ago
#1
The problem I have is when for example someone selects whether they are male or female from the dropdown box, the email needs to go a specific email address on which option they choose.. for example if they chose male it would go to [email protected] or female it would go to [email protected]..

I'm completely stumped on how to do this.. does anyone know how its possible?

It would be great! :confused:

Cheers

PHP:
Code:
//If the form is submitted
if(isset($_POST['submit'])) {

	//Check to make sure that the name field is not empty
	if(trim($_POST['contactname']) == '') {
		$hasError = true;
	} else {
		$name = trim($_POST['contactname']);
	}

	//Check to make sure that the subject field is not empty
	if(trim($_POST['subject']) == '') {
		$hasError = true;
	} else {
		$subject = trim($_POST['subject']);
	}

	//Check to make sure sure that a valid email address is submitted
	if(trim($_POST['email']) == '')  {
		$hasError = true;
	} else if (!eregi("^[A-Z0-9._%-][email protected][A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
		$hasError = true;
	} else {
		$email = trim($_POST['email']);
	}
	
	
	
	if(trim($_POST['dropdown']) == '') {
		$hasError = true;
	} else {
		$dropdown = trim($_POST['dropdown']);
	}
	

	//Check to make sure comments were entered
	if(trim($_POST['message']) == '') {
		$hasError = true;
	} else {
		if(function_exists('stripslashes')) {
			$comments = stripslashes(trim($_POST['message']));
		} else {
			$comments = trim($_POST['message']);
		}
	}

	//If there is no error, send the email
	if(!isset($hasError)) {
		$emailTo = '[email protected]'; '[email protected]' 
		$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject  \n\ndropdown: $dropdown \n\nComments:\n $comments";
		$headers = 'From: Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

		mail($emailTo, $subject, $body, $headers);
		$emailSent = true;
	}
}


Form etc
Code:
<script type="text/javascript">
$(document).ready(function(){
	$("#contactform").validate();
});
</script>

      	<div id="contact-wrapper">

	<?php if(isset($hasError)) { //If errors are found ?>
		<p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
	<?php } ?>

	<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
		<p><b>Email Successfully Sent!</b></p>
		<p>Thank you <b><?php echo $name;?></b> for getting in contact with us! Your email was successfully sent and we will be in touch with you soon.</p>
	<?php } ?>

	<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
		<div>
		    <label for="name"><strong>Name:</strong></label>
			<input type="text" size="50" name="contactname" id="contactname" value="" class="required" />
		</div>

		<div>
			<label for="email"><strong>Email:</strong></label>
			<input type="text" size="50" name="email" id="email" value="" class="required email" />
		</div>

		<div>
			<label for="subject"><strong>Phone Number:</strong></label>
			<input type="text" size="50" name="subject" id="subject" value="" class="required" />
		</div>


	<div>

<label for="subject"><strong>What gender are you?</strong></label>
	<select name="dropdown" id="dropdown" > 
    
       <option value="Please Select" >Please Select</option>
        <option value="Male" >Male</option>
        <option value="Female" >Female/option>

	</select>

</div>
		<div>
			<label for="message"><strong>Message:</strong></label>
			<textarea rows="5" cols="50" name="message" id="message" class="required"></textarea>
		</div>
	    <input type="submit" value="Send Message" name="submit" class="submit" />
	</form>
	</div>
0
reply
Astonix
Badges: 14
Rep:
?
#2
Report 8 years ago
#2
It's really annoying but I can't remember the way to do it with a drop list off the top of my head. I can however re-call how to do it if you're using check boxes / radio buttons if you want to change your form a little?

HTML Code:
  <b>Gender</b><br />
  <input type="radio" name="male">Male <input type="radio" name="female">Female 
This will create two circle buttons with Male and Female next to each other for them to select. Now for the PHP. You will want to make use of the isset function provided in PHP : )

PHP Code:
if (isset($_POST['male']; {
    
$emailTo "[email protected]";   //Male is selected
}

if (isset(
$_POST['female']; {
   
$emailTo "[email protected]";   //Female is selected


I hope that helped. Ask if you have any more questions : )
1
reply
laser
Badges: 0
Rep:
?
#3
Report 8 years ago
#3
(Original post by Astonix)
It's really annoying but I can't remember the way to do it with a drop list off the top of my head. I can however re-call how to do it if you're using check boxes / radio buttons if you want to change your form a little?

HTML Code:
  &lt;b&gt;Gender&lt;/b&gt;&lt;br /&gt;
  &lt;input type=&quot;radio&quot; name=&quot;male&quot;&gt;Male &lt;input type=&quot;radio&quot; name=&quot;female&quot;&gt;Female 
This will create two circle buttons with Male and Female next to each other for them to select. Now for the PHP. You will want to make use of the isset function provided in PHP : )

PHP Code:
if (isset($_POST['male']; {
    
$emailTo = &quot;mike@test.com&quot;;   //Male is selected
}

if (isset(
$_POST['female']; {
   
$emailTo = &quot;amy@test.com&quot;;   //Female is selected


I hope that helped. Ask if you have any more questions : )
I think the radio buttons need to have the same name if you want to actually be able to toggle between them. You should use the value field...
0
reply
MagicToaster1
Badges: 0
Rep:
?
#4
Report Thread starter 8 years ago
#4
(Original post by Astonix)
It's really annoying but I can't remember the way to do it with a drop list off the top of my head. I can however re-call how to do it if you're using check boxes / radio buttons if you want to change your form a little?

HTML Code:
  <b>Gender</b><br />
  <input type="radio" name="male">Male <input type="radio" name="female">Female 
This will create two circle buttons with Male and Female next to each other for them to select. Now for the PHP. You will want to make use of the isset function provided in PHP : )

PHP Code:
if (isset($_POST['male']; {
    
$emailTo "[email protected]";   //Male is selected
}

if (isset(
$_POST['female']; {
   
$emailTo "[email protected]";   //Female is selected


I hope that helped. Ask if you have any more questions : )
Thanks for the help! But atm it doesnt matter which radio option I choose it just sends the email to the end address which would be the [email protected]

I needed to add )) on both the isset lines as it was coming up with an error unless that broke it?

if (isset($_POST['male'])); {

:confused:
0
reply
Dez
Badges: 20
Rep:
?
#5
Report 8 years ago
#5
Drop-down box:
Code:
<select name="gender">
    <option value="male">Male</option>
    <option value="female">Female</option>
</select
Then you can just check the value for $_POST['gender']:
PHP Code:
if ($_POST['gender'] == 'male')
{
    
// Male
}
else
{
    
// Female

1
reply
Astonix
Badges: 14
Rep:
?
#6
Report 8 years ago
#6
(Original post by MagicToaster1)
Thanks for the help! But atm it doesnt matter which radio option I choose it just sends the email to the end address which would be the [email protected]

I needed to add )) on both the isset lines as it was coming up with an error unless that broke it?

if (isset($_POST['male'])); {

:confused:

Yer sorry, it should of had the closing )). What Dez said should work and is probably a better way of doing it : )
0
reply
MagicToaster1
Badges: 0
Rep:
?
#7
Report Thread starter 8 years ago
#7
(Original post by Dez)
Drop-down box:
Code:
<select name="gender">
    <option value="male">Male</option>
    <option value="female">Female</option>
</select
Then you can just check the value for $_POST['gender']:
PHP Code:
if ($_POST['gender'] == 'male')
{
    
// Male
}
else
{
    
// Female

Cheers for that. Im a newbie at PHP though to get your code to work is this what I would do?

Code:
if ($_POST['gender'] == 'male')
$emailTo = "[email protected]";
{
}

else {

$emailTo = "[email protected]"; }

else {

$emailTo = "[email protected]";  }
And if I wanted to add in a 3rd option such as private or prefer not to disclose etc is this what I what do?

Any help be appreciated cheers!
0
reply
laser
Badges: 0
Rep:
?
#8
Report 8 years ago
#8
Nope, you'd need to do an elseif, e.g.,

PHP Code:
if ($_POST['gender'] == 'male') {
    
$emailTo "[email protected]";
} elseif (
$_POST['gender'] == 'female') {
    
$emailTo "[email protected]";
} else {
    
$emailTo "[email protected]"

1
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Have you registered to vote?

Yes! (250)
39%
No - but I will (44)
6.86%
No - I don't want to (47)
7.33%
No - I can't vote (<18, not in UK, etc) (300)
46.8%

Watched Threads

View All
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise