PHP OOP -> Form Validation

<?php //Target : Creating form, and if the user forget to enter any field it alerts the user to fill it. //This code deals with oop concepts, html fields, php validation, css tricks, reset forms, //Autor : R.krishna, //Website : http://www.sriabiramicomputer.com //Post your questions or comment, i will try to reply asap. ?> <!-- Form Design --> <!-- Save this php file as formValidation.php --> <h1> Form Validation </h1> <form action = "formValidation.php" method = "post"> <p> Enter your Name : <input type = "text" name = "name" /> </p> <p> Enter your Phone : <input type = "text" name = "phone" /> </p> <p> Enter your Email : <input type = "text" name = "email" /> </p> <!-- <p>Enter Comments : <textarea name = "comment" cols="24" rows="5"> </textarea> </p> --> <p style = "float:left; margin-right:21px;"> <input type = "submit" name = "checkit" value = "Validate Form" /> </p> <p style = "float:left;"> <input type = "reset" name = "bleach" value = "Reset Form" /> </p> <p style = "float:left;"> <input type = "hidden" name = "invisible" value = "Invisible button" /> </p> </form> <!-- end of form declaration --> <?php //check for form submit if($_SERVER['REQUEST_METHOD'] == 'POST') { class formValidation { var $saname; var $saphone; var $saemail; //function or method declaration. function sagetData(){ $saname = "".$_POST['name']; //echo "Name you entered is : ".$saname; // Validate the name: if (!empty($_REQUEST['name'])) { $name = $_REQUEST['name']; echo "<br>"; echo "<br>"; echo "<br>"; echo "Name you entered is : ".$saname; } else { $name = NULL; echo "<br>"; echo "<br>"; echo "<br>"; echo "<br>"; echo '<p class="error" style="color:red;">You forgot to enter your name!</p>'; } }// end of function. function sagetDataPhone() { $saphone = "".$_REQUEST['phone']; //I used $_REQUEST here. // Both $_POST[''] and $_REQUEST[''] works alike. // Validate the phone: if (!empty($_REQUEST['phone'])) { $phone = $_REQUEST['phone']; echo "<br>"; echo "Phone you entered is : ".$saphone; } else { $phone = NULL; echo '<p class="error" style="color:red;">You forgot to enter your phone number!</p>'; } } // end of function. function sagetEmail() { $saemail = "".$_REQUEST['email']; //Validating email. if (!empty($_REQUEST['email'])) { $phone = $_REQUEST['email']; echo "<br>"; echo "Email you entered is : " .$saemail; } else { $email = NULL; echo '<p class="error" style="color:red;">You forgot to enter your Email!</p>'; } }// end of function }// End of class formValidation. // Object creation $x = new formValidation(); //phpinfo(); This code displays php information. //calling method $x->sagetData(); $x->sagetDataPhone(); $x->sagetEmail(); }//server if statement ends here. ?>

8 Responses

First of all thank you for sharing this informative blog.. this blog really helpful for everyone.. explanation are clear so easy to understand... I got more useful information from this blog

<a href=" http://www.credosystemz.com/training-in-chennai/best-php-training-in-chennai/">best php training institute in chennai velachery</a>

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.