#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7

    Placement of AJAX response


    I am running an AJAX process to query my DB and return 2 textboxes with the queried items to take place of text boxes currently on the page. Everything runs fine EXCEPT where the coding seems to land. I intend it to replace the innerHTML within a DIV tag, but the original 2 blank text boxes remain, and the 2 new ones appear even above the drop-down calling this function. If I make a different selection from my drop-down, the 2 textboxes returned from AJAX do update. Any ideas? Thanks in advance.

    SELECT box triggers and DIV to be replaced...
    Code:
                                  <TABLE id="sick" style="display: none;">
                                    <TR>
                                      <TD style="text-align: right;width: 110px;"><H3>Name:&nbsp;</H3></TD>
                                      <TD colspan="2">
                                        <SELECT style="width: 150px;" onchange="live(this.value)">
                                          <OPTION>Select a name...</OPTION>
                                          <OPTION value="msanchez">Maria</OPTION>
                                          <OPTION value="ddeitsch">Dave</OPTION>
                                        </SELECT>
                                      </TD>
                                    </TR>
                                    <DIV id="userInfo">
                                    <TR>
                                      <TD style="text-align: right;"><H3>Department:&nbsp;</H3></TD>
                                      <TD colspan="2">
                                        <INPUT type="text" name="dept" value="" disabled />
                                      </TD>
                                    </TR>
                                    <TR>
                                      <TD style="text-align: right;"><H3>Manager:&nbsp;</H3></TD>
                                      <TD colspan="2">
                                        <INPUT type="text" name="mgr" value="" disabled />
                                      </TD>
                                    </TR>
                                    </DIV>
                                  </TABLE>
    Function...
    Code:
                            function live(str) {
                              if(str == "") {
                                document.getElementById("userInfo").innerHTML = "";
                                return;
                              } 
                              if(window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
                                xmlhttp = new XMLHttpRequest();
                              } else { // code for IE6, IE5
                                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                              }
                              xmlhttp.onreadystatechange = function() {
                                if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                                  document.getElementById("userInfo").innerHTML = xmlhttp.responseText;
                                }
                              }
                              xmlhttp.open("GET","modules/tore/live.php?q="+str,true);
                              xmlhttp.send();
                            }
    And the live.php returning the 2 new boxes...
    PHP Code:
      include('../../includes/db.php');
      
      
    $q $_GET['q'];
       
      
    $statement $link->prepare("SELECT `Department`, `Manager` FROM `userpass` WHERE `Username` = ?");
      
    $statement->bind_param('s'$q);
      
    $statement->execute();
      
    $statement->bind_result($col1,$col2);
      while (
    $statement->fetch()) {
        
    $var['department'] = $col1;
        
    $var['manager'] = $col2;
      }
      
    $statement->close();
      
      echo 
    '                                <TR>
                                      <TD style="text-align: right;"><H3>Department:&nbsp;</H3></TD>
                                      <TD colspan="2">
                                        <INPUT type="text" name="dept" value="' 
    $var['department'] . '" disabled />
                                      </TD>
                                    </TR>
                                    <TR>
                                      <TD style="text-align: right;"><H3>Manager:&nbsp;</H3></TD>
                                      <TD colspan="2">
                                        <INPUT type="text" name="mgr" value="' 
    $var['manager'] . '" disabled />
                                      </TD>
                                    </TR>'

    Last edited by Triple_Nothing; April 3rd, 2013 at 01:36 PM.
  2. #2
  3. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,124
    Rep Power
    4304
    What you're describing is the result of the browser automatically correcting invalidly nested elements. It seems you need a reminder of what was said in one of your previous threads:
    Originally Posted by Triple_Nothing
    Originally Posted by Kravvitz
    <tr> elements may not be direct children of a <div> element. Perhaps you should be using <tbody> elements instead of the <div>s.
    Kravvitz was right with his first comment. I was placing the 'id' in my DIV tags, and found out the inner TR and TD items were not direct children of such. So, I just made individual tables of the 2 sets of 3 rows I was trying to swap, and applied the 'id' to the TABLE tag of each one. This worked out just fine.
    I should forewarn you that in IE the innerHTML is readonly on <tbody> elements.

    What I would recommend is changing your PHP to output a JSON object and then use DOM methods like createElement() and appendChild() to build the new elements instead of using innerHTML. Though in this case, it's even much simpler to just update the values of the two text fields directly.

    Also it would be better to use a <th> (table header) element instead of a <td> that only contains an <h3>.
    Last edited by Kravvitz; April 3rd, 2013 at 02:26 PM.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo