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

  • Home
  • SEARCH
  • 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 9158223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:11:52+00:00 2026-06-17T13:11:52+00:00

I changed my code to the following: $(document).ready(function () { $(‘#add’).on(‘click’, function () {

  • 0

I changed my code to the following:

$(document).ready(function () {
            $('#add').on('click', function () {
                var $contacts = $(".contact");
                $("<div/>").append($("<input>", {
                  "type": "text",
                  "name": "contact[" + $contacts.length + "][name]"
                })).insertAfter($contacts.last());
            });
          });   

<div class="contact">
Name: &nbsp; <input type="contact[0][name] type="text">
</div>
<div class="contact">
Name: &nbsp; <input type="contact[1][name] type="text">
</div>
<div class="contact">
Name: &nbsp; <input type="contact[2][name] type="text">
</div>

This works and allows me to add another row. However, I need help on two things:

1) When I add another row and hit submit. The $_POST array only shows the first three values, not the dynamically added arrays.

2) How do I add more the text to the appended html? It only shows the input box, not “Name: ” in front of it.

Thanks!

This is what print_r($_POST) is returning (I have other form fields in there):

 Post:
 Array
  (
[custsubmit] => Submit
[contact] => Array
    (
        [0] => Array
            (
                [id] => 
                [name] => name1
            )

        [1] => Array
            (
                [id] => 
                [name] => name2
            )

        [2] => Array
            (
                [id] => 
                [name] => name3
            )

    )
)
  • 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-17T13:11:53+00:00Added an answer on June 17, 2026 at 1:11 pm

    You can start by using a multidimensional array.

    <div class="contact">
      <input type="text" name="contact[0][name]">
      ...
    </div>
    <div class="contact">
      <input type="text" name="contact[1][name]">
      ...
    </div>
    ...
    

    I changed the id of your <div> container to a classname since the id attribute is meant to be unique.

    Now, to add a new row you can use something as follows.

    var $contacts = $(".contact");
    $("<div/>", {
      "class": "contact",
      "html": $("<input>", {
        "type": "text",
          "name": "contact[" + $contacts.length + "][name]"
      })
    }).insertAfter($contacts.last());
    

    This way, you will receive a nice array in PHP to work with which will look like this:

    Array
    (
        [0] => Array
            (
                [name] => foo
            )
        [1] => Array
            (
                [name] => bar
            )
        ...
    )
    

    You can use more complicated layout by making use of a DOM template as I previously answered here.

    .template {
      display: none;
    }
    

    <div class="contact template">Name: &nbsp;
      <input type="contact[{{id}}][name]" type="text">
    </div>
    

    $(function() {
      var $template = $(".template");
      $('#add').on('click', function () {
        var $contacts = $(".contact").not(".template");
        $template.template({
          id: $contacts.length
        }).insertAfter($contacts.last());
      });
    });
    

    You can see it here.

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

Sidebar

Related Questions

I am having problems with the following code $(document).ready(function() { var $selection = $('<div
I have the following code: JQuery $(document).ready(function(){ var imglink; imglink = 'url(http://poweruser.aeiou.pt/wp-content/uploads/2012/12/google.jpg)'; $('#trick_img').css('content',imglink); $(document).on(click,change,function(){
My following code works fine. $(document).ready(function() { $('a.address').click(function() { $('#box').slideDown(slow); $.ajax({ type: POST, url:
I have the following code in my head jQuery(document).ready(function() { var pic = 0;
The following code: jQuery(document).ready(function($) { function getBooks() { var query = ajax.php?do=allbooks; $.ajax({ dataType:
Can I get some help with the following code? $(document).ready(function() { $('#searchform').submit(function() { var
i have the following jquery code: $(document).ready(function() { $('.restrictiveOptions input[type!=checkbox], .restrictiveOptions select').change(function() { //
I have the following that fires off when a checkbox is changed. $(document).ready(function() {
I have the following code: $(document).ready(function(){ $(.datepicker).each(function() { $(this).datepicker({ beforeShowDay: function(date){ return [$(this).parent().find(span.days).html(), ]
I am using following code to populate DropDown: $(document).ready(function(){ $('#inDistrict').sSelect(); $(#inDistrict).change(function(){ $.getJSON(filldistricts.php,{id: $(this).val(), ajax:

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.