Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2008
    Location
    India
    Posts
    88
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Ajax script redirecting to Apache home page

    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:
    <html>
    <head>
    <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;
    ajaxreq.open("GET","ajaxvalidation.php"+querystring,true);
    ajaxreq.send(null);
    }
    </script>
    </head>

    <body style="border-style: solid;border-width: 20px;border-color: #808080;padding: 10px;">
    <!--<center>-->
    <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>
    <br>
    <fieldset>
    <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>
    <div>
    <table border="1" cellpadding="0" cellspacing="0">
    <tbody>
    <tr>
    <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>
    </tr>
    </tbody>
    </table>
    </div>
    </fieldset>
    </form>
    </body>
    </html>



    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??

    Thanks...

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by satish_j View Post
    Code:
    <a href="" onclick="checkId()">Check Availability</a></td>
    try changing that to href="#"
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    New Coder
    Join Date
    Aug 2008
    Location
    India
    Posts
    88
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question

    Quote Originally Posted by shyam View Post
    try changing that to href="#"
    Already tried that without any success...

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #5
    New Coder
    Join Date
    Aug 2008
    Location
    India
    Posts
    88
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question

    Quote Originally Posted by A1ien51 View Post
    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.

    Eric
    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:
    [PHP]
    <?php
    $loginid=$_GET['loginid'];
    $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';
    else
    $display='You can use this ID';
    echo $display;
    ?>
    [PHP]

  • #6
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #7
    New Coder
    Join Date
    Aug 2008
    Location
    India
    Posts
    88
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Thumbs up

    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>
    </tr>


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

    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..
    Thanks..

  • #8
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •