8 Replies - 1730 Views - Last Post: 03 April 2013 - 11:18 PM Rate Topic: -----

#1 JioFreed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-March 13

How to update multiple input fields using one button (PHP)

Posted 28 March 2013 - 08:05 AM

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++; 
    } 


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!

Is This A Good Question/Topic? 0
  • +

Replies To: How to update multiple input fields using one button (PHP)

#2 huzi8t9  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 326
  • Joined: 11-July 07

Re: How to update multiple input fields using one button (PHP)

Posted 02 April 2013 - 01:11 PM

Hello, JioFreed, and welcome to Dream.In.Code.

To look at this in the easiest perspective, would it not be possible to simply use a number stepper and then the "Update Cart" button reload the same page (<form method="POST"> will just resend the form with new variables), and then update accordingly?

I hope that's some help

~huzi
Was This Post Helpful? 0
  • +
  • -

#3 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: How to update multiple input fields using one button (PHP)

Posted 02 April 2013 - 02:28 PM

otherwise you could create a simple Javascript-Method, that loops trough all existing buttons and changes their value... I would not do it in PHP. With JS you can update things, without reloading the page.
Was This Post Helpful? 0
  • +
  • -

#4 huzi8t9  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 326
  • Joined: 11-July 07

Re: How to update multiple input fields using one button (PHP)

Posted 02 April 2013 - 03:27 PM

View PostAnthonidas, on 02 April 2013 - 10:28 PM, said:

otherwise you could create a simple Javascript-Method, that loops trough all existing buttons and changes their value... I would not do it in PHP. With JS you can update things, without reloading the page.


A sample would be (in jQuery)

$(function()
{
     $("A").each(function() {
     {
          var src = $(this).attr("href");
          $(this).attr("href", src + "?affid=<?php echo $_GET['affid']; ?>");
     }
});



Although a back-up should be in place since not every user has Javascript enabled; either way, a developer should always prepare for the worst scenario - it's the best way, that way, you're covered from every angle.

~huzi
Was This Post Helpful? 0
  • +
  • -

#5 JioFreed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-March 13

Re: How to update multiple input fields using one button (PHP)

Posted 03 April 2013 - 07:36 AM

Thank you guys, this was more or less what I was looking for!
Was This Post Helpful? 0
  • +
  • -

#6 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: How to update multiple input fields using one button (PHP)

Posted 03 April 2013 - 08:21 AM

I'm happy we could help.
Perhaps consider giving some rep. (the green button with the "+" sign) for helpful posts. ;)
Was This Post Helpful? 1
  • +
  • -

#7 JioFreed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-March 13

Re: How to update multiple input fields using one button (PHP)

Posted 03 April 2013 - 08:23 AM

Hey Anthonidas, I tried to, but all that happens is the page reloads without adding any rep :/
Was This Post Helpful? 0
  • +
  • -

#8 huzi8t9  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 326
  • Joined: 11-July 07

Re: How to update multiple input fields using one button (PHP)

Posted 03 April 2013 - 09:09 AM

View PostAnthonidas, on 03 April 2013 - 04:21 PM, said:

I'm happy we could help.
Perhaps consider giving some rep. (the green button with the "+" sign) for helpful posts. ;)/>


I can confirm, I'm also unable to give any rep. :/
Was This Post Helpful? 0
  • +
  • -

#9 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: How to update multiple input fields using one button (PHP)

Posted 03 April 2013 - 11:18 PM

It might be due to addons installed. I have the same problem on Chrome and Firefox, but it works on IE. (I've never used IE, so I do not have any addons installed there...)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1