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 [b]echo “<a href=”#” id=”remove” onClick=”document.forms[0].submit(); return false”>Remove</a> <br />”;
Please, help!
[code=php]<?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″> </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″> </td>
<td colspan=”4″ class=”style20″> </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>