How to create an email form with PHP

Friday, September 14, 2012

PHP was designed for making interactive web pages and mixing functionality with HTML. Form handling in PHP is quite a simple process. Here is a step-by-step guide for creating a simple feedback form. A visitor to your website fills this out and the information is emailed to you.

Create the web form

First we need to create a simple HTML form, to start with we'll keep the form simple by just asking for the users email address and comments. Here is our HTML form:
  1. <html>
  2. <head>
  3. <title>Simple Feedback Form</title>
  4. <style>label{display:block;}</style>
  5. </head>
  6. <body>
  7. <form action="/feedback_form.php" method="post">
  8. <label>Email Address</label>
  9. <input type="text" name="email_address" size="40">
  10. <label>Your Feedback</label>
  11. <textarea name="feedback" cols="50" rows="10"></textarea>
  12. <input type="submit" name="send" value="Submit">
  13. </form>
  14. </body>
  15. </html>
This form will send two parameters to our PHP script, email_address and feedback. Save this file as feedback_form.html and upload it to the web folder on your hosting.

Create the form script

First we receive the data from our form and store it in two PHP variables, $email_address and $feedback.
  1. <?php
  2. $email_address = $_POST['email_address'];
  3. $feedback = $_POST['feedback'];

Filtering user submitted data

Whenever you write a PHP script that receives data from an unknown source you should always filter the data to make sure it doesn't contain anything harmful. For example, if we don't filter the data in our form it would be quite easy for a Hacker to use our PHP script to send out spam to thousands of people. The golden rule is never trust any data you haven't created or don't control.
To filter our user data we're going to create a functions:
  1. function filter_email_header($form_field) {
  2.   return preg_replace('/[\0\n\r\|\!\/\<\>\^\$\%\*\&]+/','',$form_field);
  3. }
The filter function removes special characters which could be used to trick our script into sending spam and is applied to the $email_address data. We'll place the two functions at the bottom of our script.
Now we'll call the filter function to clean up our user submitted email address:
  1. $email_address  = filter_email_header($email_address);

Emailing the feedback

Once we have the filtered data we need to email it back to you. Our web hosting servers run a local mail server (PHP script can use to send email. This can be done using the PHP in-built mail function:
  1. $headers = "From: $email_address\n";
  2. $sent = mail('you@domain.com', 'Feedback Form Submission', $feedback, $headers);
Make sure you set your email address on line 2.

Thank the user for their feedback

Finally, when a user submits your form lets show a page thanking them for their feedback:
  1. if ($sent) {
  2. ?><html>
  3. <head>
  4. <title>Thank You</title>
  5. </head>
  6. <body>
  7. <h1>Thank You</h1>
  8. <p>Thank you for your feedback.</p>
  9. </body>
  10. </html>
  11. <?php
  12. } else {
  13. ?><html>
  14. <head>
  15. <title>Somthing went wrong</title>
  16. </head>
  17. <body>
  18. <h1>Somthing went wrong</h1>
  19. <p>We could not send your feedback. Please try again.</p>
  20. </body>
  21. </html>
  22. <?php
  23. }
  24. ?>

The final script

This example script shows a very basic way to get form contents emailed to you, it doesn't however have the refinements of a professional script, e.g. input validation. Below is the finished script. We've added some comments (lines beginning with #) to help make it clearer.
  1. <?php
  2. #Receive user input
  3. $email_address = $_POST['email_address'];
  4. $feedback = $_POST['feedback'];
  5. #Filter user input
  6. function filter_email_header($form_field) {
  7.   return preg_replace('/[\0\n\r\|\!\/\<\>\^\$\%\*\&]+/','',$form_field);
  8. }
  9. $email_address  = filter_email_header($email_address);
  10. #Send email
  11. $headers = "From: $email_address\n";
  12. $sent = mail('you@domain.com', 'Feedback Form Submission', $feedback, $headers);
  13. #Thank user or notify them of a problem
  14. if ($sent) {
  15. ?><html>
  16. <head>
  17. <title>Thank You</title>
  18. </head>
  19. <body>
  20. <h1>Thank You</h1>
  21. <p>Thank you for your feedback.</p>
  22. </body>
  23. </html>
  24. <?php
  25. } else {
  26. ?><html>
  27. <head>
  28. <title>Somthing went wrong</title>
  29. </head>
  30. <body>
  31. <h1>Somthing went wrong</h1>
  32. <p>We could not send your feedback. Please try again.</p>
  33. </body>
  34. </html>
  35. <?php
  36. }
  37. ?>
Save this script as feedback_form.php and upload it to the root of your web hosting on your web hosting.
Now you're ready to test your feedback form. Load your feedback form in your browser, http://www.domain.com/feedback_form.html, fill the form in and submit it. If everything works you should receive an email containing what you just entered in the form.

Creating simple PHP contact form

Overview

if you want to test on you own computer you must set your computer as a mail server by using Argosoft mail server other softwares that have the same features as Argosoft. Read this tutorial for how to set up.


In this tutorial, create 2 files
1. contact.php
2. send_contact.php

Steps
1. Create file contact.php.
2. Create file send_contact.php.

STEP1: Create file contact.php

############### Code


<table width="400" border="0" align="center" cellpadding="3" cellspacing="1">
<tr>
<td><strong>Contact Form </strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="send_contact.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td width="16%">Subject</td>
<td width="2%">:</td>
<td width="82%"><input name="subject" type="text" id="subject" size="50"></td>
</tr>
<tr>
<td>Detail</td>
<td>:</td>
<td><textarea name="detail" cols="50" rows="4" id="detail"></textarea></td>
</tr>
<tr>
<td>Name</td>
<td>:</td>
<td><input name="name" type="text" id="name" size="50"></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="customer_mail" type="text" id="customer_mail" size="50"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>

STEP2: Create file send_contact.php

############### Code


<?php

// Contact subject
$subject ="$subject";

// Details

$message="$detail";

// Mail of sender

$mail_from="$customer_mail";

// From

$header="from: $name <$mail_from>";

// Enter your email address

$to ='someone@somewhere.com';
$send_contact=mail($to,$subject,$message,$header);

// Check, if message sent to your email
// display message "We've recived your information"

if($send_contact){
echo "We've recived your contact information";
}
else {
echo "ERROR";
}
?>

Making a login form using PHP

This is in continuation of the tutorial on making a membership based web site. Please see the previous page PHP registration form for more details.

Download the code

You can download the whole source code for the registration/login system from the link below:
RegistrationForm.zip

The ReadMe.txt file in the download contains detailed instructions.

The login form

PHP login form
Here is the HTML code for the login form.
<form id='login' action='login.php' method='post' accept-charset='UTF-8'>
<fieldset >
<legend>Login</legend>
<input type='hidden' name='submitted' id='submitted' value='1'/>
<label for='username' >UserName*:</label>
<input type='text' name='username' id='username'  maxlength="50" />
<label for='password' >Password*:</label>
<input type='password' name='password' id='password' maxlength="50" />
<input type='submit' name='Submit' value='Submit' />
</fieldset>
</form>

Logging in

We verify the username and the password we received and then look up those in the database. Here is the code:
function Login()
{
    if(empty($_POST['username']))
    {
        $this->HandleError("UserName is empty!");
        return false;
    }
    if(empty($_POST['password']))
    {
        $this->HandleError("Password is empty!");
        return false;
    }
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
    if(!$this->CheckLoginInDB($username,$password))
    {
        return false;
    }
    session_start();
    $_SESSION[$this->GetLoginSessionVar()] = $username;
    return true;
}
In order to identify a user as authorized, we are going to check the database for his combination of username/password, and if a correct combination was entered, we set a session variable.
Here is the code to look up the username and password.
function CheckLoginInDB($username,$password)
{
    if(!$this->DBLogin())
    {
        $this->HandleError("Database login failed!");
        return false;
    }
    $username = $this->SanitizeForSQL($username);
    $pwdmd5 = md5($password);
    $qry = "Select name, email from $this->tablename ".
        " where username='$username' and password='$pwdmd5' ".
        " and confirmcode='y'";
    $result = mysql_query($qry,$this->connection);
    if(!$result || mysql_num_rows($result) <= 0)
    {
        $this->HandleError("Error logging in. ".
            "The username or password does not match");
        return false;
    }
    return true;
}
Please notice that we must compare the value for the password from the database with the MD5 encrypted value of the password entered by the user. If the query returns a result, we set an "authorized" session variable, and then redirect to the protected content. If there are no rows with the entered data, we just redirect the user to the login form again.

Access controlled pages

For those pages that can only be accessed by registered members, we need to put a check on the top of the page.
Notice that we are setting an "authorized" session variable in the login code above. On top of pages we want to protect, we check for that session variable. If user is authorized, we show him the protected content, otherwise we direct him to the login form.
Include this sample piece of code on top of your protected pages:
<?PHP
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin())
{
    $fgmembersite->RedirectToURL("login.php");
    exit;
}
?>
See the file: access-controlled.php in the downloaded code for an example.
Here is the CheckLogin() function code.
function CheckLogin()
{
     session_start();
     $sessionvar = $this->GetLoginSessionVar();
     if(empty($_SESSION[$sessionvar]))
     {
        return false;
     }
     return true;
}
These are the basics of creating a membership site. Now that you have the basic knowledge, you can experiment with it and add new features, such as a "Forgot password" page to allow the user to retrieve or change his password if he forgets it.

Life OK Live

ICC World T20 2012 Match Schedule and Groups

ICC WORLD TWENTY20 2012 GROUPS:

Group A Group B Group C Group C
England Australia Srilanka Pakistan
India West Indies South Africa New Zealand
Afghanistan Ireland Zimbabwe Bangladesh

 MATCH SCHEDULE:

Day Date Teams Venue (Srilanka)
1 18th Sept Srilanka vs Zimbabwe Hambontota
2 19th Sept Australia vs IrelandIndia vs Afghanistan Colombo
3 20th Sept South Africa vs Zimbabwe Hambontota
4 21st Sept New Zealand vs BangladeshEngland vs Afghanistan ColomboPallekele
5 22nd Sept Srilanka vs South AfricaAustralia vs West Indies HambontotaColombo
6 23rd Sept Pakistan vs New ZealandEngland vs India PallekeleColombo
7 24th Sept West Indies vs Ireland Colombo
8 25th Sept Pakistan vs Bangladesh Pallekele

SUPER EIGHT GROUPS:

S8 Group A S8 Group B
A1 A2
B2 B1
C1 C2
D2 D1

SUPER EIGHT SCHEDULE:

Day Date Teams Venue (Srilanka)
9 27th Sept C1 VS D2A1 VS B2 PallekelePallekele
10 28th Sept D1 VS C2B1 VS A2 ColomboColombo
11 29th Sept A1 VS D2C1 VS B2 PallekelePallekele
12 30th Sept B1 VS C2D1 VS A2 ColomboColombo
13 1st Oct B2 VS D2A1 VS C1 PallekelePallekele
14 2nd Oct B1 VS D1A2 VS C2 ColomboColombo


SEMI FINALS:

Day Date Teams Venue(Srilanka)
15 4th Oct S81  1st VS S82 2nd Colombo
16 5th Oct S81 2nd VS S82 1st Colombo

FINAL:

Day Date Teams Venue (Srilanka)
17 7th Oct Finalist Teams Colombo

NTV Live


ATN Bangla Live


Channel I Live


ETV Live

Watch live streaming video from blu3etv at livestream.com

Bangla Vision Live


RTV Live


Boishakhi TV Live


ZEE Cinema Live


9x Music

 
Support :
Copyright © 2011. All In One Blog - All Rights Reserved
Template Created by Creating Website Inspired by Sportapolis Shape5.com
Proudly powered by Blogger