In web development it is very normal that we create 'Contact-Us' Form. And we want to send complete form submitted data in a mail to somebody (normally to our self).
There are many ways to do this.
1) Using JavaScript
2) Using any server side scripting like PHP .
To understand this at first, we will have to understand the concept of “send mail" and "mail server" . for sending mail , we need to have a mail server or mail client . Best example for mail client is MS-outlook and mail client is "mail" command in linux .




Using client site scripting  javascript :-

As we know JavaScript is client side scripting, so it need a client to send mail i.e. mail client.
Example:-
lets take a simple form .
form.html
<form action="mailto:abc@gmail.com">
Name <input type="text" name="name" />
Phone <input type="text" name="phone" />
<input type="submit" name="submit" value="submit"/>
</form>
This is simplest example. If we try to see what happens here , we are using JavaScript "mailto" function . "Mailto" will trigger mail client to send mail that means, it will call you MS-Outlook (if installed and configured) .This way is not used widely as it is client dependent way and we don't have any control here .
 Using server side scripting PHP :

php will need a mail server configured to send a mail . As we are developers we can control and configure mail server by our own.
Example :-
form.html
<form action="abc.php" method="post">
Name <input type="text" name="name" />
Phone <input type="text" name="phone" />
<input type="submit" name="submit" value="submit"/>
</form>

abc.php

<?php
$name = $_POST['name'];
$phone = $_POST['phone'];
$to = "abc@gmailo.com";
$subject = "Contact details ";
$message = "your message content goes here...  $name \n\r My Contact number is $phone ";
mail($to,$subject,$message);
?>
this is stable and trusted code to send mails . And everyone should use this way of sending mail .
There are two ways of submitting form i.e. GET and POST. Both methods have their importance.
If we submit form using get method and then all data submitted will be append in the action page url . And we know that url have its limits so we can send only small amount of data using GET method .
In POST method data- there is no limit here to send data .
Also in GET method data is visible in th url , so it is not safe . Whenever we need to create safe application we should use POST method .
GET method is fast as everything is simply appended in the url . But in using POST method every request need to maintain data in the body . So it is lsightly time consuming method .
Both methods are useful . So we need to choose wisely according to our application taht which method we should use .
Thank you .
Please ask in comments if need any clarification. Suggestions are also welcome.

0 comments:

Post a Comment

 
Top