Location: PHPKode > scripts > Rapid Form Builder > rapid-form-builder/checkClasses.php
<?php
session_start();
/* ************************************************************
 *  Christopher Knott, hide@address.com  http://digitallight.ca
 ***************************************************************/

class processFields {
	var $cleanData ; 
	var $ajaxField ; 
	var $fieldName ; 
	var $sid ; 
	var $fieldType ; 
	var $msg ;
	var $result ; 
	var $query ; 
	var $num_rows ;
	var $row ; 
	
	public function processTextField ($ajaxField, $cleanData, $sid) {  
		
	    $this->cleanData = $cleanData ;
	    $this->ajaxField = $ajaxField ; 
	    $this->sid = $sid ; 
	    $cleanData = str_replace("%40", "@", $cleanData) ;
	    
	    $fieldType = substr($ajaxField, 0, 3) ;
		$fieldMandatory = substr($ajaxField, 3, 1) ;
	    $fieldName = substr($ajaxField, 5); 
	    $stripThese = array(" ", ",", "-", ".", "+");
	    $stripped = str_replace($stripThese, "", $cleanData);
		
	    if ($fieldType != "eml" AND $fieldType != "alp" AND $fieldType != "aln" AND $fieldType != "dig" AND $fieldType != "phn" AND $fieldType != "txt" AND $fieldType != "chk" AND $fieldType != "rad" AND $fieldType != "sel") {
	    	echo "ATTENTION DEVELOPER:  Invalid field validator (<strong>$fieldType</strong>).  Please create a new one, or used a defined one." ;
	    	exit() ; 
	    }
	    
	    if ($fieldType == "eml") {
		    if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $cleanData)) {
				$msg = "1<br>Invalid email address" ;
				return $msg ;
		    } 
	    }
	    
		if ($fieldType == "alp") {				
    		if (!ctype_alpha($stripped)) {	
				$msg = "1<br>A-Z letters only";
				return $msg ; 
    		}
		}	
		
		if ($fieldType == "aln") {	
			if (!ctype_alnum($stripped)) {
				$msg = "1<br>Alphanumeric characters only";
				return $msg ;
			}
	    }
		
	    if ($fieldType == "dig") {	
			if (!ctype_digit($cleanData)) {
				$msg = "1<br>0-9 Numbers only";
				return $msg ;
			}
	    }
		
		if ($fieldType == "phn") {	
			if (!ctype_digit($cleanData)) {
				$msg = "1<br>Numbers Only";
				return $msg ; 
			}
			if (strlen($cleanData) != "10") {
				$msg = "1<br>Entry must be 10 digits";
				return $msg ;  
			}
	    }
	    
    			$msg = manageData::checkLength($fieldName, $cleanData);
				if (isset($msg)) {
					return $msg ;
				}		
				
				$msg = manageData::checkUnique($fieldName, $cleanData, $sid) ;
					if (isset($msg)) {
						return $msg ; 
					}
						
				if (!$msg) {			
					manageData::saveData($fieldName, $cleanData, $sid) ;
					$msg = $this->msg = "0<img align='absmiddle' src='images/greenCheckMark.gif' alt='$fieldName is correct'>" ;
					return $msg ; 
				}			
				    
	}



	public function processCheckBox ($ajaxField, $cleanData, $sid) {  
    $this->cleanData = $cleanData;
    $printThis = $_REQUEST['$ajaxfield'] ;
 		if ($cleanData == 'true') {
	    		$msg = "0<img align='absmiddle' src='images/greenCheckMark.gif'>";
				return $msg ; 
				} else {
				$msg = "1<br>Should be selected";
				return $msg ; 
				}
	}
	
	
	public function processSelect($ajaxField, $cleanData, $sid) {  
    $this->cleanData = $cleanData;
    	if (ctype_alpha($cleanData)) {
    		$msg = "0<img align='absmiddle' src='images/greenCheckMark.gif'>";
			return $msg ; 
			} else {
			$msg = "1<br>Make a selection";
			return $msg ; 
			}
	}
	
	
	public function processRadioButton ($ajaxField, $cleanData, $sid) {  
    $this->a = $a;
		if (strlen($a) > '1') {
	    		$msg = $this->msg = "0<img align='absmiddle' src='images/greenCheckMark.gif'>";
				return $msg ; 
				} else {
				$msg = $this->msg = "1<br>Select one from the list";
				return $msg ; 
				}
	}
	
	public function determineClass ($a, $b) {
		$this->a = $a ; 
		$testMandatory = $this->b = $b ;
		$class = substr($a, 0, 1) ;
			if ($class == 0) { $testMandatory = 0 ; }
		$class = $class + $testMandatory ; 
		if ($class == 1) { $this->class = "black" ; }  
		if ($class == 0) { $this->class = "green" ; }
		if ($class == 2) { $this->class = "red" ; }
		return $this->class ;
	}

	
	public function cleanField ($a) {
		$this->a = $a ;
		$a = urlencode($a) ;
		$a = htmlentities($a, ENT_QUOTES) ;
		$a = mysql_real_escape_string($a) ;
		return $a ; 
	}
	
}

class manageData {
	
	public function databasePostConnect ($connect) {
	
		if ($_GET['op'] == 'ajax') {
			$link = mysql_connect("localhost", "root", "YOURPASSWORDGOESHERE");
			mysql_select_db("ajaxForms");
		}
	}

	
	public function databaseConnect ($connect) {
		$link = mysql_connect("localhost", "root", "YOURPASSWORDGOESHERE");
		mysql_select_db("ajaxForms");
	}

	
	public function getTableMappings ($fieldName) {
		$this->fieldName = $fieldName ;
		$query = mysql_query("SELECT tableName FROM tableMappings WHERE ajaxField = '$fieldName' ");
			$row = mysql_fetch_row($query) ;		
				$tableName = $row['0'] ;
				if (!$query) {
    				die('Invalid query table mappings: ' . mysql_error());
				}		
			return $tableName ;
	}

	
	public function getFieldMappings ($fieldName) {
		$this->fieldName = $fieldName ;
		$query = mysql_query("SELECT fieldName FROM tableMappings WHERE ajaxField = '$fieldName' ");
			$row = mysql_fetch_row($query) ;		
				$dataFieldName = $row['0'] ;
				if (!$query) {
    				die('Invalid query AA: ' . mysql_error());
				}		
			return $dataFieldName ;
	}

	
	public function getStoredValues($fieldName = '', $sid = '') {				 
		$this->fieldName = $fieldName ;
		$this->sid = $sid ;
		manageData::databaseConnect($connect) ;
		$tableName = manageData::getTableMappings($fieldName) ; 
		$dataFieldName = manageData::getFieldMappings($fieldName) ; 
		$query = mysql_query("SELECT $dataFieldName FROM $tableName WHERE id = '$sid' ");
			$row = mysql_fetch_row($query) ;		
				$storedValue = $row['0'] ;
				if (!$query) {
	    			die('Invalid query AA: ' . mysql_error());
				}		
		return $storedValue ;
	}

	
	public function saveData ($fieldName, $cleanData, $sid) {
		$this->fieldName = $fieldName ;
		$this->cleanData = $cleanData ; 
		$this->sid = $sid ;
		$tableName = manageData::getTableMappings($fieldName) ; 
		$dataFieldName = manageData::getFieldMappings($fieldName) ; 
		$sql = mysql_query("UPDATE $tableName SET $dataFieldName = '$cleanData' WHERE id = '$sid' ") or die (mysql_error());		
			if(!$sql){
				echo 'There has been a data error. Please contact the webmaster.';
			}  ; 
	}
	
	public function checkUnique ($fieldName, $cleanData, $sid) {
		$this->fieldName = $fieldName ;		
		$this->cleanData = $cleanData ; 
		$this->sid = $sid ;
	    manageData::databaseConnect($connect) ;	    
	    $query = mysql_query("SELECT * FROM tableMappings WHERE ajaxField = '$fieldName' ");
			$row = mysql_fetch_row($query);	
				if (!$query) {
    				die('Invalid query CheckUniqueTest: ' . mysql_error());
					}
				$uniqueRow = $row[6] ; 	
				if ( $uniqueRow == '1' ) { 
					$errorFieldLabel	= manageData::getFieldLabel($fieldName);	    
				    $tableName 			= manageData::getTableMappings($fieldName) ; 
					$dataFieldName 		= manageData::getFieldMappings($fieldName) ; 
					$query = mysql_query("SELECT $dataFieldName FROM $tableName WHERE $dataFieldName = '$cleanData' and id != '$sid' ");
						 $num_rows = mysql_fetch_row($query);		
						 	if (!$query) {
			    				die('Invalid query CheckUnique: ' . mysql_error());
								}
							if (isset($num_rows[0])) {
							$msg = "1<br>$errorFieldLabel has been taken";
							return $msg ; 
							}
				}
	}
	
	
	public function getFieldLabel ($fieldName) {	    
	    $this->fieldName = $fieldName ; 
	    manageData::databaseConnect($connect);
		$query = mysql_query("SELECT fieldLabel FROM tableMappings WHERE fieldName = '$fieldName' ");				
			$row = mysql_fetch_row($query);
			$fieldLabel = $row['0'] ;
			if (!$query) {
    			die('Invalid query getFieldLabel: ' . mysql_error());
			}
		return $fieldLabel ;
	}
	    
	   
	public function checkLength ($fieldName, $cleanData) {
		$this->fieldName = $fieldName ;
		$this->cleanData = $cleanData ;	
		manageData::databaseConnect($connect);
		$query = mysql_query("SELECT minimumLength FROM tableMappings WHERE ajaxField = '$fieldName' ");
			$row = mysql_fetch_row($query);		
				if (!$query) {
    				die('Invalid query CheckUniqueTest: ' . mysql_error());
					}
				if ( $row[0] > '0' ) { 		
		$query = mysql_query("SELECT minimumLength FROM tableMappings WHERE ajaxField = '$fieldName' ");
			$row = mysql_fetch_row($query) ;
			$minumumLength = $row['0'] ;
			if (!$query) {
    			die('Invalid query checkLength: ' . mysql_error());
			}
			if ($minumumLength > 0) {
				if (strlen($cleanData) < $minumumLength) {
					$msg = "1<br>Use more than $minumumLength characters";
					return $msg ; 
				}
			}
		}
	}

	
	
}	



?>
Return current item: Rapid Form Builder