View Full Version : Ajax script redirecting to Apache home page

10-08-2008, 10:53 AM
I have an issue running my first Ajax script.Iam checking for a loginid field entered by user for availaibility.Iam connecting to database through ajax to check for existence of the same.
The html+Javascript code for Ajax request object is as follows:
<title>Registration Form</title>
<script language="javascript" type="text/javascript">
function checkId() {
var ajaxreq;
try {
ajaxreq=new ActiveXObject("Microsoft.XMLHTTP"); }
catch(e) {
try{ ajaxreq=new ActiveXObject("Msxml2.XMLHTTP"); }
catch(e2) {
try{ajaxreq=new XMLHTTPRequest; }
catch(e3) {
alert('Pls update your Browser to use this feature');
return false; }
ajaxreq.onreadystatechange=function() {
if (ajaxreq.readyState==4) {
document.getElementById('checkid').innerHTML=ajaxreq.responseText; }
var loginid=document.getElementById('loginid').value;
var querystring="?loginid="+loginid;

<body style="border-style: solid;border-width: 20px;border-color: #808080;padding: 10px;">
<form name="registration" method="post" action="ValidationFailed.php">
<p align="center"><font size="3">Pls fill in the below Registration form.Fields marked with <font color="red">*</font> are Mandatory</font></p>
<div style="background-image: url(rb6.jpg);background-repeat: no-repeat;width: 167px;height: 30px;text-align: center;padding: 7px;">
<strong>Account Details</strong></div>
<table border="1" cellpadding="0" cellspacing="0">
<td width="300" style="background-color: #f5f5f5;padding: 4px;"><strong>Login ID</strong><font color="Red">*</font></td>
<td width="500" style="background-color: #fffafa;padding: 4px;"><input type="text" name="LoginId" id="loginid" value="" size="30" maxlength="40">
<a href="" onclick="checkId()">Check Availability</a></td>
<div id="checkid"></div>

My issue is whenever I click on the link,iam redirected to Apache Home Page which says that "If you can see this, it means that the installation of the Apache web server software on this system was successful.Blah Blah"
Is there any configuration to be changed to make ajax working with apache?
Or can anyone point to me if anything is wrong in the above code??


10-08-2008, 04:49 PM
<a href="" onclick="checkId()">Check Availability</a></td>

try changing that to href="#"

10-08-2008, 07:03 PM
try changing that to href="#"

Already tried that without any success...

10-09-2008, 03:51 PM
When you load that login php [ajaxvalidation.php] directly into the browser, does it do the same exact thing?

It should have nothing to do with your Ajax call.


10-10-2008, 06:56 AM
When you load that login php [ajaxvalidation.php] directly into the browser, does it do the same exact thing?

It should have nothing to do with your Ajax call.


The ajaxvalidation.php script is a simple script to connect to database.
When i load this script directly into the browser,i get the message as
"You can use this ID" (i.e it goes into one of the condition of IF clause.)
So,that means that there is no issue with php script.
PHP script in use is as follows:
$connection=mysql_connect('localhost','satish','abcd') or die('Error connecting to server: '.mysql_error());
mysql_select_db('PHPTesting') or die('Error connecting to Database: '.mysql_error());
$query="select loginid from users where loginid='$loginid'";
$result=mysql_query($query) or die('Error executing query: '.mysql_error());
if(mysql_num_rows($result)==1 )
$display='A User with the same Id already exists.Pls enter another ID';
$display='You can use this ID';
echo $display;

10-10-2008, 03:11 PM
Okay, so now lets see where your Ajax call is going.

If you have Firebug, enable it to see the XMLHttpRequests in the options. Look to see where the url is going. Make sure that it looks right.

If you are using IE, download Fiddler [http://www.fiddlertool.com/fiddler/] and run it. With fiddler open, make the Ajax call and see the request in Fiddler. Make sure that is going to the right place. Fiddler will also let you see the headers and such.


10-11-2008, 08:08 AM
Thanks all for your support.The problem is resolved,but I dont know how....
Yesterday(without changing anything in code),when I clicked on the link,the page was not redirected to apache home page and neither was there any error or message.it was just like clicking at blank area of the page.
Then I checked the html file and got the mistake I commited:
<a href="" onclick="checkId()">Check Availability</a></td>
<div id="checkid"></div>

Changed the above to:
<a href="" onclick="checkId()">Check Availability</a>
<div id="checkid"></div>

But I still cannot understand why it was redirecting to apache home page 2-3 days back...
Anyway,Iam happy my code is working fine now...Lets now get deeper into the world of AJAX..

10-11-2008, 02:24 PM
Well that is invalid HTML markup so that is not right either.

If you are going to use a link, than you should add something to the href and also add return false like someone else already suggested.

<a href="#" onclick="checkId(); return false;">Check Availability</a>

Also be careful naming things pretty closely with functions and element ids, sometimes IE has issues.