Solved

How do i give an input field a dynamic ID?

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 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

22 Experts available now in Live!

Get 1:1 Help Now