views:

803

answers:

3

I'm a simple soul with simple needs, and I'm trying to configure a form. I detest forms.

It needs to have JavaScript to transfer the data, it needs to send an e-mail with the data to an e-mail address, and it needs to redirect visitors to a pdf. CGI has always been confusing to me, and I don't know much JavaScript.

I've already done the html, but the post action and the JavaScript is killing me. It's been 4 hours of searching. Stick a fork in me. I'm done.

A: 

OK, first thing's first, we need to clarify you're wording. JavaScript and Java are two significantly different languages.

In order to send mail, you need to use whichever language you're using in conjunction with an SMTP server, which is what would have to be what actually sends the e-mail, not JavaScript which is client-side.

An example of using JavaScript to post to an SMTP server can be found here (though I wouldn't recommend this method as ActiveX is IE exclusive): http://www.ostrosoft.com/smtp_component/smtp_javascript.asp

Ultimately with JavaScript, the best method you'd use would just be posting to a non-SMTP server that then sends the data to the SMTP server.

If you're actually using Java, there's an optional library you can add called JavaMail to make it much easier to send mail, though again, you need to communicate with an SMTP server.

An example of JavaMail can be found here: http://www.javapractices.com/topic/TopicAction.do?Id=144

Zurahn
A: 

Your form sounds like a FormMail-form, which is exclusively made for mail-sending, it doesn't really need javascript, but it certainly needs a score of hidden fields, and all the information you especially want to extract must have their own sections/fields in the form.

So, what is the question ?-)

roenving
The client asked for javascript. Just trying to deliver.
+1  A: 

SOunds like you need to validate the form and then send it to a server wich then revalidates (javascript can not be thrusted) and sends the mail-request to an e-mail server.

I'd recommend PHP for the server that revalidates the form and sends the request to the e-mail server because it's easy and wide supported.

Say you have this HTML

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="script.js"></script>
<form id="emailForm" method="post" action="mail.php">
    <input type='text' name='firstName' id="firstName"><br>
    <input type='text' name='lastName' id='lastname'><br>
    <input type='submit' value='submit' name='submit'>
</form>

You could verify that the user entered something in all fields using javascript and jQuery. Say in the file script.js you have:

var formIsOkay = true;
$(document).ready( function() {
$('#emailForm').submit( function() {
    $('#emailForm input').each( function() {
         if ( this.val() == '' ) { formIsOkay = false; }
    }
    return formIsOkay;
}
}

And then in email.php you would have something like this:

 <?php
 $to =  '[email protected]';
 $from = '[email protected]';
 $subject = 'Form';
 $message = 'Hello, the following variables were supplied:<br>';
 foreach($_POST as $key => $val){
     $message .= "$key = $val<br>";
 }
 $message = wordwrap($message, 70);
 $headers  = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers .= "To: $to" . "\r\n";
 $headers .= "From: $from" . "\r\n";
 mail($to, $subject, $message, $headers);
 ?>

You will need PHP running on your server for this, and an SMTP server setup in php, but most servers have this. Note btw how PHP does not reavlidate the form right now so when someone submits an empty form it will still be send in the e-mail.

Pim Jager
Thank you. Very helpful.