How do I get a quantity of two input fields?

Hello !
How can I send the data from these two fields and print a datute in the following way?

// from here i need to take product id
<input type="checkbox" name="qty['.$getColorTitle['uniqueID'].']" class="css-checkbox pColor" value="" id="cb-'.$getColorTitle['uniqueID'].'" />

<label for="cb-'.$getColorTitle['uniqueID'].'">
	<img src="'.$colorFolder.''.$rowColors['image'].'" height="50px" />
	<div class="colorsC">
		// from here i need to take quantity
		<input type="hidden" name="qty['.$getColorTitle['uniqueID'].']" value="0" style="width: 45px;" class="qtyC" />
	</div>
</label>

if($_SERVER['REQUEST_METHOD'] == 'POST') {
switch($_POST['action']) {
    case 'C':
        foreach($_POST['qty'] as $color_id=>$quantity) {
            if($quantity > 0) {
                $_SESSION['cart'][$_POST['product_id']][$color_id] = $quantity;
            } else {
                unset($_SESSION['cart'][$_POST['product_id']][$color_id]);
            }
        }
    break;
}

}

The way to print:
Color 1, Quantity 6
Color 2, Quantity 1
Color 3, Quantity 1
Color 4, Quantity 1

There may be another method, but I personally took a look at this, because by choosing a color, the jQuery field opens the quantity field. If it is not selected, it is hidden.
Thanks !

I’m assuming you are asking this because if you uncheck a checkbox, you want to ignore the submitted quantity? The checkbox field name needs to be different from the quantity field. You would then test the checkbox state that corresponds to the current quantity field when looping over the the foreach($_POST[‘qty’] … data and add a conditional test to the if($quantity > 0) { … logic.

1 Like

Hi, phdr!
I try it this way:
When checked, the box appears and the value is entered. When the check mark is removed, it is hidden and its value is zeroed. (zeroing is a bit redundant, because it is not sent anywhere when not tagged). The thing I can not do is write the quantity from the field itself. You did it perfectly, but I send the data from two fields.

I did it. I just had to take the quantities from the second field correctly.
Thanks phdr !

Sponsor our Newsletter | Privacy Policy | Terms of Service