www.webdeveloper.com
Results 1 to 5 of 5

Thread: Remove elements from a session array

  1. #1
    Join Date
    Jan 2008
    Posts
    56

    Remove elements from a session array

    I am building a form where a user will have the ability to add items from selection lists. The items will be stored in session arrays after beeing added.
    The user will also have the ability to remove added items and that is the problem: I don´t know how to build such a function.

    I have added echo "<a href=\"#\" id=\"remove\" onClick=\"document.forms[0].submit(); return false\">Remove</a> <br />"; to send the session id to be removed, but I don&#180;t know how I can complete the function. You can run the code below as an example.

    Please, help!

    PHP Code:
    <?php
        session_start
    ();

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <style type="text/css">
    <!--
    .style20 {color: #333333; font-size: x-small; font-family: Verdana;}
    -->
    </style>

    <SCRIPT LANGAUGE="JavaScript">

    function addItems()
    {
        var vehicle = document.getElementById('vehicle');
        var manufacturer = document.getElementById('manufacturer');

        while(manufacturer.options.length > 0)
            manufacturer[manufacturer.options.length - 1] = null;

        if(vehicle.value=="Cars")
        {
            manufacturer.options[manufacturer.options.length] = new Option(<?php echo "'Ferrari','Ferrari'"?>);
            manufacturer.options[manufacturer.options.length] = new Option(<?php echo "'Porsche','Porsche'"?>);
        }
        else if(vehicle.value=="MCs")
        {
            manufacturer.options[manufacturer.options.length] = new Option(<?php echo "'Ducati','Ducati'"?>);
            manufacturer.options[manufacturer.options.length] = new Option(<?php echo "'Kawasaki','Kawasaki'"?>);
            manufacturer.options[manufacturer.options.length] = new Option(<?php echo "'Honda','Honda'"?>);
        }    
        
    }
    </SCRIPT>

    </head>

    <body>
    <form name"addvehicles" method="post" action="">
    <table width="100%" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td colspan="5" class="style20"><div align="left"><strong>Utbildning</strong></div></td>
      </tr>
      <tr>
        <td width="17%" height="36" class="style20">Vehicle</td>
        <td colspan="4" class="style20"><div align="left">
          <select name="vehicle" id="vehicle" onchange="addItems()">
            <option>- Choose vehicle -</option>
            <option value="Cars">Cars</option>
            <option value="MCs">MCs</option>
          </select>
        </div></td>
      </tr>
      <tr>
        <td height="25" class="style20">Manufacturer</td>
        <td colspan="4" class="style20"><select name="manufacturer" id="manufacturer">
        </select></td>
      </tr>
      <tr>
        <td height="25" class="style20">&nbsp;</td>
        <td colspan="4" class="style20"></td>
      </tr>
      <tr>
        <td height="25" class="style20">Country and city</td>
        <td colspan="4" class="style20"></td>
      </tr>
      <tr>
        <td height="25" colspan="5" class="style20">
          <div align="left">
            <select name="country" id="country">
              <option value="USA">USA</option>
              <option value="United Kingdom">Unitet Kingdom</option>
              <option value="Germany">Germany</option>
              <option value="France">France</option>
            </select>
            <select name="city" id="city">
              <option value="City1">City1</option>
              <option value="City2">City2</option>
              <option value="City3">City3</option>
              <option value="City4">City4</option>
              <option value="City5">City5</option>
            </select>
            <input type="submit" name="addVehicle" id="addVehicle" value="Add" />
            </div>  </td>
      </tr>
      <?php     
     
            
    if(isset($_POST['addVehicle']) or isset($_POST['remove'])) 
            {            
                
    $_SESSION['vehicle'][] = $_POST['vehicle'];
                
    $_SESSION['manufacturer'][] = $_POST['manufacturer'];
                
    $_SESSION['country'][]  = $_POST['country'];
                
    $_SESSION['city'][]  = $_POST['city'];
            
            }
            
    ?>
      <tr>
        <td height="31" class="style20"><div align="left">
          <p>
            <?php
            
    if(isset($_POST['addVehicle'])) 
                foreach(
    $_SESSION['vehicle'] as $value)
                    echo 
    $value."<br />";
        
    ?>
          </p>
          </div></td>
        <td width="19%" height="31" class="style20"><div align="left">
          <?php    
            
    if(isset($_POST['addVehicle']))     
                foreach(
    $_SESSION['manufacturer']  as $value)
                    echo 
    $value."<br />";
        
    ?>
        </div></td>
        <td width="22%" class="style20"><div align="left">
          <?php
            
    if(isset($_POST['addVehicle']))         
                foreach(
    $_SESSION['country'] as $value)
                    echo 
    $value."<br />";
        
    ?>
        </div></td>
        <td width="23%" class="style20"><div align="left">
          <?php
            
    if(isset($_POST['addVehicle'])) 
                foreach(
    $_SESSION['city'] as $value)
                    echo 
    $value."<br />";
            
    ?>
          
          </div>
        <td width="19%" class="style20"> 
        <?php
            
    if(isset($_POST['addVehicle'])) 
                foreach(
    $_SESSION['country'] as $value)
                    echo 
    "<a href=\"#\" id=\"remove\" onClick=\"document.forms[0].submit(); return false\">Remove</a> <br />";
            
    ?>
        <tr>
        <td class="style20">&nbsp;</td>
        <td colspan="4" class="style20">&nbsp;</td>
      </tr>
      <tr>
        <td class="style20"><label> </label>
            <div align="left"></div></td>
        <td colspan="4" class="style20"><div align="left">
          <input type="button" name="next" id="next" value="Next" />
        </div></td>
      </tr>
    </table>  
    </form>
    </body>
    </html>
    Last edited by Roxxor; 10-09-2008 at 12:29 PM.

  2. #2
    Join Date
    May 2007
    Posts
    12
    I'm not sure if I understood the question, but does the "unset" solve your problem?

    With unset you will be able to clean the variable.

  3. #3
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Untested:
    PHP Code:
    <?php
            
    if(isset($_POST['addVehicle']))
                foreach(
    $_SESSION['country'] as $value)
                    echo 
    '<a href="#" onclick="return removeEntry('.$value.')">Remove</a> <br />';
            
    ?>
    Code:
    <script type="text/javascript">
    function removeEntry(removeSessionIndex){
    var input=document.createElement("input");
    input.type="hidden";
    input.name="removeIndex";
    input.id="removeIndex";
    input.value=removeSessionIndex;
    document.forms[0].appendChild(input);
    setTimeout('document.forms[0].submit()',1);
    return false;
    }
    </script>
    PHP Code:
    if(isset($_POST['removeIndex'])){
    $removeIdx=(int)$_POST['removeIndex'];
    array_splice($_SESSION['vehicle'],$removeIdx,1);
    array_splice($_SESSION['manufacturer'],$removeIdx,1);
    array_splice($_SESSION['country'],$removeIdx,1);
    array_splice($_SESSION['city'],$removeIdx,1);


  4. #4
    Join Date
    Jan 2008
    Posts
    56
    Ultimater: I have done tested the code you posted, but I can&#180;t get it work. The page reloads but the array still contains the element.

    Something I&#180;ve missed?

  5. #5
    Join Date
    Jan 2008
    Posts
    56
    Quote Originally Posted by Roxxor View Post
    Ultimater: I have done tested the code you posted, but I cant get it work. The page reloads but the array still contains the element.

    Something Ive missed?
    Now I got it work. Thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles