Solution 1 :

You need to create a function and then assign value of qty1 to qty1send using onclick function for submit button. The following code need to be incorporated into your code. Hope it helps 🙂

HTML Code:
    <button type="submit" onclick="myFunction()"> Add to cart </button>
JS Code:
    function myFunction() {
      document.getElementById("qty1send").value = document.getElementById("qty1").value;
    }

The other option is to use the following statement in both of your increase and decrease functions as mentioned by @Rob Moll

document.getElementById("qty1send").value = document.getElementById("qty1").value;

Solution 2 :

You could add this to each function:

document.getElementById("qty1send").value = document.getElementById(field).value;

So your js would look like:

// Quantity spin buttons
function increase_by_one(field) {
    nr = parseInt(document.getElementById(field).value);
    document.getElementById(field).value = nr + 1;
    document.getElementById("qty1send").value = document.getElementById(field).value;
}

function decrease_by_one(field) {
    nr = parseInt(document.getElementById(field).value);
    if (nr > 0) {
        if ((nr - 1) > 0) {
            document.getElementById(field).value = nr - 1;
            document.getElementById("qty1send").value = document.getElementById(field).value;
        }
    }
}

Honestly though, that would just add more bad code to this unusual approach. No disrespect intended, but you should look at some examples and maybe start over with this. Intended as constructive criticism.

Problem :

I want to assign the value of input id qty1 to input id qty1send and then send it via POST method when the button Add to cart is pressed. How do I do it? Help!!

<button class="qtyBtn" onclick="increase_by_one('qty1');">+</button>
        <input id="qty1" type="text" value="1" name="J1" />                          
    <button class="qtyBtn" onclick="decrease_by_one('qty1');" />-</button>

    <form action="somehwere.php" method="POST"> 
        <input id="qty1send" type="hidden" name="qty1" value="" > 
        <button type="submit"> Add to cart </button> 
    </form>

my JS code

// Quantity spin buttons
function increase_by_one(field) {
    nr = parseInt(document.getElementById(field).value);
    document.getElementById(field).value = nr + 1;
}

function decrease_by_one(field) {
    nr = parseInt(document.getElementById(field).value);
    if (nr > 0) {
        if ((nr - 1) > 0) {
            document.getElementById(field).value = nr - 1;
        }
    }
}

Comments

Comment posted by El_Vanja

I don’t understand why the actual input is not part of the form.

Comment posted by Funk Forty Niner

How is this a “php” question?

Comment posted by M.B

i was trying to do something like this, thanks, i have a clear picture now

Comment posted by M.B

I get your point. All I am trying to do is send the value of the quantity input without including that input in the form and sending it through another input variable. So i came up with this. I will take your suggestion into consideration thanks

By