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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:36:01+00:00 2026-05-19T01:36:01+00:00

I’ll try to explain this logically as best as I can. Basically I have

  • 0

I’ll try to explain this logically as best as I can. Basically I have a facility on my site whereby users can enter their employment history. This is a form with input fields. To make things easier I have a jQuery function that clones these input fields and allows the user to enter multiple records at once.

Whilst adding new records the user may wish to remove a record from the list, in which case they can press the ‘remove’ link next to each record.

HTML (1 Item):

<div class="history-form-fields">

<div class="row">
  <label for="History_0_type">Type</label>
  <select name="History[0][type]" id="History_0_type">
  <option value="">Select</option>
  </select>
</div>

<div class="row">
  <label for="History_0_name">Name</label>
  <input type="text" name="History[0][name]" id="History_0_name" />
</div>

<div class="row">
  <label for="History_0_year">Year</label>
  <select name="History[0][year]" id="History_0_year">
  <option value="">Select</option>
  </select>
</div>

</div>

<input id="add" type="button" value="Add Another" />    

JS:

<script type="text/javascript">
    var counter = <?php echo $historyCount; ?>;

    $('#add').click(function(){
        var divCloned = $('.history-form-fields:first').clone();
        divCloned.insertAfter('.history-form-fields:last');
        initNewInputs(divCloned.children('.row'), ++counter);
    });

    $('.remove').live('click', function(){
        $(this).parent().remove();
        return false;
    });
</script>

In the above JS code, the $historyCount variable is set beforehand in my PHP script:

$historyCount=isset($_POST['History']) ? count($_POST['History'])-1 : 0;

So for example when the form is posted, if there were multiple records on the page and there are some validation errors, then the form is redisplayed showing those same records.

There is also a call to a function initNewInputs() which is passed in the new
counter value. This is so that new form input elements can receive unique ID and NAME attributes (in the HTML code above you can see the integer indexes within the attribute values).

This is all working fine. The issue I’m having is in the following scenario:

  1. User starts adding in new records

    • Record 1 – Count 0
    • Record 2 – Count 1
    • Record 3 – Count 2
    • Record 4 – Count 3
    • Record 5 – Count 4
  2. User removes Records 3 & 4 (so we now only have 3 records). Record 5 is now Record 3 but maintains the same Count value (4).

  3. User then adds in a new record:

    • Record 4 – Count 5
  4. User posts the form – there are some validation errors so the form is redisplayed. The PHP script determines that it received 4 records, so it pre-sets the count to 3.

  5. User decides to add a new record whilst fixing the errors

    • Record 5 – Count 4

This is where the problem arises – a record with the count value of 4 already exists on the page. I.e. it has now created input elements with the same ID and NAME attribute values as one that is already on the page.

I know this is a little bit difficult to understand but I would appreciate any help. It will most likely require modification of how the counter is set.

  • 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-19T01:36:02+00:00Added an answer on May 19, 2026 at 1:36 am

    Rather than using count($_POST['History']) to fill in counter use the upper bound (in this case, I mean highest index number) of the $_POST['History'] array.

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

Sidebar

Related Questions

No related questions found

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.