Sunday 6 May 2012

How to save form data in database and send activation key

In this tutorial you will learn how to save signup form data in database and send activation key to user's email. I am not including validation code here if you want to validate your form submission code click here to see. For looking the sign in form click here
You should follow these two points:
  1. Creat a database named 'olympic' and a table named 'user'.
    CREATE TABLE IF NOT EXISTS `user` ( `username` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `postal_code` int(11) NOT NULL, `status` varchar(20) NOT NULL, `key` varchar(20) NOT NULL, `id` int(11) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=32 ;
  2. Copy and past code given below.
    <?php
    // Assing value about your server to these variables for database connection
    $hostname_connect= "localhost";
    $database_connect= "olympic";
    $username_connect= "root";
    $password_connect= "";
    $connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
    @mysql_select_db($database_connect) or die (mysql_error());

    if($_POST)
    {
    $key = mt_rand(); // this mt_rand() function genrates randum key for you
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $passconf = $_POST['passconf'];
    $postal_code = $_POST['postal_code'];

    // code below checks username in database exist or not. if exist data will not submit to database
    $user_query="select username from user where username='$username'";

    $user_result=mysql_query($user_query);
    $user_row=mysql_fetch_array($user_result);
    if(!empty($user_row))
    {
    if($user_row['username'])
    {
    echo 'Username Already exist';
    }


    }
    else
    {
    //If username not already exist then insert data into 'user' table
    $q = "INSERT INTO `user`(`username`,`email`,`password`,`postal_code`,`key`,`status`)VALUES('$username','$email','$password','$postal_code','$key','InActive')";

    $qr=mysql_query($q);
    if(!$qr){
    echo "Not Submitted";
    }
    else
    {
    // if data has saved in data base then code below will send email to user
    $to = $email;
    $subject = "Activation key";
    $message = "Your activation key is this" .$key.'<br>'.' click here to activate your acount.<a href="localhost">here</a>';
    $from = "admin_abc@yahoo.com";
    $headers = "From:" . $from;
    if(mail($to,$subject,$message,$headers))
    {
    echo "Check your email to activate your acount.";
    }
    else
    {
    echo "Email not sended";
    // if you are working on localhost email will not send and below line helps you to activates your acount
    echo $message = "Your activation key is this " .$key.'<br>'.' click here to activate your acount. <a href="activation.php?key='.$key.'">here</a>';
    }
    }
    }
    }
    ?>
    <html>
    <body>
    <form id='registeration' action='' method='post' >
    <fieldset >
    <legend>Register</legend>
    <input type='hidden' name='submitted' id='submitted' value='1'/>
    <label for='username' >Username*: </label><br/>
    <input type='text' name='username' id='username' value="<?php if(!empty($username)) echo $username ?>" maxlength="50" /><br/>
    <label for='email' >Email Address*:</label><br/>
    <input type='text' name='email' id='email' value="<?php if(!empty($email)) echo $email ?>" maxlength="50" /><br/>
    <label for='password' >Password*:</label><br/>
    <input type='password' name='password' id='password' maxlength="50" /><br/>
    <label for='passconf' >Confirm Password*:</label><br/>
    <input type='password' name='passconf' id='password' maxlength="50" /><br/>
    <label for='postal_code' >Postal Code*:</label><br/>
    <input type='text' name='postal_code' id='postal_code' value="<?php if(!empty($postal_code)) echo $postal_code ?>" maxlength="50" /><br/>
    <input type='submit' name='Submit' value='Submit' />
    </fieldset>
    </form>
    </body>
    </html>

0 comments:

Post a Comment