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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:50:30+00:00 2026-06-01T02:50:30+00:00

I am trying to make a page where users can add as many rows

  • 0

I am trying to make a page where users can add as many rows to a table as they want, and then the values of the inputs (there is an input in each cell of the table) are passed back to the page.

My problem comes when trying to make the name of each dynamically generated input box different. I’ve tried to create a variable to do it, but the syntax I’m using, which works in other places, doesn’t seem to work when creating new elements.

Here is the code in question:

    if ($_POST['dept_1'] != null){  //If there was an input
    $i = intval($_POST['num_of_rows']);
    while ($i != 0){
        $dept = $_POST['dept_' . $i];
        $hours = $_POST['hours_' . $i];
        echo $dept . " " . $hours . "\n";
        $i--;
    }
}

…..

var i = 2;
        $("document").ready(function(){
            $("#newrow").click(function(e){
                $("#maintable").append('<tr> \
                                        <td><input type="text" name="dept_" + i size="5" maxlength="5" /></td> \
                                        <td><input type="text" name="hours_" + i size="5" maxlength="1" /></td> \
                                    </tr>');
                                    alert("dept_" + i);
                e.preventDefault();
                $("#hiddenvalue").attr("value", "" + i + "");
                i = i + 1;
            });
        });

$POST[‘dept‘ . $i] works when $i = 1, because dept_1 is written into the HTML of the page.

alert(“dept_” + i) works, concatenating the value of i to the string.

name=”dept_” + i does not work. Inspecting the new element, its name is “dept_ + i”

Why is this happening, and how can I fix it?

  • 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-01T02:50:31+00:00Added an answer on June 1, 2026 at 2:50 am

    The proper solution would be using an array by naming the element dept[]; that will give you an array $_POST['dept'] on the PHP side.

    Besides that, syntax highlighting already shows you the problem: Your +i is inside the string; use this instead:

    $("#maintable").append('<tr> \
        <td><input type="text" name="dept_' + i + '" size="5" maxlength="5" /></td> \
        <td><input type="text" name="hours_' + i + '" size="5" maxlength="1" /></td> \
      </tr>');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a php login page that allows users to access
I'm trying to make a page in php that takes rows from a database,
Trying to make a make generic select control that I can dynamically add elements
I am trying to make a page with a table enhanced with the DataTables
trying to make a page which will recursively call a function until a limit
Here is a page I am trying to make print friendly, so it will
I'm trying to make a web scraper that will parse a web-page of publications
I'm trying to make it so that a form submit doesn't cause the page
I'm trying to make a WordPress site that has six lists on a page,
i'm trying to make my site with a few languages. Every page includes config-file,

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.