[SOLVED]having trouble validating with ajax/php...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

[SOLVED]having trouble validating with ajax/php...

Post by dhrosti »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I hope you can help with this because I really cant fugure it out! i think i might be the Validate class that holds the problem but i'll post all the code incase im wrong...

It's not validating the given string as I expect it to, so the $validator->isEmail() function always returns false.

My text input box sends this request...

Code: Select all

onblur="sendReq(this.value, this.id, 'email_valid');"
Which is picked up by the javascript...

Code: Select all

function createXmlHttp() {
	var xmlHttp;
	try {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp = new XMLHttpRequest();
		}
	}
	return xmlHttp;
}
function sendReq(str, objId, responseId) {
	var obj=document.getElementById(objId);
	var response=document.getElementById(responseId);
	if (str.length==0) {
		response.innerHTML='';
		return;
	}
	var xmlHttp=createXmlHttp();
	if (xmlHttp==null) {
 		alert ("Your browser does not support AJAX!");
  		return;
  	}
	var url="process.php?"+obj+"="+escape(str)+"&a="+Math.random();
	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4 && xmlHttp.status==200) {
			response.innerHTML=xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET",url);
	xmlHttp.send(null);
}
Now, that all works well, i can send and receive data with my php file..

Code: Select all

require_once("lib/Validator.php");

$str = urldecode($_GET['email']);

$validator = new Validator($str);

if ($validator->isEmail()) {

	echo "<img src='img/tick.gif' />";

} else {

	echo "<img src='img/cross.gif' />";

}
But i think the problem is here somewhere...

Code: Select all

class Validator {

	var $str;
	
	var $pattern;

	function Validator($str) {
	
		$this->str=$str;
	
	}
	
	function isEmail() {
			
		if (!preg_match("/^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i", $this->str)) {
            
			return false;
        	
		} else {
			
			return true;
				
		}
	
	}

}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by dhrosti on Fri May 04, 2007 11:21 am, edited 1 time in total.
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post by dhrosti »

...anyone? im desperate, i still cant figure it out

Edit: i got there, the problem was in the javascript.
Post Reply