Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do i give an input field a dynamic ID?

Posted on 2013-05-21
3
Medium Priority
?
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
Jagadishwor Dulal earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

688 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