在经典的asp中展示多种产品。使用数据库

时间:2011-12-30 07:44:57

标签: asp-classic

我有一个这样的访问表:有100个产品

Product Id, 
Product Name, 
Image Path, 
Price

我可以使用asp代码轻松地在我的网页上显示这些产品。

我想在每个产品下添加一个按钮,允许用户将产品添加到购物车。

我的页面一次显示30个项目。这是我的页面的HTML。当我点击任何按钮时,我调用下面提到的java sctipt函数来提交表单。

<table class="retail">
        <tr>
            <td>
                <img src="images/<%=rs.fields("image")%>" width="125" height="125"/>
            <td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr>
            <td style="font-weight: bold;color: #151B8D;text-align: center;font-size: 10px; padding: 5px 5px 5px 5px;">
                <%=rs.fields("details")%>
            <td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr>
            <td style="font-weight: bold;color: red;text-align: center;font-size: 14px; padding: 0px 1px 1px 1px;">
                <%=rs.fields("productid")%>
            <td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr>
            <td style="font-weight: bold;color: red;text-align: center;font-size: 16px; padding: 0px 1px 1px 1px;">
                USD $<%=rs.fields("price")%>.00
            </td>
            <input type="hidden" id="details" name="details"/>
            <input type="hidden" id="productid" name="productid"/>
            <input type="hidden" id="price" name="price"/>
        </tr>
        <tr>
        <td>
         <input type="button" value="Add to Trolly" id="cmdAdd" name="cmdAdd" onClick="javascript:callMe(this,'<%=rs.fields("details")%>','<%=rs.fields("productid")%>','<%=rs.fields("price")%>')"/>
        </td>
        </tr>
    </table>




<SCRIPT LANGUAGE="JavaScript">
function callMe(obj,details,prod_id,price) {
    var prodnm = document.getElementById("productid");
    prodnm.value = prod_id;

    var itemprice = document.getElementById("price");
    itemprice.value = price;

    var trollyform = document.getElementById("trollyform");

    document.forms["trollyform"].submit();
}

这是我编写的用于检查表单子目标值的代码:

<%

Response.Write "<br>Product Code value : " & request.Form("productid")
Response.Write "<br>Price value : " & request.Form("price")

%GT;

作为回应我得到了这个:

我的页面显示30个项目,如果我点击任何添加到购物车按钮,它会给我这个。所以我点击的按钮,它给了我这个值,但它也给了我逗号分隔值的休息

产品代码值:RNG-2 ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 价格值:10 ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

基本上,第一个值是我在该页面上为其余产品点击产品的按钮值,它给了我COMMA

2 个答案:

答案 0 :(得分:1)

<a href="cart.asp?mode=add&itemID=<%=rs("Product Id")%>">Add to Cart</a>

类似于你正在循环的记录集,而“cart.asp”是一个旨在管理购物车的页面。

答案 1 :(得分:1)

如果您担心,可以在一个页面中完成所有操作。代码posted by cavillac是一个好的开始,而不是指向不同的页面只指向同一页面,但仍然有查询字符串值来指示要添加的产品:

<a href="<%=Request.ServerVariables("SCRIPT_NAME")%>?mode=add&itemID=<%=rs("Product Id")%>">Add to Cart</a>

现在在你的页面中,有这样的代码块:

If Request.QueryString("mode")="add" Then
    productID = Request.QueryString("itemID")

    If IsNumeric(productID) Then
        arrCurrentCart = Session("UserCart")
        If IsArray(arrCurrentCart) Then
            ReDim Preserve arrCurrentCart(UBound(arrCurrentCart) + 1)
        Else
            ReDim arrCurrentCart(0)
        End If
        arrCurrentCart(UBound(arrCurrentCart)) = productID
        Session("UserCart") = arrCurrentCart

        Response.Write("item added to cart")
        Response.END
    End If
End If

这是一个非常基本的例子,使用普通数组作为Session变量来存储添加的项目 - 希望意图明确。

相关问题