Solved

How do i give an input field a dynamic ID?

Posted on 2013-05-21
3
337 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now