【问题标题】:Showing multiple products in classic asp. Using database以经典的 asp 显示多个产品。使用数据库
【发布时间】:2011-12-30 07:44:56
【问题描述】:

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

Product Id, 
Product Name, 
Image Path, 
Price

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

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

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

<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")

%>

作为回应,我得到了这个:

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

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

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

【问题讨论】:

  • 您使用产品 ID。它绝对是主键,并且会唯一标识每一个产品,对吧?
  • 如果 Product id 不是主键,则通常创建一个随机 ID,或者您可以使用自动增量值,这需要您在表中创建一个新字段。
  • 附注:您是否考虑过在每篇文章旁边放置一个复选框和一个“全局”添加到购物车按钮?
  • 问题是您使用多个值填充表单输入“productid”。您发布的代码是整页代码吗?如果不发布所有内容。

标签: asp-classic


【解决方案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 变量来存储添加的项目 - 希望意图明确。

【讨论】:

  • 谢谢大家,请看一下我原始消息中的代码..并请回复和帮助。
【解决方案2】:
<a href="cart.asp?mode=add&itemID=<%=rs("Product Id")%>">Add to Cart</a>

类似的东西,其中 rs 是您正在循环的记录集,而“cart.asp”是一个旨在管理您的购物车的页面。

【讨论】:

  • 我只有一个 ASP,我正在做所有事情。原因是,我想在左侧显示购物车项目。所以它都是一页导航的东西。我简单地这样做:-创建数据库连接-根据用户单击的链接读取表所以我在页面中有这样的条件进入该选择的代码块:我列出了我的选择代码我的页面显示 30目前的项目。
  • @user1122575 先别喊。您可以使用编辑器中的代码格式化图标发布代码。其次,您可以在一页中完成所有操作,我很快就会发布我自己的答案。
猜你喜欢
  • 2012-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多