AJAX, Javascript, and more
During the previous week I learnt form validation in Ajax . It was a very interesting process. There is a advantage in Ajax form validation, because the validation is completed without page reloading. Here the Ajax Code:-
var subject = document.getElementById(“txt1″).value;
if (subject.length == 0)
{
document.getElementById(“txtHint”).innerHTML = “”;
return;
}
var selected_selPlans = document.form1.selPlans.selectedIndex;
var selected_value = document.form1.selPlans.options[selected_selPlans].value;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open(“GET”,”gethint2.php?q=”+subject+”&r=”+selected_value,true);
xmlhttp.send();
}
I also learnt how to retrieve the multiple checkbox values in which input names are the same.It is very easy to get single checkbox value. The Javascript code is as follows :-
for (var i=0; i < document.myform.plan.length; i++)
{
if (document.myform.plan[i].checked == true)
{
Plan=Plan+" "+ document.myform.plan[i].value;
}
}
It was very interesting for me to create a table of two column which displayed the from database. The code is very simple :-
$con = mysql_connect("192.168.1.100","codez","codez123");
$database = mysql_select_db("dealer_db",$con);
$dirStateInput = $_GET['msg'];
$query = "SELECT * FROM dealerdetails WHERE state LIKE '".$dirStateInput."'";
$result = mysql_query($query,$con);
$num = mysql_num_rows($result);
$x = 1;
$output = "";
?>
while($dirDealDetList = mysql_fetch_array($result)){//Display two column records
if($x%2 == 0){
$output .= "
“.$dirDealDetList['Address'].”
“.$dirDealDetList['Phone1'].”
“.$dirDealDetList['Phone2'].”
“.$dirDealDetList['Phone3'].”
“.$dirDealDetList['Mobile1'].”
“; //for even records
}else{
$output .= “
“.$dirDealDetList['Address'].”
“.$dirDealDetList['Phone1'].”
“.$dirDealDetList['Phone2'].”
“.$dirDealDetList['Phone3'].”
“.$dirDealDetList['Mobile1'].”
“; // for odd records
}
$x++;
}
if($num%2 != 0)$output .= “”;//for blank td
echo “
“;
?>
Latest posts by Sahel Aktar
- SQL Query Optimization - September 21st, 2011
- How to create Google Charts - August 17th, 2011
- How to work with ckfinder and ckeditor incase of image upload - July 11th, 2011
- PHP Thumbnail Generator - June 15th, 2011
- SQL Training - April 25th, 2011
