Solved

Selecting the specific record from the HTML table

Posted on 2008-10-07
9
261 Views
Last Modified: 2012-05-05
Hi experts,

I was trying to generate the product list from the database and then display them in the tabular form using HTML tables with the Add button in front of every row. Now what I need to do is that whenever the user clicks on the Add button, the corresponding record should give me its unique value for which I am using hidden field. Here is the code snippet,

<table border="1">
<% mydb.connect();
                     rs = mydb.query("SELECT * FROM Product");
                     while (rs.next()) {
                  %>
                  <tr>
                  <!-- <td><%= rs.getString("Product_Code") %></td>  -->
                        <td><%= rs.getString("Product_Category") %></td>
                        <td><%= rs.getString("Product_Desc") %></td>
                        <td align="right"><%= rs.getString("Product_Price") %></td>
                        <td>
                              <input type="text" id="qty" name="qty" maxlength="3" size="3">
                              <input type="hidden" id="pk" name="pk" value=<%= rs.getString("Product_Code")%>>
                        </td>
                        <td>
                              <img src="images/addtocart.gif" alt="Cart" border="0" height="40" width="40" onclick="CheckQty();return">
                        </td>
                  </tr>
                  <% } %>
            </table>

0
Comment
Question by:RohitGupta2008
[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
  • 4
  • 3
  • 2
9 Comments
 
LVL 15

Accepted Solution

by:
dosth earned 500 total points
ID: 22661929
<table border="1">
<% mydb.connect();
                     rs = mydb.query("SELECT * FROM Product");
                     while (rs.next()) {
                  %>
                  <tr>
                  <!-- <td><%= rs.getString("Product_Code") %></td>  -->
                        <td><%= rs.getString("Product_Category") %></td>
                        <td><%= rs.getString("Product_Desc") %></td>
                        <td align="right"><%= rs.getString("Product_Price") %></td>
                        <td>
                              <input type="text" id="qty" name="qty" maxlength="3" size="3">
                              <input type="hidden" id="pk" name="pk" value=<%= rs.getString("Product_Code")%>>
                        </td>
                        <td>
                              <a href="" onClick="javascript:CheckQty('<%=rs.getString("Product_Code")%>');return"><img src="images/addtocart.gif" alt="Cart" border="0" height="40" width="40"></a>
                        </td>
                  </tr>
                  <% } %>
            </table>
0
 

Author Comment

by:RohitGupta2008
ID: 22662030
adding the parameter to CheckQty() function really works for me and I am able to get the value of the selected product. If you observe the code, I have textbox with every record so that user can enter the value for the qty. How can I pick that qty value of the selected product???Please help...
0
 
LVL 15

Expert Comment

by:dosth
ID: 22662207
<table border="1">
<% mydb.connect();
                     rs = mydb.query("SELECT * FROM Product");
                 idx=0
                     while (rs.next()) {
                  %>
                  <tr>
                  <!-- <td><%= rs.getString("Product_Code") %></td>  -->
                        <td><%= rs.getString("Product_Category") %></td>
                        <td><%= rs.getString("Product_Desc") %></td>
                        <td align="right"><%= rs.getString("Product_Price") %></td>
                        <td>
                              <input type="text" id="qty" name="qty" maxlength="3" size="3">
                              <input type="hidden" id="pk" name="pk" value=<%= rs.getString("Product_Code")%>>
                        </td>
                        <td>
                              <img src="images/addtocart.gif" alt="Cart" border="0" height="40" width="40" onclick="CheckQty('<%=idx%>');return">
                        </td>
                  </tr>
                  <% } %>
            </table>

on your js function, you will get the qty as array of object, get the index you passed to get the values
0
Technology Partners: 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!

 

Author Comment

by:RohitGupta2008
ID: 22662306
This is fine but still I am not getting the value of qty. Can you please provide the snippet for js on how to retreive qty as array of object.
0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 22665133
javascript --
var quantity = document.getElementById('qty').innerHTML;

or if a form value --
var quantity = document.form_name.qty.value;

the quantity is now in the javascript variable "quantity"
Remember, if using inner HTML, you have to convert to number --

var qty = ParseInt(quantity);
0
 

Author Comment

by:RohitGupta2008
ID: 22665838
Thanx scrathcyboy.
but if you check the previous comment by dosth where we need to pass the qty as argument to the js function where I can use qty as the array of object. I want help on using qty as the array of object so that I can extract the value of the qty of the selected product.
0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 22666209
my code gets it without an array.  THere is no array in your code.

"whenever the user clicks on the Add button, the corresponding record should give me its unique value for which I am using hidden field. Here is the code snippet,"

That is NOT an array, you are asking for the quantity one by one, per click.
0
 

Author Comment

by:RohitGupta2008
ID: 22666225
Sorry for not explaining my question well. Actually that was the first question which is already resolved. After resolving first one then the next problem was to get the corresponding value of qty. The solution you gave, I already did that but that wasnt working thats why i put the question here...
0
 
LVL 15

Expert Comment

by:dosth
ID: 22718649
thanks for the points
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
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)

717 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