3

所以这是交易:

我有一个订单页面,我使用两种形式。

第一个表单包含一个订单项目的输入数据,当我按 OK 时,我会将表单的输入数据传递给 javascript onsubmit=send_item_data(this),最后我将return false;在 send_item_data 函数中,这样它就不会被发布。

在第二个中,我想在 div 组中附加/减去以前的数据(这样我可以轻松添加或删除它们),但我想不出(或找到)将第一种形式的数据分组的解决方案在一个 div 中并将该子 div 附加到第二种形式。

最后,通过按下按钮,第二个表单将发布所有 div 的数据,我将使用 PHP 后端代码处理它。

代码主体:

<form action="" id="first_form" method="post" onsubmit="return send_item_data(this)"> 

    <div id="Select_list">
        <select blah blah>
            ---bunch of options---
        </select>
    </div>

    <div id="extras">
                ---Some extras that you can choose by a checkbox input (I generate it via PHP)---
                example:
                <input name="checkbox[<?php echo $row['column_data_from_query']?>]" type="checkbox" value="checked">
    </div>

            --->Possibly two more input fields here<---

    <input type="button" value="clear" onclick="clear_form()">
    <input type="submit" value="OK">
</form>

<form action="" id="finalize_order_form" method="post"> 
   --->This is the second form<---
   --->In here I want to put the data from the first form so i can handle them by group<---

    if I click OK three times in the first form there should be three groups here that contain each form's data
    <input type="submit" class="button" value="Finallize order"/>
</form>

我主要使用 PHP Mysql 和 Javascript(包括 AJAX,不是 jQuery)。

4

1 回答 1

1

因此,您希望将订单商品列在第二种形式中,例如结帐前的购物车。如果您为此使用 div,它们将不会与 POST 数据一起提交到服务器 - 它们将仅显示。因此,每次添加/删除项目时,您都需要遵循 Robert 的建议并将第一种形式的数据保存到数据库中(除了他的其他原因,例如不丢失客户的会话信息)。这样,当他们单击确认订单时,数据库就已经是最新的了。否则,您需要将 Confirm Order 按钮连接到 JS 函数,该函数将 div 转换回 JSON 并将其发布到服务器以存储在 DB 中。

至于从第一个表单的数据创建仅显示的 div,您的 send_item_data 函数需要遍历所有表单的输入,获取它们的值,然后将它们添加到 div,但是您希望它们显示。然后您可以将 div 插入到第二种形式中。由于您将“this”传递给函数,即表单对象本身,您可以通过以下方式获取输入:

var inputs = this.getElementsByTagName("input");
for(var i = 0; i < inputs.length; i++) {
    if(inputs[i].type == 'submit') continue; //ignore the Submit button
    var name = inputs[i].name, value = inputs[i].value;
    ---use the name and value of this input to construct a span or something to insert inside your div---
}
---now you can insert the div into the 2nd form---
于 2013-08-20T04:24:14.720 回答