hi everyone, i m very new to Ajax, and this is my first program in Ajax. I dont know what is wrong in the script. Please help me to find out the error, The main error is the value that i suppose to pass through url is not passing. thanking you in advance.
test.html
[HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator " CONTENT="EditPl us">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Descripti on" CONTENT="">
</HEAD>
<BODY>
<script language="javas cript" type="text/javascript">
function ajaxFunction()
{
var ajaxRequest; // the variable that makes Ajax possible
try
{ //opera + firefox safari
ajaxRequest = new XMLHttpRequest( );
}
catch (e)
{
try
{
ajaxRequest = new ActiveXOhject(" Msxml2.XMLHTTP" );
}
catch (e)
{
alert("Your browser is not support ajax");
return false;
}
}
//create a function that will receive data sent from the server
ajaxRequest.onr eadystatechange = function()
{
if(ajaxRequest. readyState ==4)
{
document.myform .name.value = ajaxRequest.res ponseText;
}
}
var empFirstName = document.getEle mentById('empFi rstName').value ;
var queryString = "?empFirstName= "+empFirstN ame;
ajaxRequest.ope n("GET","test-ajax.php"+query String, true);
ajaxRequest.sen d(null);
}
</script>
<form name="myform">
Employee First Name:<input type="text" id="empFirstNam e"></br>
<input type="button" onclick="ajaxFu nction()" value="Test">
</form>
</BODY>
</HTML>
[/HTML]
my php file is
test-ajax.php
[PHP]<?php
$error = "";
include ("config.inc.ph p");
include ("connect.inc.p hp");
$empFirstName = $_GET['empFirstName'];
$sql = "SELECT * FROM userdetails wherer empFirstName='$ empFirstName'";
$result = mysql_query($sq l) or die(mysql_error ());
if($result)
{
while($row=mysq l_fetch_array($ result))
{
echo "Employee Last Name: ".$row['empLastName'];
}
}
else
{
echo "Sorry";
}
?>[/PHP]
test.html
[HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator " CONTENT="EditPl us">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Descripti on" CONTENT="">
</HEAD>
<BODY>
<script language="javas cript" type="text/javascript">
function ajaxFunction()
{
var ajaxRequest; // the variable that makes Ajax possible
try
{ //opera + firefox safari
ajaxRequest = new XMLHttpRequest( );
}
catch (e)
{
try
{
ajaxRequest = new ActiveXOhject(" Msxml2.XMLHTTP" );
}
catch (e)
{
alert("Your browser is not support ajax");
return false;
}
}
//create a function that will receive data sent from the server
ajaxRequest.onr eadystatechange = function()
{
if(ajaxRequest. readyState ==4)
{
document.myform .name.value = ajaxRequest.res ponseText;
}
}
var empFirstName = document.getEle mentById('empFi rstName').value ;
var queryString = "?empFirstName= "+empFirstN ame;
ajaxRequest.ope n("GET","test-ajax.php"+query String, true);
ajaxRequest.sen d(null);
}
</script>
<form name="myform">
Employee First Name:<input type="text" id="empFirstNam e"></br>
<input type="button" onclick="ajaxFu nction()" value="Test">
</form>
</BODY>
</HTML>
[/HTML]
my php file is
test-ajax.php
[PHP]<?php
$error = "";
include ("config.inc.ph p");
include ("connect.inc.p hp");
$empFirstName = $_GET['empFirstName'];
$sql = "SELECT * FROM userdetails wherer empFirstName='$ empFirstName'";
$result = mysql_query($sq l) or die(mysql_error ());
if($result)
{
while($row=mysq l_fetch_array($ result))
{
echo "Employee Last Name: ".$row['empLastName'];
}
}
else
{
echo "Sorry";
}
?>[/PHP]
Comment