Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2005
    Posts
    15

    Question Unanswered: Physicaly remove list item (not array)

    Is there a way to remove (not hide) items between certian tags? In my instance, users add to a list <ol><li>. I want them to be able to remove something from that list if they want. Is there a way to put something to identify each row and remove specific items? Would this be better suited for Javascript? Thanks

  2. #2
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    If I understand your question correctly, this is pretty simple in javascript. You could do something like:


    Code:
    <ol>
    <li onclick="removeFromList(this);" ...>
    ...
    </ol>

    with the function removeFromList:

    Code:
    function removeFromList(el) {
        var parent = el.parentNode;
        parent.removeChild(el);
    }
    Hope that helps some.

    check out:
    http://www.scit.wlv.ac.uk/~jphb/javascript/dom2.html
    http://www.mozilla.org/docs/dom/tech...e/#fundamental

  3. #3
    Join Date
    Jul 2005
    Posts
    15
    Thanks, I'll give it a shot. Just to make sure, it will remove the line completly not just hide it?

  4. #4
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    You can use javascript to access the nodes of an HTML/XML document. Google DOM (Document Object Model) to find out more.

    What the code I wrote should do...suppose you have a list like:
    Code:
    <ol>
    <li onclick="removeFromList(this);">Item 1</li>
    <li onclick="removeFromList(this);">Item 2</li>
    <li onclick="removeFromList(this);">Item 3</li>
    <li onclick="removeFromList(this);">Item 4</li>
    </ol>
    now I click on Item 2. The html will now look like:
    Code:
    <ol>
    <li onclick="removeFromList(this);">Item 1</li>
    <li onclick="removeFromList(this);">Item 3</li>
    <li onclick="removeFromList(this);">Item 4</li>
    </ol>
    The code doesn't use css or hiding. It actually deletes the <li> node from the document. You may need to modify it a little bit, but that's the general idea.

  5. #5
    Join Date
    Jul 2005
    Posts
    15
    Works Great! But it didn't solve the problem Like I thought it would. Maybe you can shed some light on why my idea isn't working. I have a page where a user chooses Quantity - Product - Color - Size. Then submits it. The info goes into an array and is displayed. The user can add as many as they want and it adds the info to the array (arrays inside an array). I figure someone will decide to remove an item so I've been working for a few days on how to remove an array from the master array. I can remove it, but (I think because of sessions) it comes back. Here is what I'm working with:
    Code:
    <?php session_start(); 
    if(isset($_POST['product'])) { 
      $thisItem['qty'] = $_POST['quantity']; 
      $thisItem['product'] = $_POST['product']; 
      $thisItem['color'] = $_POST['select_2']; 
      $thisItem['size'] = $_POST['select_X']; 
      $_SESSION['items'][] = $thisItem; # add array to session 'items' array 
    } 
    ?> 
    Later in the page...
    <form name="form_php" action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    <?php
    $arr = $_SESSION['items'];			
    if(isset($_POST['checkbox1'])){unset($arr[0]);}
    if(isset($_POST['checkbox2'])){unset($arr[1]);}
    if(isset($_POST['checkbox3'])){unset($arr[2]);}
    if(isset($_SESSION['items']) and count($_SESSION['items']) > 0) 
    { 
      echo "<ol>\n"; 
      foreach($_SESSION['items'] as $ix => $val) # val will be an array 
      { 
      $ixx = $ix+1; # just makes the number start at 1 insted of 0
    
    echo "<li id='a$ixx'> $ixx ({$val['qty']}) - {$val['product']}, {$val['color']}, {$val['size']}   </li>\n"; 
      } 
      echo "</ol>\n"; 
    } 
    ?>
    <input type='checkbox' name='checkbox1' value='checkbox'> <input type='button' value='Remove' name='remove1' onclick='removeFromList(a1);submit(this);'>1<br>
    <input type='checkbox' name='checkbox2' value='checkbox'> <input type='button' value='Remove' name='remove1' onclick='removeFromList(a2);submit(this);'>2<br>
    <input type='checkbox' name='checkbox3' value='checkbox'> <input type='button' value='Remove' name='remove1' onclick='removeFromList(a3);submit(this);'>3<br>
    I am just trying to get anything to work. I simply want the users to be able to remove a row. You can get a better idea if you'd like (and if I haven't messed it all up) at http://www.localheroclothing.com/step2bb.php. Just add some specs and don't leave the page. I'm not looking for anyone to write the code for me. I've been working for a few days and I don't think I'm headed in the right direction. Thanks

  6. #6
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    If you're trying to remove items from your shopping cart, you'll need to do it using php. The javascript code that I posted will only change the appearance of the webpage.

    First of all, I'd say your best bet is to create ShoppingCart and Item classes. This would make things much easier as you could just pull the cart from the Session variable to update and then stick it back in there to save it. No need for all of the arrays and setting unsetting. You could just make calls like:
    PHP Code:
     $cart = &$_SESSION['cart'];
     
    $item = new Item($_POST['product'], $_POST['color'], $_POST['size']);
     
    $cart->addItem($item$_POST['qty']);
     
    $cart->removeItem($_POST['itemnumber']);  //or something similar
     
    $_SESSION['cart'] = $cart;  //is this needed if we use a reference var? 
    I'm not 100% sure about the use of the '&' to reference session variables. But play with it.

    (A note: if you are using a shared webserver, you may have difficulty storing user created objects in session variables. The session_autostart() parameter must be turned off in php.ini. It makes sense, but I never thought about it and this frustrated me for quite some time until I figured it out.)


    But going with what you have. In order to remove the items from the cart (the session variable), you should send a request to the php page, ie. submit the form. Create a separate form for the remove checkboxes (if it's all on the same form, you'll be creating new items in the cart because the 'product' 'color' 'size' will be set in your POST).

    And then I believe you may need to change the following line(somebody correct me if I'm wrong):
    PHP Code:
    $arr $_SESSION['items']; 
    to
    PHP Code:
    $arr = &$_SESSION['items']; 
    or rather, after the modifications, just write:
    PHP Code:
    $_SESSION['items'] = $arr;  
    //is that proper syntax for setting session variables? 

    If you don't want to go with all of the forms to remove items, just write a simple javascript function like:

    Code:
    function removeItem(idx) {
       url = window.location + "?action=remove&item=" + idx;
       window.location = url;
    }
    Heh, or something like that. Just trying to give you some quick ideas. You get the picture.

  7. #7
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    Sorry about writing the novel.

    Basically, I'm pretty sure you need to update your session variable. Either by using the '&' operator, or by explicity stating $_SESSION['items'] = $arr; after you have modified that array.

  8. #8
    Join Date
    Jul 2005
    Posts
    15
    No need to apologize. I appreciate all the help I can get!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •