Ok, so another solution which I thought would be cool if I share with other new bees like me using AJAX.
I have a new user registration form, I allow users to enter their email IDs, Name, Phone number etc.
Now sometimes it is very irritating from the user perspective that they have filled all the lengthy form and then they got to know that their email ID already existed in the database.
So, think smartly.
Why not to notify the user as soon as he enters the email ID. hmmm !!! Looks cool but how to do it?
first of all make a html form and within the email id field make use of the onblur function and call another javascript function like mentioned below.
<input type="text" name="Email" onblur="email_chk(2);"/></td>
Now the email_chk function in the javascript
include this is Head element of your form
<PLEASE ALSO DO NOT FORGOT TO INCLUDE THE AJAX.JS FILE WHICH IS REQUIRED TO CALL THE AJAX.CONNECT FUNCTION>
<script language="javascript" type="text/javascript">
var gchkblur=0;
function email_chk(chkblur)
{
gchkblur=chkblur;
var id="";
var txt_name=document.onlineform.Email.value; //----getting the value of the email id that the user has entered
if(txt_name!="" && isValidEmail(txt_name)) //---> custom function to validate the email as well
ajaxConn.connect(siteUrl+"dup_chk.php", "POST", "txt_name="+txt_name+"&id="+id,fnWhenDone_email_chk);
//The above is the main function where we are calling another file named as dup_chk.php(included later in this post)
//Now if you look closely into the function I am actually passing the parameters
parameter number 1 --> telling the function to refer to dup_chk.php file
parameter number 2 --> giving a POST method
parameter number 3 --> providing the value to compare
parameter number 4 --> the ID
parameter number 4 --> to execute the function when I am back from the dup_chk file
}
function fnWhenDone_email_chk(XML)
{
//alert(XML.responseText);
if(XML.responseText==1)
{
document.getElementById("email").innerHTML = "<<<<< Email ID already exist. >>>>>";
document.onlineform.Email.focus();
document.onlineform.Email.select();
//alert(chkErr);
}
if(XML.responseText==2)
{
document.getElementById("email").innerHTML =" ";
if(gchkblur==1)
{
document.onlineform.action="resource.php";
document.onlineform.method="post";
document.onlineform.submit();
}
}
}
</script>
Now my dup_chk will look something like this
dup_chk.php
<?php
session_start();
ob_start();
include("includes/functions.php");
autoout();
sessionChk();
userStatus();
$tstrval = "";
if($_REQUEST["txt_name"]!="") // For Email
{
$query="select resource_id from resource_master where rm_email_id ='" . trim($_REQUEST["txt_name"]) . "'";
if($_REQUEST["id"]!="")
{
$query.=" and resource_id <>'" . $_REQUEST["id"] . "'";
}
$result=mysql_query($query);
while($row=mysql_fetch_assoc($result))
{
$tstrval = $tstrval . $row['resource_id'];
}
}
//echo $_REQUEST["txt_name"] . "<!!>" . $_REQUEST["id"];
if($tstrval!="")
echo "1";
else
echo "2";
?>
In the above file I am hitting a query to run a comparison check and then passing the values in a array.
If my conditions gets satisfied then my variable $tstrval will return 1 and that I will catch on the fnWhenDone_email_chk
This is how I can compare the values on a real time basis.
This is a very good technology AJAX is also used by GOOGLE. It actually reduces a lot of work.
My next post will follow another example on the AJAX itself.
Just post your comment if you have any doubts on the above function.
happy coding !! Cheers
I have a new user registration form, I allow users to enter their email IDs, Name, Phone number etc.
Now sometimes it is very irritating from the user perspective that they have filled all the lengthy form and then they got to know that their email ID already existed in the database.
So, think smartly.
Why not to notify the user as soon as he enters the email ID. hmmm !!! Looks cool but how to do it?
first of all make a html form and within the email id field make use of the onblur function and call another javascript function like mentioned below.
<input type="text" name="Email" onblur="email_chk(2);"/></td>
Now the email_chk function in the javascript
include this is Head element of your form
<PLEASE ALSO DO NOT FORGOT TO INCLUDE THE AJAX.JS FILE WHICH IS REQUIRED TO CALL THE AJAX.CONNECT FUNCTION>
<script language="javascript" type="text/javascript">
var gchkblur=0;
function email_chk(chkblur)
{
gchkblur=chkblur;
var id="";
var txt_name=document.onlineform.Email.value; //----getting the value of the email id that the user has entered
if(txt_name!="" && isValidEmail(txt_name)) //---> custom function to validate the email as well
ajaxConn.connect(siteUrl+"dup_chk.php", "POST", "txt_name="+txt_name+"&id="+id,fnWhenDone_email_chk);
//The above is the main function where we are calling another file named as dup_chk.php(included later in this post)
//Now if you look closely into the function I am actually passing the parameters
parameter number 1 --> telling the function to refer to dup_chk.php file
parameter number 2 --> giving a POST method
parameter number 3 --> providing the value to compare
parameter number 4 --> the ID
parameter number 4 --> to execute the function when I am back from the dup_chk file
}
function fnWhenDone_email_chk(XML)
{
//alert(XML.responseText);
if(XML.responseText==1)
{
document.getElementById("email").innerHTML = "<<<<< Email ID already exist. >>>>>";
document.onlineform.Email.focus();
document.onlineform.Email.select();
//alert(chkErr);
}
if(XML.responseText==2)
{
document.getElementById("email").innerHTML =" ";
if(gchkblur==1)
{
document.onlineform.action="resource.php";
document.onlineform.method="post";
document.onlineform.submit();
}
}
}
</script>
Now my dup_chk will look something like this
dup_chk.php
<?php
session_start();
ob_start();
include("includes/functions.php");
autoout();
sessionChk();
userStatus();
$tstrval = "";
if($_REQUEST["txt_name"]!="") // For Email
{
$query="select resource_id from resource_master where rm_email_id ='" . trim($_REQUEST["txt_name"]) . "'";
if($_REQUEST["id"]!="")
{
$query.=" and resource_id <>'" . $_REQUEST["id"] . "'";
}
$result=mysql_query($query);
while($row=mysql_fetch_assoc($result))
{
$tstrval = $tstrval . $row['resource_id'];
}
}
//echo $_REQUEST["txt_name"] . "<!!>" . $_REQUEST["id"];
if($tstrval!="")
echo "1";
else
echo "2";
?>
In the above file I am hitting a query to run a comparison check and then passing the values in a array.
If my conditions gets satisfied then my variable $tstrval will return 1 and that I will catch on the fnWhenDone_email_chk
This is how I can compare the values on a real time basis.
This is a very good technology AJAX is also used by GOOGLE. It actually reduces a lot of work.
My next post will follow another example on the AJAX itself.
Just post your comment if you have any doubts on the above function.
happy coding !! Cheers
Comments
Post a Comment