Page 1 of 1

Client Side Validation

Posted: Wed Oct 08, 2008 3:32 am
by prnkrish
Hi to All,


I am new to php and and I have created a new php project... and in that I have established a connection to mysql db..


I am receiving 5 fields as user entry.. and stored in the db.... and in this case I want to validate the textbox and email... and all type regular expression validation in this form...

I used javascrip.. but I don't know how to use....... in proper ...? Can anyone tell me how to do this... by viewing the below code.... The below code doesn't validate.... but the javascript is running by displaying the Hello World in Uppercase


I am using Dream Weaver, Xampp as server, Mysql as db.

Code: Select all

<?php
//Project       :   TEST PROJECT
//Author        :   NK
//Last Updated  :   06/10/2008
//Company       :   5G Technologies 
 
//error
    error_reporting(0);
 
//session
    session_start();
    
//buffer start
    ob_start();
    
//include WEBCONFIG file
    include("includes/webconfig.php");
 
//Get All Posted Values
    $Txt_ClientName         =   $_POST['Txt_ClientName'];
    $Txt_ClientCompany  =   $_POST['Txt_ClientCompany'];
    $Txt_ClientAddress      =   $_POST['Txt_ClientAddress'];
    $Txt_ClientPhone        =   $_POST['Txt_ClientPhone'];
    $Txt_ClientEmail        =   $_POST['Txt_ClientEmail'];
    $Cmd_Submit         =   $_POST['Cmd_Submit'];
    
    if(trim($Cmd_Submit)==trim("New"))
    {
        $SQLQry_InsertClient=mysql_query("INSERT INTO tbl_client(var_Name,var_Company,var_Address,int_Phone,var_Email,var_Status) VALUES('$Txt_ClientName','$Txt_ClientCompany','$Txt_ClientAddress','$Txt_ClientPhone','$Txt_ClientEmail','Yes')", $Conn);
        
        $message = "{$Txt_ClientName} Added Successfully";       
    }
    
?>  
 
   
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test Work </title>
 
<link href="style/ssheet.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style1 {font-size: 22px}
.style2 {font-size: 27px}
-->
</style>
 
</head>
 
<body>
<script type = "text/javascript" language="javascript">
var h = "Hello World";
 
document.write(h.toUpperCase());
 
function validate(thisform)
        {
            if(this.document.Frm_Client.Txt_ClientName == "")
            {
                document.wirte("coming in validate enter name");
                alert("Please Enter Name");
                document.Frm_Client.Txt_ClientName.focus();
                return false;
            }
            if(this.document.Frm_Client.Txt_ClientCompany == "")
            {
                alert("Please Enter Company Name");
                document.Frm_Client.Txt_ClientCompany.focus();
                return false;
            }   
            if(this.doucment.Frm_Client.Txt_ClientPhone == "")
            {
                alert("Please Enter Phone Number");
                document.Frm_Client.Txt_ClientPhone.focus();
                return false;
            }
            if(this.document.Frm_Client.Txt_ClientEmail == "")
            {
                alert("Please Enter Email id");
                document.Frm_Client.Txt_ClientEmail.focus();
                return false;
            }               
        }
        
</script>
 
 
<form name="Frm_Client"  action="<?php $HTTP_SERVER_VARS['PHP_SELF'];?>" method="POST" onsubmit="return validate()">
 
  <table width="571" border="0" align="center" cellpadding="0" cellspacing="0"> 
    <tr>
      <td height="54" colspan="3"><div align="center" class="tbltitle"><strong>Client Information System </strong></div></td>
    </tr>
    <tr>
      <td width="48%" class="bodybg"><div align="right" class="bodybg">Client Name (required, atleast 2 char)</div></td>
      <td width="3%" class="bodybg">&nbsp;</td>
      <td width="49%" class="bodybg"><label for="cname">
        <input name="Txt_ClientName" type="text" class="txt-box" id="Txt_ClientName" />
      </label></td>
    </tr>
    <tr>
      <td height="40" class="bodybg"><div align="right" class="bodybg">Client Company </div></td>
      <td class="bodybg">&nbsp;</td>
      <td class="bodybg"><div align="left">
        <label>
        <input name="Txt_ClientCompany" type="text" class="txt-box" id="Txt_ClientCompany" />
        </label>
      </div></td>
    </tr>
    <tr>
      <td class="bodybg"><div align="right" class="bodybg">Client Address </div></td>
      <td class="bodybg">&nbsp;</td>
      <td class="bodybg"><div align="left">
        <label>
        <textarea name="Txt_ClientAddress" class="txt-area" id="Txt_ClientAddress"></textarea>
        </label>
      </div></td>
    </tr>
    <tr>
      <td class="bodybg"><div align="right" class="bodybg">Phone</div></td>
      <td class="bodybg">&nbsp;</td>
      <td class="bodybg"><div align="left">
        <label></label>
        <label>
        <input name="Txt_ClientPhone" type="text" class="txt-box" id="Txt_ClientPhone" />
        </label>
      </div></td>
    </tr>
    <tr>
      <td class="bodybg"><div align="right" class="bodybg">Email</div></td>
      <td class="bodybg">&nbsp;</td>
      <td class="bodybg"><div align="left">
        <label>
        <input name="Txt_ClientEmail" type="text" class="txt-box" id="Txt_ClientEmail" />
        </label>
      </div></td>
    </tr>
    <?php 
    if($message<>"")
    {
    ?>
    <tr>
      <td colspan="3" align="center" valign="middle" class="bodybg"><?php echo  $message; ?></td>
    </tr>
    <?php
    }
    ?>
    <tr>
      <td colspan="3" class="bodybg"><label>
        <div align="center">
          <input name="Cmd_Submit"  type="submit" class="cmd_submit" id="Cmd_Submit" value="New" />
        </div>
      </label></td>
    </tr>    
  </table>
</form>
  
  <br /> 
  
  <table width="88%" height="57" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <th width="21%" class="bodybg"><div align="center">Name</div></th>
      <th width="21%" class="bodybg"><div align="center">Company</div></th>
      <th width="23%" class="bodybg"><div align="center">Address</div></th>
      <th width="15%" class="bodybg"><div align="center">Phone</div></th>
      <th width="20%" class="bodybg"><div align="center">Email</div></th>
    </tr>
    
    <?php $SQL_Select = mysql_query("SELECT * FROM tbl_client", $Conn);
    while($Get_Value = mysql_fetch_array($SQL_Select))
    {
    ?>
    <tr>
      <td align="center" valign="middle" class="bodybg"><?php echo $Get_Value['var_Name']?></td>
      <td align="center" valign="middle" class="bodybg"><?php echo $Get_Value['var_Company']?></td>
      <td align="center" valign="middle" class="bodybg"><?php echo $Get_Value['var_Address']?></td>
      <td align="center" valign="middle" class="bodybg"><?php echo $Get_Value['int_Phone']?></td>
      <td align="center" valign="middle" class="bodybg"><?php echo $Get_Value['var_Email']?></td>
    </tr>
    <?php
    }
    ?>    
</table>
</body>
</html>


And also I have a doubt... Is there any possibilities to fix a breakpoint and compile the code like we use in .net.


How to compile my php code.. to fix the error fast....?



And finally How to use jquery instead of using javascript coding for client side validation...... ? explain with example.... please.....




Thanks in Advance...

Re: Client Side Validation

Posted: Wed Oct 08, 2008 3:43 am
by aceconcepts
I don't entirely understand what you're getting at. However, js is obviously client-side and php is server-side. server-side validation is always recommended whether you use client-side validation or not.

JS is useful because you can validate with sending info to the server, however, server-side validation can ensure more extensive validation on submitted form data.

Re: Client Side Validation

Posted: Wed Oct 08, 2008 4:30 am
by prnkrish
Thanks for ur kind reply first.... aceconcepts

Ya U r right.....

Client Side validation is the must one either we can check using js or php also...
but the page will be reloaded when we use php script....

so in order to faster the page without reload... we use js or jquery...


In my php I am getting five fields as input... and storing in table... a simple php code... to insert..


For me I want only validation part...... how to validate.. for eg... the five fields are mandatory... to check that i need validation........


I hope u understand...

Re: Client Side Validation

Posted: Thu Oct 09, 2008 4:43 am
by aceconcepts
Ok, so you're looking to find PHP validation for your fields right?

Code: Select all

 
//set an error variable
$frmErr=0;
 
//first check whether user has clicked "submit" button
if(isset($_POST['submit']))
{
   //get the field's values
   $field_1=$_POST['field_1'];
   $field_2=$_POST['field_2'];
   $field_3=$_POST['field_3'];
   $field_4=$_POST['field_4'];
   $field_5=$_POST['field_5'];
 
   //validate fields - this validation will produce an error if you do not enter a value in field_1
   if(empty($field_1))
   {
      $frmErr=1;
   }
 
   //process data
   if($frmErr==0)
   {
      //form data is valid
   }
}
 
So basically, the above validation will retrieve the form data, check whether field_1 has a value in it - if it is empty then the user will be halted and the process will not continue.

Get it?

Re: Client Side Validation

Posted: Fri Oct 10, 2008 5:51 am
by prnkrish
Ya .. I got it...

As per ur suggestion...... the validation is done on php...... when we done validation in php it will take time to validate. because all the values will be post to the server and then the reply will display the error... so this is not the standard method... the page will get refresh....

I want to do it in the client side itself.. only when the (username already exists or not) at that time we need php validation.....

And also as per ur suggestion in ur example...there is only null field validation..... Now I would like to do the regular expression validation in all the combination.....


Now How could to do this.... ?

What is ur suggestion now....?

Re: Client Side Validation

Posted: Fri Oct 10, 2008 6:15 am
by aceconcepts
Client-side is so named because all takes place on client machine. If you want data from database then use server-side. Obvious really isnt it :wink:

Well regular expression validation is wide open to anything you want to validate against.

There are some very useful web site with good examples of reg ex. Try searching for some.

The same principle and method will apply when using reg ex validation as in my example.