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 7559247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:35:24+00:00 2026-05-30T12:35:24+00:00

My table is being populated using ajax from a mysql database. I have a

  • 0

My table is being populated using ajax from a mysql database. I have a text field below it which adds the entered data to the database. All this works fine, but what i want to do is on adding that new row to the table, i want to dynamically show the user that their entry has been added (or simply refresh that div when new field has been added). Currently aim able to achieve that using a simple function:

function addItem(new_item, edit_table) {
     var itemName = new_item;
     var newRow = document.createElement('tr');
     var rowCell = document.createElement('td');
     rowCell.textContent = itemName;
     // rowCell.addClass("grid");
     newRow.appendChild(rowCell);
     edit_table.appendChild(newRow);
  }

However this does not let me add extra functionalities to that row e.g. i have a delete and edit icon upon hover. So by using this function i am able to show the new row added but its not exactly functioning. So i recon the better option would be to refresh that div when this happens.

I am using the following code to call the addItems method:

$('#b_go').click(function(){
    //some other conditions, then using ajax to post the data
    success: function (data) {
    if(data == 'success') {
        addItem(new_row, selected_table);
   }

HTML for the table:

<table class="table table-striped table-bordered home" id="subjects">
    <thead>
        <th>Subject Title</th>
    </thead>
    <tbody>
        <?php
        $sql = mysql_query("select Title from Subject");
        while($row = mysql_fetch_array($sql)) {
            echo "<tr><td>";
            echo $row['Title']?> <!--using html to output buttons for delete and edit for each row-->
         <?;echo "</td></tr>";
        }
            ?>
    </tbody>
</table>

Css for the above table:

.table {
  width: 100%;
  margin-bottom: 18px;
}
.table th, .table td {
  padding: 8px;
  line-height: 18px;
  text-align: left;
  border-top: 1px solid #ddd;
}
.table th {
  font-weight: bold;
  vertical-align: bottom;
}
.table td {
  vertical-align: top;
}
.table thead:first-child tr th, .table thead:first-child tr td {
  border-top: 0;
}

.table-striped tbody tr:nth-child(odd) td, .table-striped tbody tr:nth-child(odd) th {
  background-color: #f9f9f9;
}

.home {
    width: 100%;
    border-collapse: collapse;
    text-align: left;
}

.home th {
    font-size: 15px;
    font-weight: 400;
    color: #039;
    padding: 10px 8px;
    border-bottom: 2px solid #6678b1;
}
.home td {
    line-height:15px;
    border-bottom: 1px solid #E0E0E0;
    border-left: 1px solid #E0E0E0;
    font-size: 12px;
    color: #404040;
    padding: 9px 8px 3px 8px;
}
.home tbody tr:hover td {
    background-color:#E6E6FF;
    cursor: pointer; 
}

Any help would be appreciated!

  • 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-30T12:35:25+00:00Added an answer on May 30, 2026 at 12:35 pm

    I have to leave work, but here’s a quick and dirty answer.

    When adding new elements dynamically that have pre-existing functions/events/actions that are already bound, the new elements will not automatically inherent the events/actions of their siblings. I recommend using jQuery for something like this.

    For jQuery versions greater than 1.3 – use jQuery LIVE() function:
    http://api.jquery.com/live/

    Description: This will map the data passed to your new event handlers needed

    For jQuery versions 1.7 or higher – use jquery ON() function:

    http://api.jquery.com/on/

    Description: method attaches event handlers to the currently selected set of elements in the jQuery object. This will attach the event handler to any new element you create.

    Update: 11:57 AM Tuesday: Based on your comment. You need to use bind(‘click’) or on(”) function when you SUBMIT the form.

     // First : Change your click code to this. You'll need the bind() function here.  This will make it so your events will bind to each new row
    
       $('#b_go').bind("click", function(){
            //some other conditions, then using ajax to post the data
            success: function (data) {
            if(data == 'success') {
            addItem(new_row, selected_table);
       }
    
     // Change your function to this:
        function addItem(new_item, edit_table) {
            var itemName = new_item;
            var newRow = document.createElement('tr');
            var rowCell = document.createElement('td');
            rowCell.textContent = itemName;
             $(rowCell).addClass("grid"); // Make sure GRID is the class being applied to your other TDs/TRs etc
            newRow.appendChild(rowCell);
            edit_table.appendChild(newRow);
           $(edit_table +' tr:odd td').css('background','#f9f9f9'); // Set color for all odd rows.
    
      }
    

    HOW TO RELOAD TABLE:

    STEP #1 – Create a new < div > layer with an ID #getDataGrid. THIS MUST WRAP AROUND YOUR TABLE.

    STEP #2 – Create a new file like : data-grid.php and include the following HTML. Please also include any PHP business logic that would be needed to make the appropriate database calls to make this code successful:

    <table class="table table-striped table-bordered home" id="subjects">
        <thead>
            <th>Subject Title</th>
        </thead>
        <tbody>
           <?php
            $sql = mysql_query("select Title from Subject");
            while($row = mysql_fetch_array($sql)) {
                echo "<tr><td>";
                echo $row['Title']?> <!--using html to output buttons for delete and edit for each row-->
             <?;echo "</td></tr>";
            }
                ?>
         </tbody>
    </table>
    

    STEP #3 : Update your click function:

    $('#b_go').bind("click", function(){
            //some other conditions, then using ajax to post the data
            success: function (data) {
            if(data == 'success') {
    
            addItem(new_row, selected_table);
    
            $.get('data-grid.php', function(getTable) {
                $('#getDataGrid').html(getTable);
            });
    
       }
    

    EXPLANATION. What this is doing on your click function is using jQuery to essentially perform a “GET” (just as PHP GET would perform). We are retrieveing our newly created data-grid.php file, and then PLACING the contents into the #getDataGrid div layer we created that wraps around the table. What this will do will actually wipe out the currently displayed table with the new displayed table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview which is being populated at runtime(using sessions) by entering the
I have table rows of data in html being filled from a CGI application.
I have an html table containing values that are being generated from javascript. How
I have a MySQL table with about 5,000,000 rows that are being constantly updated
I need to extract some text from a HTML table I tried using tblGridHeader.Rows[0].InnerText.ToString()
Is it possible to create a trigger on a field within a table being
I have an HTML table with one column being email addresses; spambot issues aside,
I have a table with the only relevant column being completionDate, a number of
Consider an indexed MySQL table with 7 columns, being constantly queried and written to.
I want to display the column 'name' after being found from the table mytest.

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.