0

I have a almost finish webshop with a product catalog page (with multiple products) and a product info/overview page (only one product).

The product catalog page holds a "read more" that will go to the product info page where there is a buy button and a qty input.

This is all working fine.

But now the customer wants buy buttons one the product catalog page (incl. qty input). This is giving me some problems - i have figured out how to add the product to cart but can't get the value of the qty input field right. it adds 6 of the product to the cart even if "productkolli" is fx. 1.

Can anyone tell me what im doing wrong?

i'm using jquery to add the products to cart.

Here my form template:

<form class="addtocartform" name="addtocartform" action="Kurv/kurv.php">
            <input type="text" class="qty" name="' . $productID . '" id="' . $productID . '" value="' . $productKolli . '"/>
    </form></div>
    <div class="addtocartbtn" id="' . $productID . '"><a href="#">add</a></div>

Here my jquery:

$('.addtocartbtn').click(function(){

        // Get product ID
        var product_id = this.id;

        // Get qty
        var qty = document.getElementById(<?php echo $productID ?>).value;

        //var qty = $(this).closest("input");
        //var qty = $('input[class=qty]').val();

        // right here, you could add the product to your shopping cart application
           performing an asynchronous HTTP request...

         $.get( "Kurv/kurv.php", { action: "add", id: product_id, qty: qty } );

2 Answers 2

1

Try this:

// Get qty
var qty = $("#"+<?php echo $productID; ?>).val();
Sign up to request clarification or add additional context in comments.

1 Comment

is this not the same as var qty = getElementById(<?php echo $productID; ?>).val(); ?
1

Try this jquery solution:

var qty = $('.addtocartform').find('input').val();

In general I will start with

alert(qty);

then you will be sure that you've got correct value.

1 Comment

This gives me 1 everytime - wouldn't i need to tell it witch of the .addtocartform to find the input in?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.