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

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

问题描述

道歉,如果我不清楚问题的清晰或问题中的措辞含糊不清。



我目前正在为购物车编码-commerce网站和我一直在使用教程/谷歌来帮助我进入现阶段,但现在我被卡住了。



我想做的是做它这样当用户更新各个单独项目的数量时,只需点击一个按钮就可以保存它们(价格会相应地改变)。现在,他们保存并计算,但每个按钮项目都有自己的更改数量按钮,而我想要的只是更新购物车按钮。



目前,以下PHP块用于检查数量是多少并修改数量/价格:



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/google to help get me to my current stage but now I am stuck.

What I am trying to do is make it so that when the user updates the quantities of various individual items, they are saved (the price then changes accordingly) at the click of one button. Right now, they save and calculate, but each button item has it''s own "Change Quantity" button, whereas what I want is to just have on "Update Cart" button.

Currently, the following PHP block is used to check what the quantity is and to amend the quantity/price:

<?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
}
?>





此块呈现实际的购物车,然后在之后回显:





This block renders the actual cart which is then echo''d later:

<?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验证有关。



具有更改按钮的特定行是:





there is a bit more code at the end but it has to do with paypal verification and such.

The specific line which has the ''Change'' button is:

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





和更新购物车按钮目前是:





and the Update Cart button is currently this:

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





任何关于我出错或需要调整什么的帮助或建议都将不胜感激!



Any help or advice on where I''m going wrong or what needs to be tweaked would be much appreciated!

推荐答案

_POST [' item_to_adjust'] )&&&
_POST['item_to_adjust']) &&


_POST [' item_to_adjust'] != ){
// 执行一些代码
_POST['item_to_adjust'] != "") { // execute some code


item_to_adjust =
item_to_adjust =


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

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