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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:21:55+00:00 2026-05-25T12:21:55+00:00

I have some code for a PHP shopping cart which is as follow if($cart->itemcount

  • 0

I have some code for a PHP shopping cart which is as follow

if($cart->itemcount > 0) {
foreach($cart->get_contents() as $item) {
    echo "Code/ID :".$item['id']."<br/>";
    echo 'Quantity:<form method="post" action="cart.php"><input type="hidden" name="qtyid" id="qtyid" value="'.$item['id'].'"/><input type="submit" name="minus" id="minus" value="-"/>'.$item['qty'].'<input type="submit" name="plus" id="plus" value="+"/><br/></form>';
    echo "Price   :$".number_format($item['price'],2)."<br/>";
    echo "Info    :".$item['infos']."<br />";
    echo "Shipping :".$item['shipping']."<br />";
    echo "Subtotal :$".number_format($item['subtotal'],2)."<br />";
    echo '<form method="post" action="cart.php"><input type="hidden" name="removeid" id="removeid" value="'.$item['id'].'"/><input type="submit" name="remove" id="remove" value="Remove"/></form>';
    }
echo "---------------------<br>";
echo "total: $".number_format($cart->total,2);
} else {
echo "No items in cart";
}

I’ve been using jQuery Ajax to post the data to my php script and everything works fine when I’ve got one item:

        /********************************/
        /* Plus Item                   */
        /********************************/
        $('#plus').click(function()
            {
            var qtyid = $(this).val();
            var plus  = 'plus';

            $.ajax({
                url: 'cart.php',
                type: 'post',
                data: 'qtyid=' + qtyid + '&plus=' + plus,

                sucess: function (result) {
                    console.log('it worked');
                }

            })
            return false;
            })

        /********************************/
        /* Minus Item                   */
        /********************************/
        $('#minus').click(function()
            {
            var qtyid = $(this)).val();
            var minus  = 'minus';

            $.ajax({
                url: 'cart.php',
                type: 'post',
                data: 'qtyid=' + qtyid + '&minus=' + minus,

                sucess: function (result) {
                    console.log('it worked');
                }

            })
            return false;
            })

However, I’m having troubles when there are more than one product in the cart as, I guess, my code does not allow jQuery ajax to find where it should get the id from as the HTML generated does contain elements with the same ID.

</div>
<form method="post" action="cart.php">
    <input type="hidden" name="imgid" id="imgid" value="YWNoZZeXk2htZmJjaWds">
    <input type="hidden" name="imgalb" id="imgalb" value="YWNoZZeVnGdtZmJjamlj">
    <input type="hidden" name="qty" id="qty" value="1">
    <input type="submit" value="Add to cart" name="addtocart" id="addtocart">
</form>Code/ID :126264027457298<br/>
Quantity:
<form method="post" action="cart.php">
    <input type="hidden" name="qtyid" id="qtyid" value="126264027457298"/>
    <input type="submit" name="minus" id="minus" value="-"/>
    9
    <input type="submit" name="plus" id="plus" value="+"/>
    <br/>
</form>
Price   :$55.85
<br/>
Info    :
Titre<br />
Shipping :6.00<br />
Subtotal :$556.65<br />

<form method="post" action="cart.php">
        <input type="hidden" name="removeid" id="removeid" value="126264027457298"/>
        <input type="submit" name="remove" id="remove" value="Remove"/>
</form>Code/ID :126265084123859<br/>
Quantity:
<form method="post" action="cart.php">
    <input type="hidden" name="qtyid" id="qtyid" value="126265084123859"/>
    <input type="submit" name="minus" id="minus" value="-"/>
    2
    <input type="submit" name="plus" id="plus" value="+"/>
    <br/>
</form>
Price   :$25.85
<br/>
Info    :Boucles d'oreilles courtes «Conic»<br />
Shipping :6.00<br />
Subtotal :$63.70<br />
<form method="post" action="cart.php">
    <input type="hidden" name="removeid" id="removeid" value="126265084123859"/>
    <input type="submit" name="remove" id="remove" value="Remove"/>
</form>

---------------------<br>total: $620.35
</div>

I have been spending hours trying to find a solution. But I feel it’s time to ask for help.
What should I do? How should I alter my HTML to generate unique IDs and find a way to get the ids from jQuery and PHP?

  • 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-25T12:21:55+00:00Added an answer on May 25, 2026 at 12:21 pm

    It seems that you just need to get the qtyid value associated with the plus/minus buttons you click. If that’s the case, you should be able to get it using the following line:

    var qtyid = $('#qtyid', $(this).parent('form')).val();
    

    So an example of one of your click events would be:

    $('#plus').click(function()
    {
        var qtyid = $('#qtyid', $(this).parent('form')).val();
        var plus  = 'plus';
    
        $.ajax({
            url: 'cart.php',
            type: 'post',
            data: 'qtyid=' + qtyid + '&plus=' + plus,
            success: function (result) {
                console.log('it worked');
            }
        });
    
        return false;
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some PHP code which allows me to sort a column into ascending
I have some PHP code which stores whatever is typed in a textbox in
I have some code I've written in PHP for consuming our simple webservice, which
I have some PHP code which looks roughly like this require_once 'cache.php'; require_once 'generator.php';
I'm have some php code: <? $cmd=mkfifo /tmp/myfifo;; system($cmd); $cmd=echo 1 > /tmp/myfifo 2>&1
i need to have some php code inside javascript <script ...> <?php echo ...
I have some PHP code which runs a loop, spitting out a button and
I have some PHP code that generates dynamic tables of data on the fly.
I have some PHP code that is designed to make a spreadsheet with formulas
I have some PHP code that generates a calendar and then outputs html to

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.