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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:15:54+00:00 2026-05-27T20:15:54+00:00

This script extracts data from check boxes in order to determine the information a

  • 0

This script extracts data from check boxes in order to determine the information a form sends to PayPal. PayPal only accepts a cart without any gaps; however, this script counts unchecked boxes in the index which makes the cart invalid (“item_name_1”, “item_name_3”, “item_name_7”). How do I make it so that the script generates a gapless succession of numbers (“item_name_1”, “item_name_2”, “item_name_3”)?

function updateCart(form) {
    var cart = "";
    var P = $('#P');
    for (var i = 0; i < document.getElementById('sList').P.length; i++) {
        if (document.getElementById('sList').P[i].checked)
        cart += '<input type="hidden" name="item_name_' + (i+1) + '" value="' + document.getElementById('sList').P[i].value.substring(6) + '"><input type="hidden" name="amount_' + (i+1) + '" value="' + document.getElementById('sList').P[i].value.substring(0, [5]) + '">'
    }
    if (cart == "") {
        alert("Please select products")
    } else alert(cart);
    $('#cart_items').html("" + cart);
    return false;
}
  • 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-27T20:15:54+00:00Added an answer on May 27, 2026 at 8:15 pm

    Your code has a few issues aside from the numbering:

    The function has a form parameter that is never used.

    document.getElementById() returns either a single DOM element or null. Your P variable is a jQuery object that, given you are selecting by id, should hold one element or none – given you are trying to use P with an index you seem to think it could have multiple matching elements but that would only be if you’ve got multiple elements with the same id which is invalid html (which will give unreliable results – so if so you should fix it: you can elements with the same name, but not the same id).

    Either way it doesn’t make any sense to try to use P as a property of the DOM element returned by .getElementById(). So everywhere that you said document.getElementById('').P.something is wrong.

    As far as generating unique numbering for the hidden inputs, you just need one variable for the loop counter, i, and then a second (new) variable for the input counting, let’s call it n. Increment n only within the if checked statement. Or if you loop with jQuery you don’t need i, just n. If you show your HTML I could update this properly rather than guessing, but something like this:

    function updateCart(form) {
        // first remove any hidden inputs from a previous unsuccessful submit
        // (this may be optional depending on whether you're submitting with ajax
        // and/or potentially aborting the submit for other reasons, e.g., if you're
        // submitting with ajax and the web server might return an error you need to
        // remove the previous hiddens if the user tries again)
        $('#cart_items input[type="hidden"]').remove();
    
        // now process each checkbox and add numbered elements for each checked one
        var n = 0,
            $f = $("#cart_items");
    
        // you may need to vary the selector here because
        // I don't know the names of your elements
        $('input[type="checkbox"]').each(function() {
           if (this.checked) {
               // following is a tidied up version of your input creation code,
               // which assumes that the value attribute of the checkbox holds
               // the item name and price information.
               n++;
               $f.append('<input type="hidden" name="item_name' + n +
                         '" value="' + this.value.substring(6) + '">');
               $f.append('<input type="hidden" name="amount_' + n +
                         '" value="' + this.value.substring(0,5) + '">');
           }
        });
        if (n === 0) {
            // no hiddens were added
            alert("Please select products");
            return false;
        } else {
            // success, inputs added above
            return true; // or submit or whatever
        }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this script: select name,create_date,modify_date from sys.procedures order by modify_date desc I can
I'm currently working on a script which extracts data from two sources where one
My jQuery code (using ajax) request's data from a local php script (pgiproxy.php). This
This is my pagination script which extracts info for my TV guide project that
I have tried using a example file using PHPExcel script to extract data from
I have a PHP script that extract valid GeoJSON data from a database. Now
I have a data flow task that extracts data from one database and inserts
I have a script to extract certain data from a much bigger table, with
I am trying to create a BASH script what would extract the data from
I'm writing a bash script to get data from procmail and produce an output

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.