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

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:

What have you tried so far? Show us the code and weā€™ll help figure out why itā€™s not working, but no-one here will just write it for you.

Do you just want to delete it from the session variables, or is there a database table linked here? Iā€™m not sure when you say ā€œdelete item from tableā€ whether you are referring to the HTML table, or a database table.

Just session, not from table.

OK, step one is to change your code so that it has a html form around each button, and contains a reference to the unique identifier in your $_SESSION array as a hidden variable. This will be similar to the code that you already have to add the items into the array.

When you have that working, have the form post to a short bit of PHP code that just displays the contents of the $_POST array, so you can see you are getting the correct id from the form.

One that is working correctly and showing the correct value, you can then add the code to remove it from the $_SESSION array and return to the original page to redraw the list. And when thatā€™s working, you can add some JS code to remove it from the array without needing a redraw.

1 Like

Anyone help me with my workout :

my_cart.php:

<?php
header('Content-Type: text/html; charset=utf-8');
session_start();
session_regenerate_id();

echo '
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <metaname="viewport"content="width=device-width,height=device-height,initial-scale=1">
        <title>Playlist</title>
    </head>

    <body>
	    <form id="playlist" action="playlist_action.php" method="post">
		    <table id="playlist">
		        <caption>Play List</caption>
		        <thead>
			        <tr>
			    	    <th scope="col">Voice SKU</th>
			    	    <th scope="col">Voice Name</th>
			    	    <th scope="col">Action</th>
			        </tr>
                </thead>

                <tbody>';
                    foreach ($_SESSION['playlist'] as $key => $value) 
                    {
	                    $key = htmlspecialchars($key);
	                    echo '
			    	    <tr>
			    		    <th scope="row">', $key, '</th>
			    		    <td>', htmlspecialchars($value), '</td>
			    		    <td><button name="delete" value="', $key, '">Delete</button></td>
			    	    </tr>';
                    }
                    echo '
			    </tbody>
		    </table>
	    </form>
    </body>
</html>';

and
playlist_action.php

<?php
header('Content-Type: text/html; charset=utf-8');
session_start();
session_regenerate_id();

if (array_key_exists('playlist_action', $_POST)) {
	switch ($_POST['playlist_action']) {
		case 'delete':
			if (array_key_exists($_POST['delete'], $_SESSION['playlist']) {
				unset($_SESSION['playlist'][$_POST['delete']]);
                // notify user of successful delete
			} else {
				// notify user of non-existent key in the playlist
			}	
		default:
			// handle invalid/unknown action here.
	}
}
?>

my output:

|Parse error: syntax error, unexpected ā€˜unsetā€™ (T_UNSET) in C:\wamp\www\voice_bank\playlist_action.php on line 10|

Do this bit first. Get your form tags in there, and add a hidden field with your $key value in it. Plenty of references around showing how to create a html form if you canā€™t use the ā€œadd to cartā€ code as a reference.

This is because you open more brackets than you close on the line before it. But thereā€™s no need to mess with the PHP until you have a form to post to it.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.