By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,677 Members | 1,076 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,677 IT Pros & Developers. It's quick & easy.

hi, my ajax function is not working ..........

P: 99
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="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<script language="javascript" 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.onreadystatechange = function()
{
if(ajaxRequest.readyState ==4)
{
document.myform.name.value = ajaxRequest.responseText;
}
}
var empFirstName = document.getElementById('empFirstName').value;
var queryString = "?empFirstName="+empFirstName;
ajaxRequest.open("GET","test-ajax.php"+queryString, true);
ajaxRequest.send(null);
}
</script>

<form name="myform">
Employee First Name:<input type="text" id="empFirstName"></br>
<input type="button" onclick="ajaxFunction()" value="Test">
</form>
</BODY>
</HTML>

[/HTML]

my php file is
test-ajax.php
[PHP]<?php
$error = "";
include ("config.inc.php");
include ("connect.inc.php");

$empFirstName = $_GET['empFirstName'];

$sql = "SELECT * FROM userdetails wherer empFirstName='$empFirstName'";
$result = mysql_query($sql) or die(mysql_error());
if($result)
{
while($row=mysql_fetch_array($result))
{
echo "Employee Last Name: ".$row['empLastName'];
}
}
else
{
echo "Sorry";
}

?>[/PHP]
Jan 23 '08 #1
Share this Question
Share on Google+
12 Replies


rpnew
100+
P: 188
Hi,
Are you getting the value for following line.
Expand|Select|Wrap|Line Numbers
  1.  
  2. var empFirstName = document.getElementById('empFirstName').value;
  3.  
I mean are you sure that this variable is assigned properly and you are gettting the value of the textbox in this variable.
If not sure then use ALERT box after this line to check this variable. Or in any case get back here.

Regards,
RP
Jan 23 '08 #2

P: 99
yes, i did not get this value through the URL. So, is there any problem, the way i assign the value of empFirstName, if yes pls tell me
waiting for your reply

Hi,
Are you getting the value for following line.
Expand|Select|Wrap|Line Numbers
  1.  
  2. var empFirstName = document.getElementById('empFirstName').value;
  3.  
I mean are you sure that this variable is assigned properly and you are gettting the value of the textbox in this variable.
If not sure then use ALERT box after this line to check this variable. Or in any case get back here.

Regards,
RP
Jan 23 '08 #3

rpnew
100+
P: 188
yes, i did not get this value through the URL. So, is there any problem, the way i assign the value of empFirstName, if yes pls tell me
waiting for your reply
Hi,
I've tried your code and it works fine... Sending the request and getting the output as required in my case.

Anyways, So there isn't any proble the way you are assigning the value. You wont get the value for this variable if you dont put anything in your textboxt.
i.e.
[HTML]
Employee First Name:<input type="text" id="empFirstName"></br>
[/HTML]

If you are entering any value and then clicking the buttong below it from where you are calling ajax function it works fine with my code. But in case you dont enter anything in TEXT box then you wont get value for your variable and then response from Ajax request.
Post back with your problem.
Regards,
RP
Jan 23 '08 #4

P: 99
HI, thanks for the response, actually i was working on Safari browser, but when i try to run the program in IE 5+ its, saying that you browser is not support,
but when i try to run in mozila and safari, they dont show any result .. and in the url the value is not showing in the url

http://localhost/habsons_/test.html
before click in the button
but when i click its shows only http://localhost/habsons_/test.html? and no any kind of action. So can u tell me what is the exact problem


Hi,
I've tried your code and it works fine... Sending the request and getting the output as required in my case.

Anyways, So there isn't any proble the way you are assigning the value. You wont get the value for this variable if you dont put anything in your textboxt.
i.e.
[HTML]
Employee First Name:<input type="text" id="empFirstName"></br>
[/HTML]

If you are entering any value and then clicking the buttong below it from where you are calling ajax function it works fine with my code. But in case you dont enter anything in TEXT box then you wont get value for your variable and then response from Ajax request.
Post back with your problem.
Regards,
RP
Jan 23 '08 #5

gits
Expert Mod 5K+
P: 5,327
you have a typo here:

Expand|Select|Wrap|Line Numbers
  1. ajaxRequest = new ActiveXOhject("Msxml2.XMLHTTP");
  2.  
it should be:

Expand|Select|Wrap|Line Numbers
  1. ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
kind regards
Jan 23 '08 #6

dmjpro
100+
P: 2,476
A line you made wrong.
Expand|Select|Wrap|Line Numbers
  1. jaxRequest = new ActiveXOhject("Msxml2.XMLHTTP");
  2. //The ActiveXOhject spelling is wrong.
  3.  
And you should change the coding pattern.

Expand|Select|Wrap|Line Numbers
  1. if(typeof XMLHttpRequest!='undefined') ajaxRequest = new XMLHttpRequest();
  2. else{
  3.  ajaxRequest = new ActiveXOhject("Microsoft.XMLHTTP");
  4.  if(ajaxRequest==null) ajaxRequest = new ActiveXOhject("Msxml2.XMLHTTP");
  5. }
  6.  

Expand|Select|Wrap|Line Numbers
  1. //the expression
  2. ajaxRequest.readyState ==4 && ajaxRequest.status == 200
  3.  
Now run the .php alone woth specified parameter.
And see whether it runs or not.

Debasis Jana
Jan 23 '08 #7

P: 99
when i run the .php file along by passing the parameter, but when i access it from test.html file its not working. even the value is not passing through the url. i dont know what to do, can u code whole once again and i will copy from that ..
A line you made wrong.
Expand|Select|Wrap|Line Numbers
  1. jaxRequest = new ActiveXOhject("Msxml2.XMLHTTP");
  2. //The ActiveXOhject spelling is wrong.
  3.  
And you should change the coding pattern.

Expand|Select|Wrap|Line Numbers
  1. if(typeof XMLHttpRequest!='undefined') ajaxRequest = new XMLHttpRequest();
  2. else{
  3.  ajaxRequest = new ActiveXOhject("Microsoft.XMLHTTP");
  4.  if(ajaxRequest==null) ajaxRequest = new ActiveXOhject("Msxml2.XMLHTTP");
  5. }
  6.  

Expand|Select|Wrap|Line Numbers
  1. //the expression
  2. ajaxRequest.readyState ==4 && ajaxRequest.status == 200
  3.  
Now run the .php alone woth specified parameter.
And see whether it runs or not.

Debasis Jana
Jan 23 '08 #8

acoder
Expert Mod 15k+
P: 16,027
when i run the .php file along by passing the parameter, but when i access it from test.html file its not working. even the value is not passing through the url. i dont know what to do, can u code whole once again and i will copy from that ..
You're trying to set the value of a form field called 'name', but this doesn't exist in your form.
Jan 23 '08 #9

dmjpro
100+
P: 2,476
when i run the .php file along by passing the parameter, but when i access it from test.html file its not working. even the value is not passing through the url. i dont know what to do, can u code whole once again and i will copy from that ..
Just what i told to do you first do that then tell us what's the problem?

Debasis Jana
Jan 23 '08 #10

rpnew
100+
P: 188
[PHP]
$sql = "SELECT * FROM userdetails wherer empFirstName='$empFirstName'";
//check spelling of wehre
[/PHP]
You have mistake in your SQL statement as well check that as well
Anyways,
Your code is working fine with my machine(FC4/FF2)(WinXP/IE7-From network).... However i've added a textbox named 'name'-which is another textbox. And My PHP Scipt just echo backs whatever you are putting in first textbox and write it to the second one.



Regards,
RP
Jan 23 '08 #11

P: 1
hi my name is bharath
how to connect ajax application to database using servlets? not with php or asp..

can u tell me plsssssssssssssssssssssssssssssss.....
.............................bharath.
Mar 3 '08 #12

acoder
Expert Mod 15k+
P: 16,027
hi my name is bharath
how to connect ajax application to database using servlets? not with php or asp..

can u tell me plsssssssssssssssssssssssssssssss.....
.............................bharath.
Servlets would be JSP, correct? The client-side code is pretty much the same.

By the way, I think you need to check your keyboard - your S and . keys seems to have got stuck!
Mar 3 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.