Microsoft
Software
Hardware
Network
Question : can we apply ajax on the text that we fetch from database with the help of php?
can we apply ajax on the text that we fetch from database with the help of php?
Answer : can we apply ajax on the text that we fetch from database with the help of php?
Yes you can. I will show you a simple example of an AJAX form validation that I've made. And from that you can tweak it to do what you want to.
HTML FILE:
Username:
pan>
id="txtHint">
d>
E-mail:
/>
r>
FILE: getHint.php
require("../include/conn_m
ysql.php")
; ?>
require_once("../include/f
unctions.p
hp"); ?>
require_once("../include/f
orm_functi
ons.php");
?>
$string = $_GET['string'];
$type = $_GET['type'];
$query = "SELECT * FROM users";
$result = mysql_query($query);
if($type == "username"){
while($row = mysql_fetch_array($result)
){
if ($row['UserName'] == $string){
$response = "
* Användarnamnet är upptaget
";
break;
}
elseif(required_field($str
ing)){
$response = required_error();
}
else {
$response = "
";
}
}
echo $response;
}
elseif($type == "email"){
while($row = mysql_fetch_array($result)
){
if ($row['Email'] == $string){
$response = "
* E-mailadressen är redan registrerad här.
";
break;
}
elseif(required_field($str
ing)){
$response = required_error();
}
elseif(is_valid_email($str
ing) == true){
$response = email_error();
}
else {
$response = "
";
}
}
echo $response;
}
?>
FILE: showHint.js
var xmlHttp
var string
var type
function showHint(str)
{
type = str;
switch (type)
{
case "email":
string = document.getElementById("e
mailField"
).value
break;
case "username":
string = document.getElementById("u
sernameFie
ld").value
break;
default:
return;
}
if (string.length==0)
{
document.getElementById(ty
pe+"Hint")
.innerHTML
= "";
return;
}
xmlHttp=GetXmlHttpObject()
;
if (xmlHttp == null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="gethint.php?";
url +="type="+type;
url += "&string="+escape(string);
url +="&sid="+Math.random();
xmlHttp.onreadystatechange
=stateChan
ged;
xmlHttp.open("GET",url,tru
e);
xmlHttp.send(null);
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="Compl
ete")
{
document.getElementById(ty
pe+"Hint")
.innerHTML
=xmlHttp.r
esponseTex
t;
}
}
function GetXmlHttpObject()
{
var xmlHttpObject=null;
try
{
//FIREFOX OPERA SAFARI
xmlHttpObject=new XMLHttpRequest();
}
catch (e)
{
//IE
try
{
xmlHttpObject=new ActiveXObject("Msxml12.XML
HTTP");
}
catch (e)
{
xmlHttpObject=new ActiveXObject("Microsoft.X
MLHTTP");
}
}
return xmlHttpObject;
}
Hope this helps!
Random Solutions
Task Bar & Auto Hide Feature Not Popping Up When I Mouse To Bottom Of Screen
How do I get into my user in windows when I have forgotten my password?
is there any way to do image processing without using get/set pixel functions in c sharp or vb.net?
WinXP won't boot after Norton restore
Delay send message
configuring auto dial with Outlook and my land line
Alternative for "on delete cascade" of SQl-server 2000 in Sybase ASE
How could you convert a table of records into another table of the records as details records with subtotals and totals using an ADP file ?
Tilted angled text in GIMP or Paint Shop Pro?
Can't access mapped drives without re-entering username/password each time