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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:53:22+00:00 2026-05-16T20:53:22+00:00

I am trying to create a JSON object from a bunch of grouped HTML

  • 0

I am trying to create a JSON object from a bunch of grouped HTML elements, this is my markup,

HTML:

<div id="locations_wrapper">
    <div id="location_0" class="locations">
        <div class="container">
          <label for="locations_province">Province: </label>
          <div>
            <select id="locations_province" name="locations_province" onchange="get_cities(this);">
              <option value="">Select one</option>
              <option>Eastern Cape</option>
              <option>Freestate</option>
              <option>Gauteng</option>
              <option>KZN</option>
              <option>Limpopo</option>
              <option>Mpumalanga</option>
              <option>North West</option>
              <option>Northern Cape</option>
              <option>Western Cape</option>
            </select>
          </div>
        </div>
        <!-- City -->
        <div class="container">
          <label for="locations_city">City: </label>
          <div>
            <select id="locations_city" name="locations_city">
              <option value="">Select one</option>
            </select>
          </div>
        </div>
        <!-- Towns -->
        <div class="container">
          <label for="locations_towns">Towns: </label>
          <div>
            <input type="text" name="locations_towns" id="locations_towns" />
          </div>
        </div>
    </div>
</div>

At the moment I am cloning these 3 fields and appending them to the parent div locations_wrapper, I also increment the id attribute of each field, the problem now is that I need to get all of the cloned elements and somehow, using jQuery/ajax, capture all of the data of each location into a database.

How could I go about getting that information?

here is some jQuery I wrote that basically does the same thing, but with one fied instead of 3:

    var sections = $('#systems_wrapper').find('.dropDowns');
    var newArray = new Array();

        sections.each(function(){
            var id = $(this).attr('id');
            var val = $(this).val();
            var o = { 'id': id, 'value': val };

            newArray.push(o);
        });

        $.ajax({
                type: 'POST',
                url: 'qwer.php',
                dataType: 'json',
                data: { json: JSON.stringify(newArray) }
        });

Maybe I could try creating a JSON object with 3 fields province, city and town? I am a little unsure.

Thanx in advance!

  • 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-16T20:53:22+00:00Added an answer on May 16, 2026 at 8:53 pm

    It’s not very clear, from your question, what you’re trying to accomplish. But I’m guessing, from your markup, that you’re building something that will allow the user to add multiple “locations” within the “locations_wrapper” object. This sort of thing usually starts with markup like you’ve shown, providing a single blank “location” form for the user to fill in (3-fields, in this case); and a button that they can click to add additional “location” blocks as needed.

    Assuming that this is what you’re doing, my first suggestion would be that, instead of cloning the three form fields individually, you should be cloning only their parent container, “location_0”. Something like this should do the trick:

    $('#location_0').clone().appendTo('#locations_wrapper');
    

    That will copy the entire block — and you don’t really need to worry about changing the ids of things, jQuery and/or the browser will take care of ensuring that everything has a unique id behind-the-scenes.

    Now, assuming that your markup is located within a form, the easiest way to get the information back up to the server is just to submit the form. The form post will contain multiple fields with the same name — which is completely valid, and most server-side languages have a simple way of dealing with it.

    In php, multi-value request parameters come in as an array. So in php, you could do something like this:

    <?php
    $field_names = ['locations_province', 'locations_city', 'locations_town'];
    
    $locations = array();
    foreach( $field_names as $fieldname ) {
        $temp = (array)$_REQUEST[$fieldname];
        for( $i=0; $i<count($temp); ++$i ) {
            if( !isset($locations[$i]) ) $locations[$i] = array();
            $locations[$i][$fieldname] = $temp[$i];
        }
    }
    

    If you’d rather aggregate it all in javascript, you can access your field groups like this:

    var allLocations = [];
    $(".locations").each( function(i, location) {
        // for each location block
        location = $(location);
        var loc = {
            'locations_province' : $("select[name='locations_province']", location).val(),
            'locations_city' : $("select[name='locations_city']", location).val(),
            'locations_towns' : $("input[name='locations_towns']", location).val()
        };
        allLocations.push( loc );
    });
    
    // allLocations will contain a list of objects with properties representing 
    // your locations.  at this point, you can do whatever you want with 'em 
    // send them up via Ajax, or whatever.
    

    good luck!

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

Sidebar

Related Questions

I'm trying to read data from a service (returns JSON object) and create an
I'm trying to create a json object from MySQL results, but not getting the
I'm trying to create a JSON object that looks like this: { request_type:send_string security_level:0
Whats wrong in below JSON Object definition I am trying to create a new
I'm trying to loop through a number of items, and create a json object.
I am trying to create a json file from a rake task using rabl.
I have a JSON object from the CrunchBase API giving me a bunch of
I'm trying to create a JSON object on form submit to pass to Perl.
I'm trying to write/create a JSON file from a AIR app, I'm trying not
I'm trying to access data within a JSON object I create using json_decode (based

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.