You are on page 1of 7

1.

Email validation Using Ajax & PHP


<script type="text/javascript"> function AjaxFunction(email) { var httpxml; 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; } } } function stateck() { if(httpxml.readyState==4) { document.getElementById("msg").innerHTML=httpxml.responseText; } } var url="email-ajax.php"; url=url+"?email="+email; url=url+"&sid="+Math.random(); httpxml.onreadystatechange=stateck; httpxml.open("GET",url,true); httpxml.send(null); } </script>

The HTML code to display the form ( as in the above demo ) is here <form name=f1 action=''> Your First Name <input type=text name=n1><br> Any email address <input type=text name=email onBlur="AjaxFunction(this.value);"><div id="msg"></div> <br> Your City Name <input type=text name=city> <input type=submit value=Submit > <input type=hidden value=test name=todo> </form> Now the code kept within email-ajax.php file is here, this is same code as explained at email validation tutorial <? $email=$_GET['email']; echo $email; if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $email)){ echo "<font color=red> Invalid email</font>"; }else{ echo "<font color=green> Valid Email</font>";} ?>

call_user_method() (use call_user_func() instead) call_user_method_array() (use call_user_func_array() instead) define_syslog_variables() dl() ereg() (use preg_match() instead) ereg_replace() (use preg_replace() instead) eregi() (use preg_match() with the i modifier instead) eregi_replace() (use preg_replace() with the i modifier instead) set_magic_quotes_runtime() and its alias, magic_quotes_runtime() session_register() (use the $_SESSION superglobal instead) session_unregister() (use the $_SESSION superglobal instead) session_is_registered() (use the $_SESSION superglobal instead) set_socket_blocking() (use stream_set_blocking() instead) split() (use preg_split() instead) spliti() (use preg_split() with the i modifier instead) sql_regcase() mysql_db_query() (use mysql_select_db() and mysql_query() instead) mysql_escape_string() (use mysql_real_escape_string() instead) Passing locale category names as strings is now deprecated. Use the LC_* family of constants instead. The is_dst parameter to mktime(). Use the new timezone handling functions instead.

2.PAGINATION
<html><head> <script type="text/javascript"> function ajaxFunction (val) { //document.writeln ( val) var httpxml; 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;

} } } function stateChanged() { if(httpxml.readyState==4) { var myObject = eval('(' + httpxml.responseText + ')'); var str="<table><tr><th>ID</th><th>Name</th><th>Class</th><th>Mark</th></tr>"; for(i=0;i<myObject.data.length;i++) { str = str + "<tr><td>" + myObject.data[i].id + " </td><td>" + myObject.data[i].name + " </td><td>" + myObject.data[i].myclass + " </td><td>" + myObject.data[i].mark + "</td></tr>" } var endrecord=myObject.value[0].endrecord myForm.st.value=endrecord; if(myObject.value[0].end =="yes"){ document.getElementById("fwd").style.display='inline'; }else{document.getElementById("fwd").style.display='none';} if(myObject.value[0].startrecord =="yes"){ document.getElementById("back").style.display='inline'; }else{document.getElementById("back").style.display='none';} str = str + "</table>" document.getElementById("txtHint").innerHTML=str; } } var url="apaging.php"; var myendrecord=myForm.st.value; url=url+"?endrecord="+myendrecord;

url=url+"&direction="+val; url=url+"&sid="+Math.random(); //alert(url) httpxml.onreadystatechange=stateChanged; httpxml.open("GET",url,true); httpxml.send(null); document.getElementById("txtHint").innerHTML="Please Wait...."; } </script> </head> <body onload="ajaxFunction('fw')";> <form name="myForm" onsubmit="ajaxFunction(this.form); return false"> <input type=hidden name=st value=0> <table width=300> <tr><td width=150><input type=button id="back" value=Prev onClick="ajaxFunction('bk'); return false"></td> <td width=150 align=right><input type=button value=Next id="fwd" onClick="ajaxFunction('fw'); return false"></td></tr></tr> </form> <tr><td colspan=2><div id="txtHint"><b>Records will be displayed here</b></div></td></tr> </table> </body> </html>

Apaging.php connecttodb($servername,$dbname,$dbusername,$dbpassword); function connecttodb($servername,$dbname,$dbuser,$dbpassword) { global $link; $link=mysql_connect ("$servername","$dbuser","$dbpassword");

if(!$link){die("Could not connect to MySQL");} mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error()); } //////////////////////////////// Main Code sarts ///////////////////////////////////////////// $endrecord=$_GET['endrecord'];// To take care global variable if OFF if(strlen($endrecord) > 0 and !is_numeric($endrecord)){ echo "Data Error"; exit; } $limit=10;// Number of records per page $nume=mysql_num_rows (mysql_query ("select * from student")); //echo "endrecord=$endrecord limit=$limit "; if($endrecord < $limit) {$endrecord = 0;}

switch($_GET['direction']) { case "fw": $eu = $endrecord ; break;

case "bk": $eu = $endrecord - 2*$limit; break;

default: echo "Data Error"; exit; break; }

if($eu < 0){$eu=0;} $endrecord =$eu+$limit; $t=mysql_query("select * from student limit $eu,$limit"); $str= "{ \"data\" : ["; while($nt=mysql_fetch_array($t)){ $str=$str."{\"id\" : \"$nt[id]\", \"name\" : \"$nt[name]\", \"myclass\" : \"$nt[class]\", \"mark\" : \"$nt[mark]\"},"; //$str=$str."{\"myclass\" : \"$nt[class]\"},"; } $str=substr($str,0,(strLen($str)-1)); if(($endrecord) < $nume ){$end="yes";} else{$end="no";} if(($endrecord) > $limit ){$startrecord="yes";} else{$startrecord="no";} $str=$str."],\"value\" : [{\"endrecord\" : $endrecord,\"limit\" : $limit,\"end\" : \"$end\",\"startrecord\" : \"$startrecord\"}]}"; echo $str;