使用<li>属性从表单发送数据

时间:2017-04-17 19:50:45

标签: javascript jquery html

我的表单中的数据将通过javascript插入。这些是li属性。我想将这些li属性中的数据发送到其他php页面。但那不可能吗?

html:

<div class="cd-cart">
        <div class="wrapper">
            <header>
                <h2>Winkelkar</h2>
                <span class="undo">Verwijder product. <a href="#0">Ongedaan maken</a></span>
            </header>

            <div class="body">
                <form id="formtosubmit" class="form-style-12" action="checkout.php" method="post">
                    <ul name="products">
                    <!-- products added to the cart will be inserted here using JavaScript -->
                    </ul>
                </form>
            </div>

            <footer>
                <a id="checkoutbtn" class="checkout btn" onclick="document.getElementById('formtosubmit').submit()"><em>Ga verder: €<span>0</span></em></a>
            </footer>
        </div>
    </div>

的javascript

function addProduct() {
        //var productid = document.getElementById('product').value;
        var product = products[type];
        //this is just a product placeholder
        //you should insert an item with the selected product info
        //replace productId, productName, price and url with your real product info
        var productAdded = $('<li class="product" name="product' + i + '">' +
            '<div class="product-image">' +
            '<a href="image">' +
            '<img src="images/' + product + '.jpg" alt="placeholder"></a>' +
            '</div>' +
            '<div class="product-details">' +
            '<a style="color: black;">' + product + '</a>' +
            '<span class="price">€ ' + price + '</span><br>' +
            '<div class="quantity">' +
            '<label>Aantal: ' + qty + '</label> <div class="actions">' +
            ' <a href="#0" class="delete-item" >Delete</a></div></div></div></li>');

        cartList.prepend(productAdded);

    }

1 个答案:

答案 0 :(得分:0)

您可以在这些<input type='hidden'/>代码之间创建<li>元素,并将每个产品ID或产品名称写入该<input>代码的值。这样,当您提交表单时,将捕获这些值。

EX:

function addProduct() {
        var productid = document.getElementById('product').value;
        var product = products[type];
        //this is just a product placeholder
        //you should insert an item with the selected product info
        //replace productId, productName, price and url with your real product info
        var productAdded = $('<li class="product" name="product' + i + '">' +
            '<div class="product-image">' +
            '<a href="image">' +
            '<img src="images/' + product + '.jpg" alt="placeholder"></a>' +
            '</div>' +
            '<div class="product-details">' +
            '<a style="color: black;">' + product + '</a>' +
            '<span class="price">€ ' + price + '</span><br>' +
            '<div class="quantity">' +
            '<input type="hidden" value="'+productid+'" readonly name='productid[]'>' +
            '<label>Aantal: ' + qty + '</label> <div class="actions">' +
            ' <a href="#0" class="delete-item" >Delete</a></div></div></div></li>');

        cartList.prepend(productAdded);

    }

现在您可能已经注意到输入标记中的name='productid[]'是什么?这意味着创建一个特定于该元素的每个输入值的数组。更多信息可以是found here

另一种方法是将您选择的产品ID保存到cookie /会话变量中。然后,您的服务器端可以访问该信息并相应地处理购物车。如果您要从购物车中删除产品,请确保同时更新会话/ cookie值。