Hi all

I am working a on a shopping cart which needs to allow users to choose if they want to gift wrap an item or not before paying for it.

I have so far got the code below to update the 'giftwrap' value of my session array to '1' when the user ticks a checkbox to say 'yes, please gift wrap my item'. However, I am stuck as I need to be able to allow the user to un-tick the checkbox and update the session array to be '0'.

Can anyone help me out please and maybe alter my function below to allow this?

Webpage displaying the Basket has:

foreach ($_SESSION['cart'] as $k => $v) {

   // find out if the session array key 'giftwrap' is set to 1 or 0...
   if ($v['giftwrap'] == 0) {
       $checkedvalue = "";
   } else {
       $checkedvalue = "checked";
   }

   // display the product data the user wishes to purchase and also display the gift wrap checkbox as such...
    <input type=\"checkbox\" name=\"giftwrap[]\" value=\"$k\" $checkedvalue />

}

And the function i call once the page is posted is:

if ($_POST['somefield']) {
  GiftWrapItem($_POST['giftwrap']);
}

function GiftWrapItem($selecteditems) {

foreach ($selecteditems as $productid) {
	$_SESSION['cart'][$productid]['giftwrap'] = 1; 
}

}

Your help will be greatly appreciated. I simply need to allow the session array to updated back to '0'.
Thankyou

    kbc1 wrote:

    Hi all

    I am working a on a shopping cart which needs to allow users to choose if they want to gift wrap an item or not before paying for it.

    I have so far got the code below to update the 'giftwrap' value of my session array to '1' when the user ticks a checkbox to say 'yes, please gift wrap my item'. However, I am stuck as I need to be able to allow the user to un-tick the checkbox and update the session array to be '0'.

    Can anyone help me out please and maybe alter my function below to allow this?

    You are dealing with a client server interatction.

    Once you have filled out a form on the client it is sent to the server, the form is analyzed. At that point if you want to change your choices in the form, you are out of luck until the server replies to the contents of your form.

    Unless you have programed the server to recognize that you might nor really want to accept the choice selected in the checlbox, you are out of luck!

      If a user chooses to tick it, then I do want to accept that choice. The page is posted, the session array key called 'giftwrap' is updated to '1' as my code explains.

      If the user then decides not to apply gift wrap to that item, i would like them to un-tick it, click the update button which posts the page back to the server and updte the session array key called 'giftwrap' to be '0'.

      Are you saying that this is impossible to achieve?

        Its the fact that as i understand it, a checkbox only gets posted when it is checked, so I am not clear on how can detect it has been unchecked and then update the session variable.

          How about if i remove the checkbox feature and use a list box?

          foreach ($_SESSION['cart'] as $k => $v) { 
          
             // find out if the session array key 'giftwrap' is set to 1 or 0... 
             if ($v['giftwrap'] == 0) { 
                 $checkedvalue = ""; 
             } else { 
                 $checkedvalue = "checked"; 
             } 
          
             // display the product data the user wishes to purchase and also display the gift wrap checkbox as such... 
              <select size=\"1\" name=\"giftwrap[]\">
          															 <option value=\"0\">No</option> 
          															 <option value=\"1\">Yes</option> 
          													     </select> 
          
          }

          How would I update the value in the session array this way?

          If anyone can advise I would be very grateful.

            kbc1 wrote:

            Its the fact that as i understand it, a checkbox only gets posted when it is checked, so I am not clear on how can detect it has been unchecked and then update the session variable.

            That's true; so if the variable isn't set, then it means that the checkbox was not checked.

            Your problem really comes down to the fact that, because your checkboxes are named "giftwrap[]", there's not really anything to say which checkbox goes with which item in the cart. All you can determine from this is how many items should be giftwrapped; not which ones.

            Presumably you have some id number for each cart item (the $k you use in the loop, possibly - you'll have to answer that one); putting that in the [] will mean that the posted array will use those id numbers as indexes in the $_POST['giftwrap'] array. Do the same for the other fields that go with each item (if any).

            Then when you receive the form, look at the keys in $POST['giftwrap']. This tells you which items in $SESSION['cart'] are to be giftwrapped. Any keys that are not in $_POST['giftwrap'] are items that are not to be giftwrapped.

            And if you're still wanting to know how to tell which keys are not in the array, get an array of ids from the session, and use array_diff().

              hi weedpacket,

              The $k is my Product ID for the item in the cart yes.

              Any keys that are not in $_POST['giftwrap'] are items that are not to be giftwrapped.

              How do i do this bit in the quote?

              Here is my updated code as you suggested:

              foreach ($_SESSION['cart'] as $k => $v) {
              
              		echo "	<div id=\"cartitem$k\">
              					<div id=\"remove_coldata\"><input type=\"checkbox\" name=\"removeitem[]\" value=\"$productid\" /></div>
              					<div id=\"product_coldata\">$productname</div>
              					<div id=\"quantity_coldata\"><input class=\"quantityfield\" name=\"quantity_".$k."\" type=\"textfield\" value=\"" . $v['quantity'] . "\"  /></div>
              					<div id=\"availability_coldata\"><input type=\"checkbox\" name=\"giftwrap[$k]\" value=\"1\" $checkedvalue /></div>
              					<div id=\"price_coldata\">£$ourprice</div>
              					<div id=\"total_coldata\">£$usetotalprice</div>
              				</div>";
              
              
              }
              

              Now when the page is posted/updated, how do I update my session array key 'giftwrap' below to be a 0:

              $_SESSION['cart'][$_POST['product']] = array('quantity' => $_POST['quantity'], 'productprice' => $_POST['productprice'], 'giftwrap' => '1')

              Thanks in advance.

                kbc1 wrote:

                How do i do this bit in the quote?

                Like I said, use the ids in the $_SESSION and a call to [man]array_diff[/man].

                  Write a Reply...