Click to See Complete Forum and Search --> : Contact Form - Change Receiver


novemberGrey
04-04-2007, 07:21 AM
On my contact form I want to allow the user to pick who they want the email to go to. So I'm going to have a drop-down list where they can select the person. How would I do this? Or would this be done with PHP?

ricp
04-04-2007, 07:56 AM
Thanks for the most minimal amount of information possible. A few questions..

1) How many email addresses are there?
2) How do you wish to send the email (smtp server side or via the client side default email application)?
3) Do you need to cater for those who have js disabled.

novemberGrey
04-04-2007, 08:14 AM
Sorry for the lack of information. There will probably be around 10 different emails to choose from. I would like to send the form server side via PHP. And yes I would like the form to be accessible to those with js diabled.

ricp
04-04-2007, 08:25 AM
Ok, if you need this to be non-js supportable then we would need to move just about all the processing onto the server side. The process I would go with is this one..

1) While parsing/generating the page on the server side, include the emails using php, so you have something like..


<form method="post" action="emailForm.php">
<select name="email">
<? populateEmailAddresses() ?>
</select>
<input type="submit" value="Email"/>
:
// include any further form elements
:
</form>


inside the populateEmailAddresses() function in the php is code to output the addresses in the format like so..


<option value="foo@bar.com"> foo </option>


The page is then all sent to the client with a form which will pass the selected email address to the page emailForm.php

2) Inside emailForm.php you have the php code for manipulating the POST values sent and initialising the message to be sent through SMTP, then sending it.



Basically there is no JS in there, even the form submission is handled "naturally" on the client to avoid the need for JS. You could overload the form using JS and have xmlhttp send the value of the selected email to a server side script that would send what it needs to. That would at least allow those with script enabled to avoid submitting/reloading the page.

Hope that helps.

novemberGrey
04-04-2007, 08:30 AM
awesome thanks for your help!

pcthug
04-04-2007, 08:40 AM
You could have a select field like so:

<select name="to">
<option value="0">John</option>
<option value="1">Jill</option>
<option value="2">Bill</option>
</select>



$emails = array(
'john123@example.org',
'jill@example.net',
'bill.smith@example.com');

$email = isset($_POST['to']) && array_key_exists($_POST['to'], $emails) ? $emails[$_POST['to']] : $_POST[0];

Taschen
04-04-2007, 08:42 AM
<option value="foo@bar.com"> foo </option>


I would strongly advise you not to follow the above method. Where possible never publish an email address directly to your website in plain text, it aids spam harvesters.

Rather make your value a resource identifier of the form 'n'

<option value="n1"> foo </option>

Use this to access an array of email address in your php script
$available_mail_addresses = array("n1"=>"n.name@domain.org", "n2" => "n2.name@domain.org");
Hard coding email addresse into you mail processing script will also make it harder for your form processor to be abused by third parties.

And I see that while I was writing that PC Thug replyed on similar lines.

ricp
04-04-2007, 09:18 AM
Where possible never publish an email address directly to your website in plain text, it aids spam harvesters.
It's a fair point, I had overlooked that when I was doing the generalisation, but it's definitely valid.

That said, is there such a thing as a spam free email address?

;)

novemberGrey
04-04-2007, 09:35 AM
thankyou everyone for your help!

novemberGrey
04-04-2007, 10:02 AM
Taschen and PC thug, using your method, could one of you show me an example of how the mailing script should look?

pcthug
04-04-2007, 10:24 AM
$emails = array(
'john123@example.org',
'jill@example.net',
'bill.smith@example.com');

$email = isset($_POST['to']) && array_key_exists($_POST['to'], $emails) ? $emails[$_POST['to']] : $_POST[0];
$subject = 'Foo';
$message = 'Bar';

mail($email, $subject, $message);

novemberGrey
04-04-2007, 11:08 AM
Thanks for your help PCthug!

novemberGrey
04-04-2007, 11:12 AM
I uploaded my form and my PHP script to one server, and it wouldn't work for some reason. When I would click Submit it would give me a "couldn't find page error" and the address of the PHP script would be in the address bar. So then I uploaded my files in a different server and it worked perfectly. What do you think is the problem with the first server?

novemberGrey
04-04-2007, 12:13 PM
I figured it out, I ran a test php script, It seems my church doesn't have PHP installed on their server, lol.