Solved

How do i give an input field a dynamic ID?

Posted on 2013-05-21
3
339 Views
Last Modified: 2013-05-21
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
Comment
Question by:wlwestrom
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
Jagadishwor Dulal earned 500 total points
ID: 39184796
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
 

Author Comment

by:wlwestrom
ID: 39184829
and for the javascript function?

how to i echo the php inside the javascript?


quantity = document.getElementById('quantitybox').value;
0
 

Author Closing Comment

by:wlwestrom
ID: 39185320
Thanks for pointing me in the right direction. The answer was literal staring at me in the face.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question