/    Sign up×
Community /Pin to ProfileBookmark

How to delete the item from table?

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:

[https://snag.gy/vOt9VX.jpg](https://snag.gy/vOt9VX.jpg)

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJul 17.2019 — And HOW do you want to do it? With a php script? With an ajax call?


As I suggested to you earlier on another post I believe, you should setup a form surrounding each row of that table. Use input tags for each td element. Then USE AN INPUT 'SUBMIT' TAG and not a button tag. Point the form's action to your script and if the script recognizes that you have received a POST method and if the button value is recognized as 'Delete' (that is how I do it) you can then grab the form's data items from those input elements (using their 'name' attributes) and do the delete query.
Copy linkTweet thisAlerts:
@renegadJul 19.2019 — To start with, indicate the table from which you erase information. Second, utilize a condition to indicate which lines to erase in the WHERE statement. In the event that the line coordinates the condition, it will be erased. Notice that the WHERE proviso is discretionary. In the event that you overlook the WHERE provision, the Erase explanation will erase all columns in the table.
Copy linkTweet thisAlerts:
@KangaRoOAug 05.2019 — I would do it this way:
  • 1. When you add a new row (in html <tr>), you should give an id attr. for the <tr>. This will help to identify the row.

  • 2. When you add the 'Delete' button into <td>, you should create an onClick event, with this value:

    del_row($(this).parent().parent().attr('id')) What does it do? It identifies the row of the button (it sends the <tr> id to a function that will remove the entire row)


  • 3. Let's create the function (jQuery REQUIRED)

    function del_row(id) {<br/>
    $("#" + id).remove();<br/>
    alert('Removed.');<br/>
    }



  • And that's it.

    If you didn't understand: https://jsbin.com/kuladudaro/edit?html,js,console,output here you can see what does it do.
    Copy linkTweet thisAlerts:
    @rickylarsonAug 05.2019 — I didn't uderstand your logic what you want to delete in table.
    Copy linkTweet thisAlerts:
    @KangaRoOAug 05.2019 — @rickylarson#1607188 He wants to delete a row, I guess.
    Copy linkTweet thisAlerts:
    @gkftiiAug 05.2019 — What you really want to do, Not understand your code. Share your code with me.
    Copy linkTweet thisAlerts:
    @ginerjmAug 05.2019 — I do believe that the OP has gone away. No responses in 19days? Yup!

    Continuing on with some of the suggestions made here, I find that using a separate form for each row of data in the html table is pretty simple to understand and, of course, easier to code. When you write the html table, wrap each row (<tr>....</tr>) inside a form tag that calls the same script with the same set of arguments (the input tags placed in each of that row's td tags) that all get processed the same way. There should be one (or more) hidden inputs in a td on each row that identifies the key of the record to be deleted.

    Of course my approach implies that a php script will take over and do the work and then have to regenerate the web page after each deletion. If one wishes to NOT do that and use Ajax instead it becomes more complex of course. Decisions, decisions, decisions.....
    ×

    Success!

    Help @Aveeva7 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 4.19,
    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: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

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

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...