Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-08-2011, 06:34 AM   PM User | #1
tibet000000
New Coder

 
Join Date: Dec 2010
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
tibet000000 is an unknown quantity at this point
struggling to pull together email validation pieces

I am trying to make a simple AJAX interaction where users type an email address into a form and when the from reaches ready state 4, the embedded java script makes a call to the server to run a PHP regexp check on the string submitted by the user, validating that it is in the right email format.

I don't really know what I am doing, and my method has been basically to grope blindly forward utilizing various internet tutorials.

However, it seems to me that what I have should work, but of course it doesn't.

so here's the html framework up to the point of interest (the double email field for validation) with the javascript snippet located in the head
:
Code:
<script language="javascript" src="checkemail.js"></script>
Code:
<form id="register" method="post" action="userdatabase.php" name="register">
            <p><label>First Name: </label><input type="text" name="fname" class="text"/></p>
            <p><label>Last Name: </label><input type="text" name="lname" class="text"/></p>
            <p><label>Your Email: </label><input type="text" name="email" id="email" onChange="AjaxFunction();" class="text"/>
            <div id="rsp_email"><!----></div>
            <p><label>Re-enter Email: </label><input type="text" name="email2" class="text"/></p>

Here is the actual javascript 'checkemail.js':
Code:
<script type="text/javascript">
function AjaxFunction(email)
{
var AjaxRequest;
try
		{
		// Firefox, Opera 8.0+, Safari
		httpxml=new XMLHttpRequest();
		}
	catch (e)
	{
	// Internet Explorer
		try
		{
		httpxml=new ActiveXObject("Msxml2.XMLHTTP");
		}
	catch (e)
	{
		try
		{
		httpxml=new ActiveXObject("Microsoft.XMLHTTP");
		}
	catch (e)
	{
	alert("Your browser does not support AJAX!");
	return false;
	}
}
}

ajaxRequest.onreadystatechange= function()
{
	if(readystate==4)
	{
		document.register.email.value = ajaxRequest.ResponseText;
	}
	ajaxRequest.open("GET","emailcheck.php", true);
	ajaxRequest.send(null);
}

}
and finally here is the php that the js utilizes- 'emailcheck.php':

PHP Code:
if(isset(!''){
if (!
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"$email)){
echo 
"<center>Invalid email</center>";
}else{
echo 
"<center>Valid Email</center>";}


I am sure I have lost the plot somewhere within this, as I am still struggling to conceptualize all the threads being passed around. but syntactically I thought I was doing ok, but apparently not.

Please help me figure out what the problem is somebody... anybody.

I am running this through my godaddy hosting, and MAMP to see if it works.


brian

Last edited by tibet000000; 01-08-2011 at 06:03 PM..
tibet000000 is offline   Reply With Quote
Old 01-08-2011, 06:11 PM   PM User | #2
tibet000000
New Coder

 
Join Date: Dec 2010
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
tibet000000 is an unknown quantity at this point
and here is the php file that the form posts to in the action userdatabase.php-

PHP Code:
<?php

$fname
=$_POST['fname'];
$lnam=$_POST['lname'];
$email=$_POST['email'];

$pass=$_POST['pass'];

$gender=$_POST['gender'];
$bdate=$_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];

$link=mysql_connect('xxxxxxx','brianjoseff123','xxxx');
if(!
$link)
{
    die(
'Could not connect: ' .mysql_error());    
}

mysql_select_db('brianjoseff123',$link);

$query"INSERT INTO registeredusers ('NULL' ,'fname','lname','email','pass','gender','bdate') VALUES ('$fname'.'$lname'.'$email'.'$pass'.'$gender'.'$bdate)";



?>

Last edited by tibet000000; 01-08-2011 at 07:41 PM..
tibet000000 is offline   Reply With Quote
Old 01-09-2011, 12:24 PM   PM User | #3
hdewantara
Regular Coder

 
hdewantara's Avatar
 
Join Date: Aug 2009
Location: Jakarta, Indonesia.
Posts: 287
Thanks: 4
Thanked 39 Times in 39 Posts
hdewantara is an unknown quantity at this point
Since you need only to validate email address, I would suggest to do that client side i.e. by javascript; no need to use ajax. I think I saw some good ones in forum, have you tried searching?

Hendra
hdewantara is offline   Reply With Quote
Old 01-09-2011, 09:32 PM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Most importantly you somehow misunderstood how ajax requests work

What you did:
- The AjaxFunction() only creates the request object "httpxml". It does not start the request!
- You try to set onreadystatechange for another object "ajaxRequest" on the global level. This will be executed before AjaxFunction(), so there is no request object yet.
- You try to start a request inside the callback of ajaxRequest. The callback would be the function that has to be executed after the request's ready state changes. It can only change after it has been started though ....

Summary: Lots of basic errors

This is how ajax requests usually work
- You create the request object. Let's call it httpxml:
Code:
try {
		// Firefox, Opera 8.0+, Safari
		httpxml=new XMLHttpRequest();
} catch (e) {
	// Internet Explorer
		try {
		   httpxml=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		   try {
		      httpxml=new ActiveXObject("Microsoft.XMLHTTP");
		   } catch (e) {
	              alert("Your browser does not support AJAX!");
	           }
                }
}
- You call the open method of that specific object
Code:
httpxml.open('GET', url, true);
- You set the callback for that specific object
Code:
httpxml.onreadystatechange = function() {
   if(httpxml.readyState == 4 && httpxml.status == 200) {
      // The request has been sucessfully finished, take the output and
      // do something useful with it. The output is available in
      // httpxml.responseText
   }
}
- You send the request
Code:
httpxml.send(null);
devnull69 is offline   Reply With Quote
Old 01-10-2011, 03:48 PM   PM User | #5
prasanthmj
Regular Coder

 
Join Date: Jul 2003
Posts: 117
Thanks: 0
Thanked 17 Times in 17 Posts
prasanthmj is an unknown quantity at this point
Quote:
alert("Your browser does not support AJAX!");


Ajax validation is needed when you need to some server side verification (like cross checking with a database) Else, simple Javascript + server side validation is good/enough.
prasanthmj is offline   Reply With Quote
Old 01-19-2011, 02:45 AM   PM User | #6
nikos777
New to the CF scene

 
Join Date: Jan 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
nikos777 is an unknown quantity at this point
thanks a lot guys!! im going to try these codes.look interesting

http://rhodesmarket.blogspot.com/
nikos777 is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, email, forms, validation

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:59 PM.


Advertisement
Log in to turn off these ads.