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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:40:28+00:00 2026-06-01T11:40:28+00:00

I have a HTML form which allows a user to add rows ad hoc

  • 0

I have a HTML form which allows a user to add rows ad hoc using JavaScript.

The Form:

<form method="post" action="send.php">
<table id="table">
<tr><th>job</th><th>comment</th></tr>
<tr>
<td><textarea name = "job[]"></textarea></td>
<td><textarea name = "comment[]"></textarea></td>
<tr>
</table>
<input type ="button" value="add entry" onclick="add('table')"/>
<input type ="submit" id="submit" value="submit"/>

The JavaScript:

 function add(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
    var colCount = table.rows[0].cells.length;

    for (var i=0; i<colCount; i++) {
        var newcell = row.insertCell(i);
        var newentry = document.createElement('textarea');
        newentry.type = "text";
        newcell.appendChild(newentry);
    }
}

The PHP code

$job = $_POST['job'];
$comment = $_POST['comment'];

//code  to connect to database

$add_stmt = $mysqli->prepare("INSERT INTO job (job, comment) VALUES (?, ?)");

$foreach($job as $a => $b) {
    $add_stmt->bind_param("ss", $job[$a], $comment[$a]);
    $add_stmt->execute();
}

When I click on the “add entry” button, a row will be successfully created. However, only data in the first row will be submitted to the database. Using

echo "row count is" . count($_POST['job']);

I got “row count is 1” even though I have several rows of data.
Can somebody figure out what might have gone wrong with my code? Thanks a bunch!

  • 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-06-01T11:40:30+00:00Added an answer on June 1, 2026 at 11:40 am

    You have to set name property for fields that you add:

    // create array with names
    var names = ['job[]', 'comment[]'];
    
    function add(tableID) {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);
        var colCount = table.rows[0].cells.length;
    
        for (var i=0; i<colCount; i++) {
            var newcell = row.insertCell(i);
            var newentry = document.createElement('textarea');
            newentry.type = "text";
    
            // set name
            newentry.name = names[i];
    
            newcell.appendChild(newentry);
        }
    }
    

    You can try make two files: index.html and send.php and insert this content:

    index.html:

    <html>
    <head>
        <script>
            var names = ["job[]", "comment[]"];
            function add(tableID) {
                var table = document.getElementById(tableID);
                var rowCount = table.rows.length;
                var row = table.insertRow(rowCount);
                var colCount = table.rows[0].cells.length;
    
                for (var i=0; i<colCount; i++) {
                    var newcell = row.insertCell(i);
                    var newentry = document.createElement('textarea');
                    newentry.name = names[i];
                    newentry.type = "text";
                    newcell.appendChild(newentry);
                }
            }
        </script>
    </head>
    <body>
        <form method="post" action="send.php">
        <table id="table">
        <tr><th>job</th><th>comment</th></tr>
        <tr>
        <td><textarea name = "job[]"></textarea></td>
        <td><textarea name = "comment[]"></textarea></td>
        <tr>
        </table>
        <input type ="button" value="add entry" onclick="add('table')"/>
        <input type ="submit" id="submit" value="submit"/>
    </form>
    
    </body>
    </html>
    

    send.php:

    <?php
    $job = $_POST['job'];
    $comment = $_POST['comment'];
    
    var_dump($job);
    var_dump($comment);
    ?>
    

    And run on you server – this works for me. Make sure that you have names set properly.

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

Sidebar

Related Questions

I have a post html form, and the action is index.php, which is the
i have my code(html/javascript) here which allows user to input the size and color
I have a simple HTML Form that allows the user to enter some text
i have a form which allows the user to upload some files to a
I have a template which allows the user to edit their user information. <form
I have a input type=file tag in my html which allows user to select
I have a html form which have a select list box from which you
I have this html form which has options: <tr> <td width=30 height=35><font size=3>*List:</td> <td
I have a an HTML form which contains the YAHOO rich text editor on
I have an HTML Contact us form which I plant on my client's HTML

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.