Forum Moderators: coopster

Message Too Old, No Replies

How to delete the item from table? I am using SESSION.

         

aveevan

11:40 am on Jul 19, 2019 (gmt 0)

5+ Year Member



Here is my code :

<html> 
<head>

<style type="text/css">
table {
border-collapse: collapse;
border: 1px solid black;
}
table td,th {
border: 1px solid black;
}
td {
text-align: center;
}
</style>
</head>

<body>

<h2>Play Lists</h2>

<table>
<th>Voice SKU</th>
<th>Voice Name</th>
<th>Action</th>


<?php
session_start();
foreach ($_SESSION['playlist'] as $key => $value) {
echo "<tr>";
echo "<td>" . $key . "</td>\n<td>" . $value . "</td>";

echo "<td>". "<button id='btn'>Delete</button>"."</td>";

echo "</tr>";
}
?>
</table>
</body>
</html>


output:

[snag.gy ]

Dimitri

11:48 am on Jul 19, 2019 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



You can pass the $key to delete to the script, like script.php?key=xxx

And to delete you do :

$key_to_delete=$_GET['key'];

if(isset($_SESSION['playlist'][$key_to_delete]))
{
unset($_SESSION['playlist'][$key_to_delete]);
}

aveevan

12:01 pm on Jul 19, 2019 (gmt 0)

5+ Year Member



Thank You.

aveevan

12:20 pm on Jul 19, 2019 (gmt 0)

5+ Year Member



Make a pardon to ask more details, i am in the learning stage,

here is my code :

<html>
<head>

<style type="text/css">
table {
border-collapse: collapse;
border: 1px solid black;
}
table td,th {
border: 1px solid black;
}
td {
text-align: center;
}
</style>
</head>

<body>

<h2>Play Lists</h2>

<table>
<th>Voice SKU</th>
<th>Voice Name</th>


<?php
session_start();
foreach ($_SESSION['playlist'] as $key => $value) {
echo "<tr>";
echo "<td>" . $key . "</td>\n<td>" . $value . "</td>";

// $key_to_delete=$_GET['$key'];

// if(isset($_SESSION['playlist'][$key_to_delete]))
// {
// unset($_SESSION['playlist'][$key_to_delete]);
// }


echo "<td>" ."<button class='delbtn' data-id='"+ k +"'>Delete</button>" . "<td>";


echo "</tr>";
}

?>
</table>
</body>
</html>


How can i use your code? I know i am asking pure coding help, but i really don't know how to do?

Dimitri

3:49 pm on Jul 19, 2019 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month




session_start();

if(isset($_GET['key']))
{
$key=$_GET['key'];
if(isset($_SESSION['playlist'][$key]))
{
unset($_SESSION['playlist'][$key]);
}
}

foreach ($_SESSION['playlist'] as $key => $value)
{

$url=$_SERVER['PHP_SELF'].'?key='.$key;
echo "<tr>";
echo "<td>" . $key . "</td>\n<td>" . $value . "</td>";
echo "<td><a href=\"",$url,"\">Delete</a></td>";
echo "</tr>";
}


I just improvised the code here, so it might not work straight away.

If you want to use a button, you can use the onclick event.

Also in your code, you have the id="btn" which is incorrect, because each id has to be unique.

I never used PHP 's session system ,so I guess there is a feature to use, to save the modifications.

aveevan

9:07 am on Jul 22, 2019 (gmt 0)

5+ Year Member



Really thank you for your code help, working successfully.

Dimitri

11:06 am on Jul 22, 2019 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



You are welcome