Monday 14 July 2014

simple html form which captures Name, Email, Address and call javascript . The javascript program should validate, and php to store the information in the database(mysql).

 <html>  
 <head>  
   <script>  
     function validation()  
     {  
 // name  
       var a = document.form.name.value;  
       if (a == "")  
       {  
         alert("Please Enter Your name");  
         document.form.name.focus();  
         return false;  
       }  
       if ((a.length < 5) || (a.length > 15))  
       {  
         alert("Your Character must be 5 to 15 Character");  
         document.form.name.focus();  
         return false;  
       }  
       if (!isNaN(a))  
       {  
         alert("Please Enter Only Characters");  
         document.form.name.select();  
         return false;  
       }  
 //email  
       var b = document.form.email.value;  
       if(b == "")  
       {  
         alert("Please Enter Your email");  
         document.form.email.focus();  
         return false;  
       }  
       if(b)  
       {  
         filter = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;  
         if (!filter.test(b))  
         {  
           alert("Please enter valid email");  
           return false;  
         }  
       }  
 // address  
       var c = document.form.address.value;  
       if(c == "")  
       {  
         alert("Please Enter Your address");  
         document.form.address.focus();  
         return false;  
       }  
       if ((c.length < 5) || (c.length > 15))  
       {  
         alert("Your Character must be 5 to 15 Character");  
         document.form.address.focus();  
         return false;  
       }  
     }  
   </script>  
   <title>Simple Form</title>  
 </head>  
 <body>  
 <form action="<?php $_PHP_SELF ?>" method="post" name="form" onsubmit="return validation()">  
   Name:<input type="text" name="name"/><br>  
   Email:<input type="text" name="email"/><br>  
   Address:<input type="text" name="address"/><br>  
   <input type="submit" value="Submit"/>  
 </form>  
 <?php  
 $name = $_POST['name'];  
 $email = $_POST['email'];  
 $address = $_POST['address'];  
 if ($name == '' || $email == ''|| $address == '')  
 {  
   echo 'plz fill the required field';  
 }else {  
   $dbhost = 'localhost:3036';  
   $dbuser = 'root';  
   $dbpass = 'root';  
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);  
   if(! $conn )  
   {  
     die('Could not connect: ' . mysql_error());  
   }  
   $sql = 'INSERT INTO persons '.  
     '(name, email, address) '.  
     "VALUES ('$name', '$email', '$address')";  
   mysql_select_db('phpform');  
   if(!empty($name))  
   {  
     $retval = mysql_query($sql, $conn);  
     if(! $retval )  
     {  
       die('Could not enter data: ' . mysql_error());  
     }  
     echo "Entered data successfully\n";  
   }  
   mysql_close($conn);  
 }  
 ?>  
 </body>  
 </html>  

No comments:

Post a Comment