Solved

Selecting the specific record from the HTML table

Posted on 2008-10-07
9
257 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
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)

861 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