This post will explain you that how to submit a form to send information in an email by using PHP. Here is simple steps to learn.
I'll be making a simple contact form and will deliver the results in an email using a PHP script.
Step 1
I have created a simple form with three form fields as:
» Name (text field)
» Email address (text field)
» Comments (textarea)
Contact form
Step 2
We will use the PHP to send our form results through an email.
Copy and paste given below php code in a file sendresults.php and set the following parameters according to your need.
» $subject (Subject of email sent to you)
» $emailadd (Your email address. This is where the form information will be sent)
» $url (Where to redirect after form is processed. )
» $req (Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.)
<?php
//-------------------Set these paramaters--------------------
// Subject of email sent to you.
$subject = 'Results from Contact form';
// Your email address. This is where the form information will be sent.
$emailadd = 'nsharma1402@gmail.com';
// Where to redirect after form is processed.
$url = 'http://tutorialfeed.blogspot.com/';
// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';
// -----------------Do not edit below this line-----------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>
Step 3
Select the form and in form properties you will see the following attributes.
The Name attribute is very clear gives your form a name. We have given our form name as Contact form.
The Action attribute defines what should be done with the form content. More Importantly, action is set to a URL for running a specific web application or for sending email.
Here you will give the URL where you saved your sendresults.php file Example http://tutorialfeed.blogspot.com/sendresults.php
The Method attribute tells the browser or the web server how to present the form contents to the application that will process the form. “POST” in the Method window indicates that the information in the form will be passed to the program processing the form as standard input. Give method=POST
Save your form and test it.
Keep following points to remember:
» In the form properties, Action=URL of your sendresults.php file and Method=POST
» Editing the following parameters in sendresults.php file:
$subject, $emailadd, $url, $req
» Make sure your host supports PHP
0 comments :
Post a Comment