Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

drop down menu for the to: variable

the mail() function

Moderators: macek, egami, gesf

drop down menu for the to: variable

Postby nikbone » Tue Jul 08, 2003 11:19 am

Hello. I'm a newbie, and have a question. I am making a contact form, and want to be able to have a drop down menu on the form for the user to select what department they want to email. Here's the code i have right now:

<?php

//Declare the variables
$recipient = $contact;
$subject = "Contact Form submission";
$message = $comments;
$from = $email;

//Contents of form
$contact = $_POST['contact'];
$sales = "sales@myserver.com"
$parts = "parts@myserver.com"
$service = "service@myserver.com"
$website = "webmaster@myserver.com"
$name =$_POST['name'];
$email =$_POST['email'];
$phone =$_POST['phone'];
$comments =$_POST['comments'];

//mail() function sends the mail
mail("To: $contact",$subject,"Name: $name\r\nPhone: $phone\r\nQuestion: $comments","From: $email");

?>

here's my html form:
<form action=contactform.php method=post>
<table width="90%" border="0" align="center">
<tr>
<td width="19%">Contact: </td>
<td width="81%">
<select name="contact">
<option value="sales" selected>Sales Department</option>
<option value="parts">Parts / Accessories</option>
<option value="service">Service Department</option>
<option value="website">Website / Design</option>
</select>
</td>
</tr>
<tr>
<td width="19%" height="19">Name: </td>
<td width="81%" height="19">
<input type="text" name="name">
</td>
</tr>
<tr>
<td width="19%">Email:</td>
<td width="81%">
<input type="text" name="email">
</td>
</tr>
<tr>
<td width="19%">Phone:</td>
<td width="81%">
<input type="text" name="phone" value="( )">
</td>
</tr>
<tr>
<td width="19%">Message:</td>
<td width="81%">
<textarea name="comments" rows="3"></textarea>
</td>
</tr>
<tr>
<td width="19%">&</td>
<td width="81%">
<input type="submit" name="Submit" value="Send">
</td>
</tr>
</table>
</form>


the variables called $sales, $parts, etc. under the contacts are the variables for the different email addresses depending on what is selected. I named the values in the drop down box those variables (ex. sales, parts, etc.) is that correct?

Should I use an array function with those different variables?

I have tried many things but can't get it to work. Thanks a lot for your help.

Thanks.

Nik
nikbone
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Jul 08, 2003 11:08 am

Postby bezmond » Tue Jul 08, 2003 11:54 am

for your menu box, you could use:

<select name="to">
<option value="sales@domain.com">sales</option>
<option value="parts@domain.com">parts</option>
</select>

and then just use $to in your to: field.

Andrew
User avatar
bezmond
Moderator
Moderator
 
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK

i tried that, but still not working

Postby nikbone » Tue Jul 08, 2003 3:52 pm

hey thanks for the help, but i still couldn't get it to work. here's my coding:

<?php

//Declare the variables
$recipient = $contact;
$subject = "Contact Form submission";
$message = $comments;
$from = $email;

//Contents of form
$contact = $_POST['contact'];
$name =$_POST['name'];
$email =$_POST['email'];
$phone =$_POST['phone'];
$comments =$_POST['comments'];

//mail() function sends the mail
mail("To: $contact",$subject,"Name: $name\r\nPhone: $phone\r\nQuestion: $comments","From: $email");

?>

-right after the mail( should i have "To: $contact" (which is the variable for the drop down box.) or should it say $recipient?

-it seems like it takes too long from when i click the send button, until it goes to the .php page and sends the form. is this a sign of something wrong?

it seems that everything is correct but it doesn't work. I am still learning and may just have a stupid mistake somewhere?

Thanks a lot for your help and patience.

Nik
nikbone
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Jul 08, 2003 11:08 am

Postby Joel » Tue Jul 08, 2003 10:30 pm

For the mail function, the to should be just an email address.

So mail("To: $toaddress") would be sending an email to "To: $toaddress". You should not have the to.
Joel
New php-forum User
New php-forum User
 
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand

Postby bezmond » Tue Jul 08, 2003 11:48 pm

yeah that too, see http://www.php.net/mail for details on this function :)

Andrew
User avatar
bezmond
Moderator
Moderator
 
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK

There are some unnecessary variables in your code

Postby pazzoboy » Tue Jan 27, 2004 7:37 pm

I'll modify your code a bit for you. Couple of things: "name" might be a problem, as I think it might be reserved, so I changed it to customerName. $recipient and those other ones were not even used in the php script, as you got everything you need from the $_POST[ ] variable assignments. Then, $sales, $parts, etc., were not necessary, as all you need is one variable ($contact) to catch the results of the user's selection. You should put those values (the email addresses) in the html form.

<?php
//One variable to declare here
$subject = "Contact Form submission";

//Contents of form
$contact = $_POST['contact'];
$customerName =$_POST['customerName'];
$email =$_POST['email'];
$phone =$_POST['phone'];
$comments =$_POST['comments'];
//mail() function sends the mail
mail($contact,$subject,"Name: " . $name. "\r\nPhone: ".$phone . "\r\nQuestion: ". $comments,"From: $email");
?>

<form action=contactform.php method=post>
<table width="90%" border="0" align="center">
<tr>
<td width="19%">Contact: </td>
<td width="81%">
<select name="contact">
<option value="sales@myserver.com">sales</option>
<option value="parts@myserver.com">parts</option>
<option value="service@myserver.com">Service Department</option>
<option value="website@myserver.com">Website Design</option>
</select>
</td>
</tr>
<tr>
<td width="19%" height="19">Name: </td>
<td width="81%" height="19">
<input type="text" name="customerName">
</td>
</tr>
<tr>
<td width="19%">Email:</td>
<td width="81%">
<input type="text" name="email">
</td>
</tr>
<tr>
<td width="19%">Phone:</td>
<td width="81%">
<input type="text" name="phone" value="( )">
</td>
</tr>
<tr>
<td width="19%">Message:</td>
<td width="81%">
<textarea name="comments" rows="3"></textarea>
</td>
</tr>
<tr>
<td width="19%">&</td>
<td width="81%">
<input type="submit" name="Submit" value="Send">
</td>
</tr>
</table>
</form>
pazzoboy
New php-forum User
New php-forum User
 
Posts: 10
Joined: Wed Dec 03, 2003 5:23 pm
Location: Rochester

Postby Martin Pickering » Mon Apr 19, 2004 2:25 pm

Just an observation, based on experience!

If you put email addresses into a form, they will be spammed.

Better to put
<option value="sales">sales</option>
<option value="parts">parts</option>
<option value="service">Service Department</option>
<option value="website">Website Design</option>

Then add "@myserver.com" to the contact name in the PHP script.

But get it working first before bothering with this!

Martin Pickering (UK)
Martin Pickering
New php-forum User
New php-forum User
 
Posts: 106
Joined: Fri Oct 17, 2003 8:00 am


Return to PHP coding => Mail

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.