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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robin1983
    New Member
    • Oct 2007
    • 99

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

    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]
  • rpnew
    New Member
    • Aug 2007
    • 189

    #2
    Hi,
    Are you getting the value for following line.
    Code:
     
    var empFirstName = document.getElementById('empFirstName').value;
    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

    Comment

    • robin1983
      New Member
      • Oct 2007
      • 99

      #3
      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

      Originally posted by rpnew
      Hi,
      Are you getting the value for following line.
      Code:
       
      var empFirstName = document.getElementById('empFirstName').value;
      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

      Comment

      • rpnew
        New Member
        • Aug 2007
        • 189

        #4
        Originally posted by robin1983
        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="empFirstNam e"></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

        Comment

        • robin1983
          New Member
          • Oct 2007
          • 99

          #5
          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


          Originally posted by rpnew
          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="empFirstNam e"></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

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            you have a typo here:

            [CODE=javascript]ajaxRequest = new ActiveXOhject(" Msxml2.XMLHTTP" );
            [/CODE]
            it should be:

            [CODE=javascript]ajaxRequest = new ActiveXObject(" Msxml2.XMLHTTP" );[/CODE]
            kind regards

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              A line you made wrong.
              [code=javascript]
              jaxRequest = new ActiveXOhject(" Msxml2.XMLHTTP" );
              //The ActiveXOhject spelling is wrong.
              [/code]

              And you should change the coding pattern.

              [code=javascript]
              if(typeof XMLHttpRequest! ='undefined') ajaxRequest = new XMLHttpRequest( );
              else{
              ajaxRequest = new ActiveXOhject(" Microsoft.XMLHT TP");
              if(ajaxRequest= =null) ajaxRequest = new ActiveXOhject(" Msxml2.XMLHTTP" );
              }
              [/code]


              [code=javascript]
              //the expression
              ajaxRequest.rea dyState ==4 && ajaxRequest.sta tus == 200
              [/code]

              Now run the .php alone woth specified parameter.
              And see whether it runs or not.

              Debasis Jana

              Comment

              • robin1983
                New Member
                • Oct 2007
                • 99

                #8
                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 ..
                Originally posted by dmjpro
                A line you made wrong.
                [code=javascript]
                jaxRequest = new ActiveXOhject(" Msxml2.XMLHTTP" );
                //The ActiveXOhject spelling is wrong.
                [/code]

                And you should change the coding pattern.

                [code=javascript]
                if(typeof XMLHttpRequest! ='undefined') ajaxRequest = new XMLHttpRequest( );
                else{
                ajaxRequest = new ActiveXOhject(" Microsoft.XMLHT TP");
                if(ajaxRequest= =null) ajaxRequest = new ActiveXOhject(" Msxml2.XMLHTTP" );
                }
                [/code]


                [code=javascript]
                //the expression
                ajaxRequest.rea dyState ==4 && ajaxRequest.sta tus == 200
                [/code]

                Now run the .php alone woth specified parameter.
                And see whether it runs or not.

                Debasis Jana

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by robin1983
                  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.

                  Comment

                  • dmjpro
                    Top Contributor
                    • Jan 2007
                    • 2476

                    #10
                    Originally posted by robin1983
                    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

                    Comment

                    • rpnew
                      New Member
                      • Aug 2007
                      • 189

                      #11
                      [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

                      Comment

                      • maddycruise
                        New Member
                        • Mar 2008
                        • 1

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

                        can u tell me plsssssssssssss sssssssssssssss sss.....
                        ............... ..............b harath.
                        Last edited by acoder; Mar 3 '08, 03:20 PM. Reason: removed quote

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Originally posted by maddycruise
                          hi my name is bharath
                          how to connect ajax application to database using servlets? not with php or asp..

                          can u tell me plsssssssssssss sssssssssssssss sss.....
                          ............... ..............b harath.
                          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!

                          Comment

                          Working...