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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:40:59+00:00 2026-06-14T20:40:59+00:00

Mostly as a personal practice, i’m writing a framework for a personal website i’m

  • 0

Mostly as a personal practice, i’m writing a framework for a personal website i’m working on. Currently I’m trying to create a generic way to display forms on my website. I’m using JSON models to model the form, which is transformed into HTML by PHP. What i’m currently trying to allow is something like this:

[type] [contact address]
[type] [contact address]
+ Add More

I know that with form element names like “type[]”, the value of the various type fields is sent as an array to php. However, in this case, a type and a contact address belong together. I know various ways to achieve this, like looping over the types, and then getting the related other fields, but somehow transforming it into an associative array seems much easier to use in the code.

Note, i know various ways to achieve this, but i’m trying to go for some best practices. I’d prefer doing this in PHP, as i’m trying to minimize the amount of javascript on my website.

  • 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-14T20:41:00+00:00Added an answer on June 14, 2026 at 8:41 pm

    You can actually build multidimensional arrays like contact[id][type] and contact[id][address]. Therefore, if you have an id, which could be an incremental value if you are adding new ones, supply the same id for both. PHP will see it as a multidimensional array like $_POST['contact'][1]['type'] == 'sometype, $_POST['contact'][1]['address'] == 'theaddress' .

    So your new form inputs look like:

    <input id='someid_type_123' name='contact[123][type]' />
    <input id='someid_address_123' name='contact[123][address]' />
    
    <input id='someid_type_124' name='contact[124][type]' />
    <input id='someid_address_124' name='contact[124][address]' />
    

    You can then loop over $_POST['contact']:

    foreach ($_POST['contact'] as $id => $values) {
      echo $values['type'] . ' ' . $values['address'];
    }
    

    And the var_dump($_POST) with sample input would look something like:

    array(1) {
      ["contact"]=>
      array(2) {
        [123]=>
        array(2) {
          ["type"]=>
          string(5) "type1"
          ["address"]=>
          string(5) "addr1"
        }
        [124]=>
        array(2) {
          ["type"]=>
          string(5) "type2"
          ["address"]=>
          string(5) "addr2"
        }
      }
    }
    

    The method you use to generate the ids in JavaScript (assuming you don’t know them ahead of time) is entirely up to you. You could just start a variable at 1 and increment it each time you click the add more link. The value of id doesn’t really matter, as long as it is the same for the pair of inputs.

    // Initialize curId on page load.
    var curId = 1;
    
    addMoreLink.onclick = function() {
      // append the new inputs using curId
      // and then increment the value
      curId++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on coding a personal website from scratch in my free time,
I mostly use vim from the terminal by vi command. I want to create
I mostly use SlickEdit and eclipse while working on embedded systems. Once in a
I am planning to release as open-source a personal project for mostly educational (if
Mostly my entries in seeds.rb are simple, like this: User.create!( name: Peter admin: false;
I am trying to implement an authentication system using C++/QtTcpSocket for a personal project
I've been mostly working with VB.Net for over a year and just noticed this
This question is mostly an HTTP question, I am working on an iOS app,
So I started using Git together with this workflow to make a personal website
Mostly we upload things by an upload field in the HTML and uploads the

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.