Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3600296
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:28:37+00:00 2026-05-18T20:28:37+00:00

I have implemented a dynamic creation of a HTML table using AJAX. Here’s what

  • 0

I have implemented a dynamic creation of a HTML table using AJAX.

Here’s what I’ve created:

index.php

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <script type="text/javascript" src="ContactController.js">
    </script>
</head>

<body>

    <div class="main-wrapper">

        <div id="menu">
            <a href="javascript:void(0)" onclick="getAllContacts()">
                Go to contacts
            </a>
        </div>

        <div id="main-content">
        </div>

    </div>

</body>

</html>

ContactsController.js

function getAllContacts() {

    // Manage new XmlHttpObject creation
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert ("Your browser is out of date. Please upgrade.");
        return;
    }

    var url = "getAllContacts.php";

    // Workaround for page caching
    url = url + "?sid=" + Math.round(Math.random() * 1000000000);

    xmlHttp.open("POST", url, true);

    // Manage XmlHttpObject state change
    xmlHttp.onreadystatechange = stateChanged;

    xmlHttp.send(null);
}

function stateChanged() {

    // Check if the XmlHttp request is complete
    if (xmlHttp.readyState == 4) {

        // Set the XmlHttp response in the contacts div
        document.getElementById("main-content").innerHTML = xmlHttp.responseText;
    }
}

// Creates a new XmlHttpObject
function GetXmlHttpObject() {

    var xmlHttp = null;

    try {
        // XmlHttpObject constructor for Chrome, Firefox, Opera, Safari
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        // XmlHttpObject constructor for Internet Explorer > v6.0
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            // XmlHttpObject constructor for Internet Explorer > v5.5
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

getAllContacts.php

<?php

include 'connectToMySQL.php';

$command = "SELECT * FROM contact";
$result = mysql_query($command);

echo "<table id='contactsTable' border='1'>";

// Table headers
echo "<tr>
          <th>Name</th>
      </tr>";

// Print all contacts
while($row = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td>
              <a href='#'
                 onclick=\"getContact('" . $row['DisplayName'] . "')\">"
                  . $row['DisplayName'] .
             "</a>
          </td>";
    echo "</tr>";
}

echo "</table>";

mysql_close();

?>

So, clicking on a Go to contacts link activates a getAllContacts javascript function. That function calls getAllContacts php function which retrieves the data from MySQL database and places it in the contactsTable table.

What I need is to tell the function to place that table in the main-content div located in the index.php page. How do I achieve this? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:28:38+00:00Added an answer on May 18, 2026 at 8:28 pm

    ok, so going from your comment about wanting two different possible target divs, just define your onreadystatechanged event inline and use a local variable to refer to the div…

    function getAllContacts() {
    
        // Manage new XmlHttpObject creation
        var xmlHttp = GetXmlHttpObject(); // MAKE THIS LOCAL INSTEAD OF GLOBAL!
        if (xmlHttp == null) {
            alert ("Your browser is out of date. Please upgrade.");
            return;
        }
    
        var url = "getAllContacts.php";
    
        // Workaround for page caching
        url = url + "?sid=" + Math.round(Math.random() * 1000000000);
    
        xmlHttp.open("POST", url, true);
    
        var targetDiv = document.getElementById(whateverIdYouWant);
    
        // Manage XmlHttpObject state change
        xmlHttp.onreadystatechange = function(){
            // Check if the XmlHttp request is complete
            if (xmlHttp.readyState == 4) {
    
                // Set the XmlHttp response in the contacts div
                targetDiv.innerHTML = xmlHttp.responseText;
            }
        }
    
        xmlHttp.send(null);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a generic IPropertyChangedNotifier using castle dynamic proxy. Here I intercept setter
I have implemented a python webserver. Each http request spawns a new thread. I
I have implemented tracing based on System.Diagnostics. I am also using a System.Diagnostics.TextWriterTraceListener, and
We have implemented a popup window as a modal dialog using the IE method:
I have implemented a linked list as a self-referencing database table: CREATE TABLE LinkedList(
I implemented Dynamic DataSource Routing for Spring+Hibernate according to this article . I have
I have a problem on using LinqToSQL with paging + dynamic sorting. These are
I have created one stored procedure which runs on 5000 users in tbluser table
I have a web form that I am attempting to implement dynamic drop down
I have implemented what I thought was a pretty decent representation of MVC in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.