CodeIgniter Forums

Full Version: form validation with ajax (mail validation)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]theighost[/eluser]
hi,
I am making a form, i want to check if the mail written in the form is not already used and doesn't exist in the database.

I don't want to use any special codeigniter form validation functions.

My idea was to make a function with ajax, at the top of the form, this function will apply a controller that will check if the mail is used or not, if its used the controller will 'print' false.
a javascript will check the result if is false, if its false then an alert ('the email is used').

this is the controller:
Code:
function validation()
    {
        
        $id=$this->uri->segment(3);//which is the name of the mail
        $sql = "select * from clients where emailClient like '".$id."'";
        $query = mysql_query($sql);
        $nr = mysql_num_rows($query);
        if($nr==0)
        {
            print 'true';
        }
        else
        {
            print 'false';
        }


    }
the ajax function in the form:
Code:
function valid(mail)
{
    var x;
   try{
            x=new XMLHttpRequest();    
   }
   catch(e)
   {
        try{
            x=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try{
              x=new ActiveXObject("Microsoft.XMLHTTP");    
            }    
            catch(e)
            {
               alert("Your browser doesn't support AJAX!");    
            }
        }
   }
  
   x.onreadystatechange=function()
   {
      if (x.readyState==4)
      {                  
         var str=x.responseText;
         var link="<?=base_url()?>clients/validation/"+mail;
   x.open("get",link,true);
   x.send(null);
   return str;
}
   }
}

and then the javascript comes:
Code:
function checkForm()
{
var mail;
mai = getElementById(mail);
if((valid(mail.value) == 'false'))
    {
        alert('the email is used.');
        mail.focus();
        return false;
    }
}

nothing happens with this script, please help.

El Forum

[eluser]Yash[/eluser]
which JavaScript framework you are using?

El Forum

[eluser]Pascal Kriete[/eluser]
No framework at all, it's just plain javascript.

Just a quick cursory glance:
1. You have 2 typos in your checkForm function: mail not mai, and the parameter for getElementById should be quoted.
2. You don't want to respond to 404s and the like, so the if statement should read: (x.readyState==4 && x.status == 200)

Make sure your php script is working as expected by visiting the url directly. If that works, then you can worry about the js.

El Forum

[eluser]theighost[/eluser]
i've written some of the script by hand in this forum and in a hurry, in the file they are correct.

actually i know that the PHP file works and i am worrying about the ajax

El Forum

[eluser]theighost[/eluser]
any help???

El Forum

[eluser]xwero[/eluser]
Easier than using your own code try using a javascript framework or if you don't want to use a framework use an ajax library like jx. This way there is less chance for a bug to occur when making an ajax request.

Check out the request in firebug. That should give you a clue if there is something wrong or not.

El Forum

[eluser]gurthgor[/eluser]
I recommend you to use Xajax, its quite easy and well documented, you just have to use xajax_functionname(params) in javascript and that loads you a php function where you can check if the email is available or not.

You can use it as a library of codeigniter with no problems and add the xajax functions in the controller.