Java
  Home arrow Java arrow Page 3 - Interacting with PHP for Server-side Data ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Interacting with PHP for Server-side Data Validation with AJAX
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2006-07-25

    Table of Contents:
  • Interacting with PHP for Server-side Data Validation with AJAX
  • Validating data on the server: building an input checking PHP class
  • Extending the functionality of the checking class: defining some additional methods
  • Assembling the pieces: completing the AJAX-driven checking application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Interacting with PHP for Server-side Data Validation with AJAX - Extending the functionality of the checking class: defining some additional methods


    (Page 3 of 4 )

    In accordance with my own expectations (and yours, of course) of the functionality the previous PHP class should have, below I listed a few additional methods. These are handy for checking alphabetic and alphanumeric values, along with email addresses. Check them out:

      // validate alphabetic field
      public function validateAlphabetic($field,$value,$errorMessage)
    {
        if(!isset($value)||!preg_match("/^[a-zA-Z]+$/",$value)){
            return $field.'|'.$errorMessage;
        }
        else{
            return $field.'| ';
        }
      }
      // validate alphanumeric field
       public function validateAlphanum($field,$value,$errorMessage){
        if(!isset($value)||!preg_match("/^[a-zA-Z0-9]+$/",$value)){
            return $field.'|'.$errorMessage;
        }
        else{
            return $field.'| ';
        }
      }
      // validate email
      public function validateEmail($field,$value,$errorMessage){
        if(!isset($value)||!preg_match("/.+@.+..+./",$value)||!
    checkdnsrr(array_pop(explode("@",$value)),"MX")){
           return $field.'|'.$errorMessage;
        }
        else{
            return $field.'| ';
         }
      }
      // validate email (Windows systems)
      public function validateEmailWin($field,$value,$errorMessage){
        if(!isset($value)||!preg_match("/.+@.+..+./",$value)||!
    $this->windnsrr(array_pop(explode("@",$value)),"MX")){
            return $field.'|'.$errorMessage;
        }
        else{
            return $field.'| ';
        }
      }
      // private method 'windnsrr()' for Windows systems
      private function windnsrr($hostName,$recType=''){
        if(!empty($hostName)){
            if($recType=='')$recType="MX";
            exec("nslookup -type=$recType $hostName",$result);
            foreach($result as $line){
                if(preg_match("/^$hostName/",$line)){
                    return true;
                }
            }
            return false;
        }
        return false;
      }
      // call validation method on the fly
    public function callValMethod(){
        eval("echo $this->validate".$this->method."('".$this-
    >field."','".$this->value."','".$this->message."');");
      }

    As shown above, I added some extra methods to expand the existing capacity of the PHP checking class. Aside from performing traditional verification of user-supplied data, notice how the class calls the corresponding validation method on the fly, as illustrated below:

      // call validation method on the fly
      public function callValMethod(){
        eval("echo $this->validate".$this->method."('".$this-
    >field."','".$this->value."','".$this->message."');");
      }

    By using the properties that I explained in the previous section, the class is now capable of determining what checking method to use, and certainly what value to pick up for performing the validation process. As you'll probably agree, the "eval()" PHP built-in function is very useful in these cases.

    At this point, I believe that you've grasped the fundamentals of how the data entered into the online form is validated with each HTTP request made in the background, and how the class sends the response text back to the client. Therefore, let's move one step forward and see how the client and server-side layers of the AJAX-based application fit together.

    To learn how this will be done, please jump into the next section and keep reading.

    More Java Articles
    More By Alejandro Gervasio


       · Over the course of this final tutorial, you'll learn how to use a set of PHP...
       · A very good article - thank you. I get a parse error in the php-script :Parse...
       · Hello Andreas,Thank you for your kind comments on this article. Now, with...
       · I'm running PHP5 and I'm seeing the same error. Might be worth another look at...
       · Thank you for letting me know about your problem. I agree with you that it's...
       · I'm on php-5.1.6.Exactly the same error, whether via web server or directly.This...
       · class formValidator{public function __construct()...
       · With this (thanks to Lee Babin "Beginning Ajax with PHP" ,p.17)and minor changes...
       · Hello Monty,I'm glad to hear that now this application is finally working for...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...


     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT