Form submition based on dropdown choice
I am trying to have a JavaScript form submit its data to a different email based on the drop down the user chooses.
Example:) [drop down list: IT / Sales / HR] If you chose IT it goes to IT@example.com, Choosing Sales from the drop down will make开发者_如何转开发 the form submit data to sales@example.com and so on.
I have seen a few php answers but I am limited by a content mangier system and must do this in JavaScript.
Any help would be great.
Have the emails set as the values for the select options
<select id="mailTo" name="mailTo">
<option value="IT@example.com">IT</option>
<option value="sales@example.com">Sales</option>
<option value="HR@example.com">Human Resources</option>
</select>
Unfortunately, if you are limited to javascript only there will be no way to send email from your form. See this link about email through javascript. The short-of-it is that it would be a major security hole if you could send out emails through javascript because it is executed client-side and therefore, any one could send unlimited emails to anyone they wanted.
The standard way is to either:
- send your email data to a cgi script on the server that will send the email
- or, perform a post-back to your php, asp.net, etc application that is configured to send email through your email providor.
If neither of these are an option, there are online resources where you can make a form that will send an email, and it will give you embed code to place in your html. Here are two from a quick google search: simfatic, formlogix There are lots more if you google around.
Hope this helps!
Forms do not "submit to an email" - you have to post it to the server, where you send the email via PHP (or some other server-side language). There is no direct way to email forms from javascript alone.
You simply must have a server-side script to handle the sending of the email. You mention having seen samples.... here's another.
PHP (test.php)
$to = $_POST['destination_email'];
$subject = "Test email";
$body = "Email sent from website";
$res = mail($to, $subject, $body);
if (!$res)
die('email failed');
else
die('email sent');
HTML
<form action="test.php" method="post">
<select name="destination_email">
<option value="emailone@domain.com">Email one</option>
<option value="emailtwo@domain.com">Email two</option>
</select>
<input type="submit" value="submit" />
</form>
精彩评论