/    Sign up×
Community /Pin to ProfileBookmark

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 [b]echo “<a href=”#” id=”remove” onClick=”document.forms[0].submit(); return false”>Remove</a> <br />”;[/b] 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!

[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&#37;” 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>[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@cachopaOct 10.2008 — 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.
Copy linkTweet thisAlerts:
@UltimaterOct 10.2008 — Untested:
[code=php]
<?php
if(isset($_POST['addVehicle']))
foreach($_SESSION['country'] as $value)
echo '<a href="#" onclick="return removeEntry('.$value.')">Remove</a> <br />';
?>
[/code]


<i>
</i>&lt;script type="text/javascript"&gt;
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;
}
&lt;/script&gt;


[code=php]
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);
}
[/code]
Copy linkTweet thisAlerts:
@RoxxorauthorOct 10.2008 — [b]Ultimater:[/b] 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?
Copy linkTweet thisAlerts:
@RoxxorauthorOct 10.2008 — [b]Ultimater:[/b] I have done tested the code you posted, but I can´t get it work. The page reloads but the array still contains the element.

Something I´ve missed?[/QUOTE]


Now I got it work. Thanks!
×

Success!

Help @Roxxor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...