如何用一个按钮更新各种输入字段? [英] How can I update various input fields with one button?

查看:51
本文介绍了如何用一个按钮更新各种输入字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果我没有清楚地问我的问题,或者问题的措词不明确.

Apologies if I don't ask my question clear enough or the wording is ambiguous in the question.

我目前正在为电子商务网站编码购物车,并且我一直在使用教程来帮助我进入目前的阶段,但是现在我陷入了困境.

I am currently coding a shopping cart for an e-commerce website and I have been using tutorials to help get me to my current stage but now I am stuck.

以下是允许客户更改数量的特定行以及旁边的更改"按钮.

The following is the specific line which allows customers to change the quantity and the 'change' button that comes up next to it.

<form action="cart.php" method="post">
<input name="quantity" type="text"  class="form-element text-element form-element-small" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
<input name="adjustBtn' . $item_id . '" type="submit" value="change" />
<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
</form> 

上面的代码是$ cartOutput的一部分-然后在DIV中回显.

The above code is part of $cartOutput - which is then echo'd in the the DIV later on.

以下是我尝试创建的用于更新所有数量的按钮,但现在所做的只是刷新页面.

The following is the button I tried to create to update all quantities but right now all it does is refresh the page.

<form action="cart.php" method="post">
<input class="btn sub-btn" name="adjustBtn' . $item_id . '" type="submit" value="Update">Update Cart</input>
<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
</form>

从字面上看,对此的任何帮助都将很棒!

Literally, any help on this would be great!

这是第一段代码来自的完整php块:

Here is the complete php block that the first piece of code came from:

<?php 

$cartOutput = "";
$minicartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$product_id_array = '';
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
    $minicartOutput = "<p>You have no items in your cart</p>";
} else {
    // Start PayPal Checkout Button
    $pp_checkout_btn .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_cart">
    <input type="hidden" name="upload" value="1">
    <input type="hidden" name="xxxxxxxxx" value="xxxxxxxxxx">';
    // Start the For Each loop
    $i = 0; 
    foreach ($_SESSION["cart_array"] as $each_item) { 
        $item_id = $each_item['item_id'];
        $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
        while ($row = mysql_fetch_array($sql)) {
            $product_name = $row["product_name"];
            $price = $row["price"];
            $shade = $row["shade"];
            $details = $row["details"];
        }
        $pricetotal = $price * $each_item['quantity'];
        $cartTotal = $pricetotal + $cartTotal;
        setlocale(LC_MONETARY, "en_US");
        $pricetotal = money_format("%10.2n", $pricetotal);
        // Dynamic Checkout Btn Assembly
        $x = $i + 1;
        $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
        <input type="hidden" name="amount_' . $x . '" value="' . $price . '">
        <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '">  ';
        // Create the product array variable
        $product_id_array .= "$item_id-".$each_item['quantity'].","; 
        // Dynamic table row assembly
        $minicartOutput .= '<div class="form shopping-cart-form">

                                                        <form id="update_cart_form" method="post" action="/cart.php"  >



                                <ul class="listing cart-ul quick-cart-ul">
                                                            <li class="listing-li cart-li quick-cart-li first-li" data-quantity="1" data-id="149"> 
                                    <div class="box cart-item-box"> 
                                        <fieldset class="form-data-group">
                                            <legend class="form-legend">' . $product_name . '</legend>
                                            <dl class="meta-listing cart-item-dl">
                                                <dt class="meta-listing-dt cart-item-dt">Quantity</dt>
                                                <dd class="meta-listing-dd cart-item-dd">' . $each_item['quantity'] . '</dd>
                                                <dt class="meta-listing-dt cart-item-dt quick-cart-item-dt-price hide-me"><!--Price--></dt>
                                                <dd class="meta-listing-dd cart-item-dd quick-cart-item-dd-price">' . $pricetotal . '</dd>
                                            </dl>
                                            <div class="related cart-item-related">
                                                <div class="media img-media cart-item-img-media">
                                                    <figure>

                                                        <img width="56" height="56" src="../uploads/inventory/' . $item_id . '.jpg" alt="' . $product_name . '" />                                                      
                                                                                                            </figure>
                                                </div>
                                            </div>
                                        </fieldset>
                                    </div>
                                </li>


                                                        </ul>
                                                        </form>';
        $cartOutput .= '<div class="form shopping-cart-form">
                                <form id="update_cart_form" method="post" action="/cart.php"  >


                            <ul class="listing cart-ul">
                                                            <li class="listing-li cart-li first-li">
                                    <div class="box cart-item-box"> 
                                        <fieldset class="form-data-group">
                                            <legend class="form-legend">' . $product_name . '</legend>
                                            <dl class="meta-listing cart-item-dl cart-item-dl-price">
                                                <dt class="meta-listing-dt cart-item-dt">Price</dt>
                                                <dd class="meta-listing-dd cart-item-dd cart-item-dd-price">' . $pricetotal . '</dd>
                                            </dl>
                                            <dl class="meta-listing cart-item-dl">
                                                <dt class="meta-listing-dt cart-item-dt">Shade</dt>
                                                <dd class="meta-listing-dd cart-item-dd">' . $shade . '</dd>
                                                <dt class="meta-listing-dt cart-item-dt">Quantity</dt>
                                                <dd class="meta-listing-dd cart-item-dd">
                                                     <form action="cart.php" method="post">
                                                        <input name="quantity" type="text"  class="form-element text-element form-element-small" value="' . $each_item['quantity'] . '" size="1" maxlength="2" /><input name="adjustBtn' . $item_id . '" type="submit" value="change" /><input name="item_to_adjust" type="hidden" value="' . $item_id . '" /></form> 
                                                </dd>
                                            </dl>
                                                <form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" class="form-label remove-from-cart-form-label delete-from-cart" type="submit" value="Remove From Cart" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form>
                                            <div class="related cart-item-related">
                                                <div class="media img-media cart-item-img-media">
                                                    <figure>

                                                           <img width="56" height="56" src="../uploads/inventory/' . $item_id . '.jpg" alt="' . $product_name . '" />

                                                                                                            </figure>
                                                </div>
                                            </div>
                                        </fieldset>
                                    </div>
                                </li>                               
                                                            </ul>

                                </form>';
        $i++; 
    } 

?>

(还有很多,但与Paypal Check out有关)

(There is a bit more but that has to do with Paypal Check out)

这应该是最终的编辑...这是处理数量调整的php块.

This should be the final edit... This is the php block that deals with adjusting quantities.

<?php 
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
    // execute some code
    $item_to_adjust = $_POST['item_to_adjust'];
    $quantity = $_POST['quantity'];
    $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
    if ($quantity >= 100) { $quantity = 99; }
    if ($quantity < 1) { $quantity = 1; }
    if ($quantity == "") { $quantity = 1; }
    $i = 0;
    foreach ($_SESSION["cart_array"] as $each_item) { 
              $i++;
              while (list($key, $value) = each($each_item)) {
                  if ($key == "item_id" && $value == $item_to_adjust) {
                      // That item is in cart already so let's adjust its quantity using array_splice()
                      array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
                  } // close if condition
              } // close while loop
    } // close foreach loop
}
?>

推荐答案

更新多个项目并不取决于按钮,而是取决于保留值的输入字段.您应为每个项目使用一种类型的字段,即,如果您需要字段"item_to_adjust"和"quantity",则每个项目都应具有这些字段.
查看您的表格,我认为这可以做到:

updating multiple items does not depend on the button, but on the input-fields that keep the values. You should use one field of a type for each item, i.e. if you need the fields "item_to_adjust" and "quantity", each item should have these fields.
Looking at your form, I think this will do:

<form action="cart.php" method="post">
<?php
// loop through your items/item-ids/whatever you use
foreach($cart_items as $item) {
    $item_id = $item['id']
    echo '
    <input name="quantity[]" type="text"  class="form-element text-element form-element-small" value="' . $item['quantity'] . '" size="1" maxlength="2" />
    <input name="item_to_adjust[]" type="hidden" value="' . $item_id . '" />
} ?>
<input name="adjustBtn" type="submit" value="change" />
</form>

您将获得:

<?php
    $_POST['item_to_adjust'] = array([0] => 127, [1] => 6, [...]); // item-ids
    $_POST['quantity'] = array([0] => 1, [1] => 15, [...]); // quantities

将item-id放入数量,您将拥有所有物品,并可以使用代码来调整数量:

Put item-ids to quantites, and you have all items and can adjust the quantities with your code:

<?php
// $cart_items_ne will be: array('127' => 1, '6' => 15, [...]);
$cart_items_new = array_combine($_POST['item_to_adjust'],$_POST['quantity']);

// update items
foreach($cart_items_new as $item_id => $quantity) {
    // do stuff here
}

您搜索的答案吗?

这篇关于如何用一个按钮更新各种输入字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆