Solved

Selecting the specific record from the HTML table

Posted on 2008-10-07
9
254 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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
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 define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…

744 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

11 Experts available now in Live!

Get 1:1 Help Now