Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2005
    Posts
    4

    Unanswered: how to loop check boxes

    i am a nebie so bear with me.

    i have got 10 checkboxes, which are named as follows
    <td width="20"><input type="checkbox" name="p101" value="on"></td><td width="63">Something</td>
    <td width="20"><input type="checkbox" name="p102" value="on"></td><td width="63">Milk</td>

    i have got the following code to get the value.

    if request.form("p101") = "p101" then
    sql = "SELECT product.ProductName, product.ProductType, product.ProductDescription, "
    sql = sql &" Prices.Asda, Prices.Tesco, Prices.Safeway, Prices.Sainsbury"
    sql = sql &" FROM "
    sql = sql &" Prices INNER JOIN product ON Prices.ProductId = product.ProductId"
    sql = sql &" WHERE"
    sql = sql &" product.productid = 101"
    set rs=Server.CreateObject("ADODB.Recordset")
    rs.Open sql,conn
    %>

    As you can see that for each checkbox i have to harcode the same code again make all 10 check box working. So here is the question

    i can a loop statement be performed, whcih reduces the harcoded code inputted into the form and uses one select statement to check all 10 products if they are checked or not.
    Last edited by needasphelp; 02-23-05 at 09:57. Reason: not look but loop

  2. #2
    Join Date
    Dec 2004
    Location
    York, PA
    Posts
    95
    REverse your logic slightly


    <td width="20"><input type="checkbox" name="product" value="p101"></td><td width="63">Something</td>
    <td width="20"><input type="checkbox" name="product" value="p102"></td><td width="63">Milk</td>

    i have got the following code to get the value.

    if len(request.form("product")) > 0 then
    sql = "SELECT product.ProductName, product.ProductType, product.ProductDescription, "
    sql = sql &" Prices.Asda, Prices.Tesco, Prices.Safeway, Prices.Sainsbury"
    sql = sql &" FROM "
    sql = sql &" Prices INNER JOIN product ON Prices.ProductId = product.ProductId"
    sql = sql &" WHERE"
    sql = sql &" product.productid in (" & request.form("product") & ")"
    set rs=Server.CreateObject("ADODB.Recordset")
    rs.Open sql,conn
    %>
    Sorry to be terse
    some say it's a curse
    I know it's worse
    I'm just diverse

  3. #3
    Join Date
    Feb 2005
    Posts
    4
    i have modified the changes suggested in the previous reply, which i have done. Below is the code. if it is correct then why am i getting the error on line 27. one other thing what is mean by len used in if len(). The changes made shouldn't effect rest of the code should it
    -------------------------------------------------------------------
    <%
    set conn=Server.CreateObject("ADODB.Connection")
    conn.Provider="Microsoft.Jet.OLEDB.4.0"
    conn.Open(Server.Mappath("fpdb/coursework.mdb"))
    product =request.Form("product")
    %>
    <form method="post">
    <table border="1" width="194" id="table1">
    <tr>
    <td width="20"><input type="checkbox" name="product" value="101"></td><td width="63">Something</td>
    <td width="20"><input type="checkbox" name="product" value="102"></td><td width="63">Milk</td>
    <td width="20"><input type="checkbox" name="product" value="103"></td><td width="63">Milk</td>
    <td width="20"><input type="checkbox" name="product" value="104"></td><td width="63">Something</td>
    <td width="20"><input type="checkbox" name="product" value="105"></td><td width="28">Milk</td>
    <td width="20"><input type="checkbox" name="product" value="106"></td><td width="63">Something</td>
    </tr>
    </table>
    <p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
    </form>
    <%
    21) if len(request.form("product")) > 0 then
    22) sql = "SELECT product.ProductName, product.ProductType, product.ProductDescription, "
    23) sql = sql &" Prices.Asda, Prices.Tesco, Prices.Safeway, Prices.Sainsbury"
    24) sql = sql &" FROM "
    25) sql = sql &" Prices INNER JOIN product ON Prices.ProductId = product.ProductId"
    26) sql = sql &" WHERE"
    27) sql = sql &" product.productid in ("&request.form("product")&)
    28) set rs=Server.CreateObject("ADODB.Recordset")
    29) rs.Open sql,conn
    %>
    <p>&nbsp;</p>
    <table width="62%" cellspacing="0" cellpadding="2" border="1">
    <tr>
    <th width="138" style="border-style: solid; border-width: 1px">
    <font size="4">Product Name</font></th>
    <th style="border-style: solid; border-width: 1px"><font size="4">Product Description</font></th>
    <th width="55" style="border-style: solid; border-width: 1px">
    <font size="4">Asda ()</font></th>
    <th width="65" style="border-style: solid; border-width: 1px">
    <font size="4">Tesco ()</font></th>
    <th width="79" style="border-style: solid; border-width: 1px">
    <font size="4">Sainsbury ()</font></th>
    <th width="72" style="border-style: solid; border-width: 1px">
    <font size="4">Safeway ()</font></th>
    </tr>
    <%
    do until rs.EOF
    response.write("<tr>")
    response.write("<td>" & rs.fields("productname") & "</td>")
    response.write("<td>" & rs.fields("productdescription") & "</td>")
    response.write("<td>" & rs.fields("asda") & "</td>")
    response.write("<td>" & rs.fields("tesco") & "</td>")
    response.write("<td>" & rs.fields("sainsbury") & "</td>")
    response.write("<td>" & rs.fields("safeway") & "</td>")
    response.write("</tr>")
    rs.MoveNext
    loop
    rs.close
    conn.Close
    set rs=Nothing
    set conn=Nothing
    end if
    %>
    </table>
    </body>
    </html>
    ------------------------------------------------------------------------
    Error Type:
    Microsoft VBScript compilation (0x800A03EA)
    Syntax error
    /db206/project/checkproduct.asp, line 27, column 61

  4. #4
    Join Date
    Feb 2005
    Posts
    4
    cheers white night, it is working excellent, you don't know how glad i am that it is working. been on it for last 30 hours as iam a newbie

    Here is another question

    i want to have a text box for all of the products, which are for entering the productqty. the user enter the product qty for the products ticked and the results are displayed with the total of the product. how can this be done. will be grateful for any help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •