Posted by: Sourav | May 23, 2011

Making a better form in php


form.php

<html>
    <head>
        <title>
            Author Details:
        </title>
    </head>
    <body>
        <table align=”center”>
            <tr>
                <td><font color=”brown”><?php echo $msg;?></font></td>
            <td>
        </table>
     <table align=”center”>
        <tr>
            <td>
                <fieldset>
                    <legend><font size=”5″ type=”Comic Sans”>
                    <b><i>Author’s Details:</b></i></font>
                    </legend>
                   
                <form action=”result.php” method=”post” name=”authorform”>
                    <br />
                   
                    <center>
                        Please enter the information to get access to the resources available
                    </center>
                    <br>
                        <table>
                            <tr>
                                <td>Author’s Name:&nbsp;</td>
                                <td>
                                    <input type=”text” size=”70″ name=”authorname”
                                           value=”<?php echo $_POST[‘authorname’];?>”>
      
                                </td>
                            </tr>
                            <br />
                            <tr>
                                <td align=”center” colspan=”2″>
                                  (format: <b>Firstname</b>[:space:]<b>Middlename</b>[:space:]<b>Lastname</b>)
                                </td>
                            </tr>
                           
                            <tr>
                              
                                <td>Birthdate:&nbsp;</td>
                                <td>
                                    <input type=”text” size=”20″ name=”birthdate”
                                           value=”<?php echo $_POST[‘birthdate’];?>”>
      
                                </td>
                            </tr>
                            <tr>
                                <td align=”center” colspan=”2″>
                                  (format: <b>dd/mm/yyyy</b>[:space:]<b>dd.mm.yyyy</b>[:space:]<b>dd-mm-yyyy</b>)
                                </td>
                            </tr>
                            <tr>
                              
                                <td>Qualifications:&nbsp;</td>
                                <td>
                                    <input type=”text” size=”70″ name=”qualification”
                                           value=”<?php echo $_POST[‘qualification’];?>”>
      
                                </td>
                            </tr>
                            <tr>
                                <td align=”center” colspan=”2″>
                                  (Use commas for multiple qualifications)
                                </td>
                            </tr>
                            <tr>
                              
                                <td>Speciality:&nbsp;</td>
                                <td>
                                    <textarea name=”speciality” cols=”50″ rows=”6″ type=”text”
                                              value=”<?php echo $_POST[‘speciality’];?>”>
                                              </textarea>
      
                                </td>
                            </tr>
                            <tr>
                                <td align=”center” colspan=”2″>
                                  (Enter data fitting in 5 lines)
                                </td>
                            </tr>
                            <table align=”center”>
                            <tr>
                                <td align=”center”>
                                    <input type=”submit” name=”submit” value=”submit” onclick=”submit()”>
                                       <input type=”reset” name=”reset” value=”clear” onclick=”reset()”>
                                </td>
                            </tr>
                            </table>
                        </table>
                </form>
                       
                </fieldset>
            </td>
        </tr>
     </table>
            </td>
            </tr>
            </table>
      
       
    </body>
</html>
        
  result.php
        
        
        

<?php
if(empty($_POST[‘authorname’]))
{
    $msg=”Please enter the author’s name”;
    include(“form.php”);
    exit;
}

if(ereg(“([[:space:]]){2}”,$_POST[‘authorname’]))
{
   
    $msg=”Please do not use double space in the author’s name”;
     include(“form.php”);
    exit;
}

if(empty($_POST[‘birthdate’]))
{
    $msg=”Please enter the birthdate”;
    include(“form.php”);
    exit;
}
if(!ereg(“^([0-9]){2}[/.-]([0-9]){2}[/.-]([0-9]){4}”,$_POST[‘birthdate’]))
{
    $msg=”Birthdate format is not correct”;
    include(“form.php”);
    exit();
}

if(empty($_POST[‘qualification’]))
{
    $msg=”Please enter the qualification”;
    include(“form.php”);
    exit;
}
if(empty($_POST[‘speciality’]))
{
    $msg=”Please enter the speciality”;
    include(“form.php”);
    exit;
}
?>
<html>
    <head>
        <title>
            Result page
        </title>
    </head>
    <center><b><i>Author’s Name:</i></b></center>
    <br />
    <body>
        <table width=”400″ align=”center” border=”1″
 cellspacing=”1″ cellpadding=”1″>
            <?php list($firstname,$middlename,$lastname)=split(” “,$_POST[‘authorname’],3);?>
            <tr>
                <td align=”center”><?php echo $firstname;?></td>
                <td align=”center”><?php echo $middlename;?></td>
                <td align=”center”><?php echo $lastname;?></td>
            </tr>
            <tr>
                <td align=”center”>Firstname</td>
                <td align=”center”>Middlename</td>
                <td align=”center”>Lastname</td>
            </tr>
        </table>
            <?php $_POST[‘birthdate’]=ereg_replace(‘[/.-]’,’ ‘,$_POST[‘birthdate’]);?>
           
            <center><b><i>Date Of Birth</i></b></center><br />
           <table width=”400″ align=”center” border=”1″
 cellspacing=”1″ cellpadding=”1″>
            <tr>
                <td align=”center”><?php echo $_POST[‘birthdate’];?></td>
            </tr>
        </table>
          
               <center><b><i>Qualification</i></b></center><br />
                <table width=”400″ align=”center” border=”1″
 cellspacing=”1″ cellpadding=”1″>
              <?php
              $qual=split(“,”,$_POST[‘qualification’]);
               foreach($qual as $fld)
               {?>
               <tr>
                <td align=”center”>
                    <?php echo $fld;?>
                </td>
               </tr>
              
               <?php } ?>
               
              </table>
                <center><b><i>Speciality</i></b></center><br />
                <table width=”400″ align=”center” border=”1″
 cellspacing=”1″ cellpadding=”1″>
              <?php
              $spe=split(“,”,$_POST[‘speciality’]);
               foreach($spe as $fld)
               {?>
               <tr>
                <td align=”center”>
                    <?php echo $fld;?>
                </td>
               </tr>
              
               <?php } ?>
               
              </table>
               
               
              </body>
</html>

        
        
       

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: