JavaScript for HTML Form

I am a simple soul with simple needs and I am trying to customize the shape. I hate forms.

To transfer data, JavaScript needs to transmit data, it needs to send an email with the data to the email address, and it needs to redirect visitors to pdf. CGI has always confused me and I don't know much JavaScript.

I already made html but post action and JavaScript are killing me. It was 4 hours of searching. Screw the plug into me. I've finished.

+1


source to share


3 answers


SOunds like you need to validate the form and then submit it to the server, which will then be overridden (javascript cannot be reassigned) and send the post request to the email server.

I would recommend PHP for a server that re-evaluates the form and sends a request to the email server because it is simple and widely supported.

Let's 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 can make sure the user is logged into all fields using javascript and jQuery. Let's say in your script.js file 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 will have something like this:

 <?php
 $to =  'email@domain.com';
 $from = 'email@domain.com';
 $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);
 ?>

      

To do this, you need PHP running on your server and setting up an SMTP server in php, but most servers do. Note how PHP is not rebuilding the form right now, so when someone submits a blank form, it will still be emailed.

+1


source


OK, first of all, you need to clarify what you are phrasing. JavaScript and Java are two significantly different languages.

In order to send mail, you need to use some language that you use in conjunction with the SMTP server, which should be what actually sends the email, not JavaScript, which is the client side.

An example of using JavaScript to send to an SMTP server can be found here (although I would not recommend this method since ActiveX is IE exclusive): http://www.ostrosoft.com/smtp_component/smtp_javascript.asp



Ultimately with JavaScript, the best method you would use would simply be hosted on a non-SMTP server, which then sends the data to the SMTP server.

If you are actually using Java, there is an additional library you can add called JavaMail to make sending mail easier, but you still need to communicate with the SMTP server.

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

0


source


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

So what's the question? -)

0


source







All Articles