Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

How do i give an input field a dynamic ID?

Little puzzled on this one. I have a HTML table for product ordering with an input field as one of the columns representing a quantity of that product to order.

<table>
      <thead>
        <tr>
          <td class="name"><?php echo $column_name; ?></td>
          <td class="model"><?php echo $column_model; ?></td>
		  <td>Quantity</td>
          <td class="price"><?php echo $column_price; ?></td>
          <td class="action"><?php echo $column_action; ?></td>
        </tr>
      </thead>
      <?php foreach ($products as $product) { ?>
      <tbody id="wishlist-row<?php echo $product['product_id']; ?>">
        <tr>
          <td class="name"><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></td>
          <td class="model"><?php echo $product['model']; ?></td>
		<td><input type="text" name="quantitybox" id="quantitybox" size="2" value="" /></td>
          <td class="price"><?php echo $product['price']; ?></td>
          <td class="action"><input type="button" value="<?php echo $button_cart; ?>" class="button" onclick="addToCart('<?php echo $product['product_id']; ?>');" /></td>
        </tr>
      </tbody>
      <?php } ?>
    </table>

Open in new window


When i press add to cart, it uses a javascript function to pass the data via ajax to the shopping cart.

function addToCart(product_id, quantity) {
	//quantity = typeof(quantity) != 'undefined' ? quantity : 1;
	quantity = document.getElementById('quantitybox').value;
	
	$.ajax({
		url: 'index.php?route=checkout/cart/add',
		type: 'post',
		data: 'product_id=' + product_id + '&quantity=' + quantity,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information, .error').remove();
			
			if (json['redirect']) {
				location = json['redirect'];
			}
			
			if (json['success']) {
				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.success').fadeIn('slow');
				
				$('#cart-total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}

Open in new window


It works great for the first row in the table, the problem is all the input fields in the columns have the same id. If you try to set a quantity in the 2, 3, 4...etc row of the table it always uses the value from the first row's input field.

How can i dynamically set the ID of each input based on the number of rows in the table and then get the value from that field and assign it to a variable that i can pass into the function?
0
wlwestrom
Asked:
wlwestrom
  • 2
1 Solution
 
Jagadishwor DulalBraces MediaCommented:
As you set the id for  
<tbody id="wishlist-row<?php echo $product['product_id']; ?>">

Open in new window

Use same method to input like
<input type="text" name="quantitybox" id="quantitybox-<?php echo $product['product_id']; ?>" size="2" value="" />

Open in new window

0
 
wlwestromAuthor Commented:
and for the javascript function?

how to i echo the php inside the javascript?


quantity = document.getElementById('quantitybox').value;
0
 
wlwestromAuthor Commented:
Thanks for pointing me in the right direction. The answer was literal staring at me in the face.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now