Populate second text box based on value extracted from first text box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #16
    A lot. You need to put the PHP code in a separate file. To use Ajax, you need to use the XMLHttpRequest object, not location.href. Get a simple Ajax page working first and then extend to your particular problem.

    Comment

    • sarega
      New Member
      • Oct 2008
      • 82

      #17
      I am new to this javascript so can anyone please tell me how can my problem be solved using ajax.
      Thanks

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #18
        Have you tried one or two of the tutorials in the thread I linked to? Get that working and understand it first, then you can work on your problem.

        Comment

        • sarega
          New Member
          • Oct 2008
          • 82

          #19
          Can you please help me with the code its urgent:(

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #20
            I can't do it for you, but I can help. Is the latest version of your code still the one you posted in post #15?

            Comment

            • sarega
              New Member
              • Oct 2008
              • 82

              #21
              How exactly does ajax solve the problem?I could not understand them in the tutorial

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #22
                It can, if you want to avoid a page reload. Is that a requirement?

                Comment

                • sarega
                  New Member
                  • Oct 2008
                  • 82

                  #23
                  Yes its the same code and i want to avoid a page reload can you help me asap....

                  Comment

                  • sarega
                    New Member
                    • Oct 2008
                    • 82

                    #24
                    How do I pass a variable in ajax to php code and how to extarct it in the php file?

                    Comment

                    • sarega
                      New Member
                      • Oct 2008
                      • 82

                      #25
                      Code:
                      function test1()
                       {
                       var a=document.getElementById("first").innerHTML;
                       var b=a.split('/');
                       var c=b[0];
                       var d=b[1];
                       if(c!="snow") {
                       document.getElementById("second").innerHTML="";
                       return;
                       }
                       xmlHttp=GetXmlHttpObject();
                       if(xmlHttp==null) {
                       alert("your browser does not support ajax");
                       return;
                       }
                       var url="cd.php";
                       url=url+"?d="+d;
                       url=url+"&sid="+math.random();
                       xmlHttp.onreadystatechange=StateChanged;
                       xmlHttp.open("GET",url,true);
                       xmlHttp.send(null);
                       }
                      
                       function GetXmlHttpObject()
                       {
                       var xmlHttp=null;
                       try
                         {
                           // Firefox, Opera 8.0+, Safari
                             xmlHttp=new XMLHttpRequest();
                               }
                               catch (e)
                                 {
                                   // Internet Explorer
                                     try
                                         {
                                             xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
                                                 }
                                                   catch (e)
                                                       {
                                                           xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                                                               }
                                                                 }
                                                                 return xmlHttp;
                                                                 }
                      
                      function StateChanged()
                      {
                      if (xmlHttp.readyState==4)
                      {
                      document.getElementById("second").innerHTML=xmlHttp.responseText;
                      }
                      }
                      </script>
                      <body>
                       <input type="text" id="first" size="40" onKeyUp="test1();"/><br><br>
                       <input type="text" id="second" size="40"/>
                       </body>
                       </html>
                      and the php code is

                      [PHP]<?php
                      $fog=$_GET['d'];
                      echo $fog;
                      ?>
                      [/PHP]
                      What is wrong with this code am not able to print anything in the second text box.
                      Can somebody reply to it fast plz...
                      Last edited by acoder; Oct 17 '08, 07:40 AM. Reason: Added [code] tags

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #26
                        Instead of setting the text box's innerHTML, set its value property.

                        Comment

                        • sarega
                          New Member
                          • Oct 2008
                          • 82

                          #27
                          even that is not working:(

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #28
                            Are you getting any errors? Post the latest version of your code with the changes you've made (using code tags please).

                            Comment

                            • sarega
                              New Member
                              • Oct 2008
                              • 82

                              #29
                              Code:
                              <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                               "http://www.w3.org/TR/html4/loose.dtd">
                               <html>
                               <head>
                               <title>Untitled Document</title>
                               <script type="text/javascript">
                               function test1()
                               {
                               var a=document.getElementById("first").value;
                               var b=a.split('/');
                               var c=b[0];
                               var d=b[1];
                               if(c!="fog") {
                               document.getElementById("second").value="";
                               return;
                               }
                               xmlHttp=GetXmlHttpObject();
                               if(xmlHttp==null) {
                               alert("your browser does not support ajax");
                               return;
                               }
                               var url="cd.php";
                               url=url+"?d="+d;
                               url=url+"&sid="+Math.random();
                               xmlHttp.onreadystatechange=StateChanged;
                               xmlHttp.open("GET",url,true);
                               xmlHttp.send(null);
                               }
                              
                               function GetXmlHttpObject()
                               {
                               var xmlHttp=null;
                               try
                                 {
                                   // Firefox, Opera 8.0+, Safari
                                     xmlHttp=new XMLHttpRequest();
                                       }
                                       catch (e)
                                         {
                                           // Internet Explorer
                                             try
                                                 {
                                                     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
                                                         }
                                                           catch (e)
                                                               {
                                                                   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                                                                       }
                                                                         }
                                                                         return xmlHttp;
                                                                         }
                              
                              function StateChanged()
                              {
                              if (xmlHttp.readyState==4)
                              {
                              document.getElementById("second").value=xmlHttp.responseText;
                              }
                              }
                              </script>
                              </head>
                              <body>
                               <input type="text" id="first" size="40" onkeyup="test1();"/><br><br>
                               <input type="text" id="second" size="40"/>
                               </body>
                               </html>
                              php code is
                              Code:
                              <?php
                              $fog=$_GET['d'];
                              echo $fog;
                              ?>
                              Last edited by Dormilich; Jan 22 '09, 10:33 AM. Reason: fixed [code] tags

                              Comment

                              • acoder
                                Recognized Expert MVP
                                • Nov 2006
                                • 16032

                                #30
                                Please enclose your posted code in [code] tags (See How to Ask a Question).

                                This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

                                Please use [code] tags in future. Thanks.

                                Moderator.

                                Comment

                                Working...