www.webdeveloper.com
Results 1 to 2 of 2

Thread: Parsing out div content from XMLHttpRequest

Hybrid View

  1. #1
    Join Date
    Jul 2011
    Posts
    54

    Parsing out div content from XMLHttpRequest

    I am trying to use XMLHttpRequest() to call a php file which queries a database and returns a row of data separating each column by a semicolon.

    Here is the JavaScript that I am using:

    PHP Code:
    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==&& xmlhttp.status==200)
                {
                    
    //document.getElementById("graph1").innerHTML=xmlhttp.responseText;
                    
    var data xmlhttp.responseText;
                    
    selectionData data.split(";");                
                }
            }
            
    xmlhttp.open("GET","householdinfoquery?fam=" FAMTYPE "&year=" YEAR "&subgeo=" OECD,true);
            
    xmlhttp.send(); 
    This code calls the "householdinfoquery" page. Since this site is built on drupal, the householdinfoquery is a page node containing the following PHP code:

    PHP Code:
    <?php
        $year 
    $_GET['year'];
        
    $famTypeStr $_GET['fam'];
        
    $subgeoStr $_GET['subgeo'];

        
    $con mysql_connect(/*CONNECTION DETAILS HERE*/);

        if (!
    $con)
        {
            die(
    'Could not connect: ' mysql_error());
        }

        
    mysql_select_db($GLOBALS["dbName"]);
        
    mysql_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci'");

        
    $sql="SELECT * FROM `householdinfo_" $year "` WHERE geography ='99' && subgeography ='" $subgeoStr"' && familytype LIKE'" $famTypeStr "%'";

        
    $result mysql_query($sql);
        
        while(
    $row mysql_fetch_array($result))
        {
            echo 
    $row['geography'] . ";" $row['subgeography'] . ";" $row['familytype'] . ";" $row['familycount'] . ";" $row['medianincome']; 
        }

        
    mysql_close($con);
    ?>
    However, because of the way drupal formats these nodes and prints them, I get the following text back in my "responsetext".

    HTML Code:
    <!-- start node.tpl.php -->
    
    <div id="node-36893" class="node odd full-node node-type-page">
      <div class="meta">
    
          </div>
    
      <div class="content">
    
        99;1;C02 - COUPLE FAMILIES WITH 1 CHILDREN < 18;769980;76900  </div>
    
      </div>
    
    <!-- /#node-36793 --> 
    The data that I am interested in here is the line inside of the "content" div. I would like to store this data inside a javascript array.

    Is there an easy way to modify either the PHP code to return just the inner HTML of the "content" div and not all of the other formatting, OR is there simple way to parse this data out once I have it stored in a javascript "data" variable.

    Thanks for all the help!

  2. #2
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,774

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles