Home arrow Java arrow Page 3 - Interacting with PHP for Server-side Data Validation with AJAX
JAVA

Interacting with PHP for Server-side Data Validation with AJAX


Looking for new and creative ways to empower your AJAX-driven Web applications? Then you’ve landed at the right place. Welcome to the final part of the series “Server-side Data Validation with AJAX.” In three tutorials, this series demonstrates how to build a simple form checking system which uses AJAX to performing server-side validation on the data supplied by users.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 17
July 25, 2006
TABLE OF CONTENTS:
  1. · Interacting with PHP for Server-side Data Validation with AJAX
  2. · Validating data on the server: building an input checking PHP class
  3. · Extending the functionality of the checking class: defining some additional methods
  4. · Assembling the pieces: completing the AJAX-driven checking application

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.


blog comments powered by Disqus
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...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials