Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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.

  • #2
    New Coder
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    353
    Thanks
    9
    Thanked 52 Times in 51 Posts
    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

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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);

  • #5
    Regular Coder
    Join Date
    Jul 2003
    Posts
    117
    Thanks
    0
    Thanked 17 Times in 17 Posts
    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.

  • #6
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks a lot guys!! im going to try these codes.look interesting

    http://rhodesmarket.blogspot.com/


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •