Jump to content

Recommended Posts

Hello friends,

My name is Jay and I'm a sys admin and have very little experience with PHP and stuff. We have an internal system developed by a dev and I'm trying to make some changes to it and hoping if someone could help.

So we have a simple results page which will fetch records from a MySQL table and display in table format. 

This is the code file - https://pastebin.com/VwxAVY9y

I'm trying to add a delete button next to each records and delete those records dynamically. I don't need a warning or any prompt/confirmation etc.

So I added a 3rd column as "<th>Action</th>" but I'm having a hard time incorporating the AJAX delete function within the echo "<td> </td>" section. 

I did quite a lot of google search and couldn't figure this out. 

What I want is a colum with a delete button like this attached screenshot. 

Any help is appreciated :)

Cheers

Jay

Monosnap Delete Data From SQL Table Using PHP & MySQL 2023-02-03 18-24-55.png

Edited by Sysadmin20
Link to comment
https://forums.phpfreaks.com/topic/315874-help-with-php-form-and-action-button/
Share on other sites

Each button needs to know which result it's supposed to delete. Since you're doing this with AJAX you have a lot of flexibility in how to go about it, but my preferred is a data attribute.

<button data-deletes-record="(id goes here)">Delete</button>

With jQuery, you can then wait for a click event on a button with that attribute,

$("#allRecords").on("click", "button[data-deletes-record]", function() {

Inside that you can grab the ID with .attr() or .data() and then pass it through AJAX...

6 minutes ago, requinix said:

Each button needs to know which result it's supposed to delete. Since you're doing this with AJAX you have a lot of flexibility in how to go about it, but my preferred is a data attribute.

<button data-deletes-record="(id goes here)">Delete</button>

With jQuery, you can then wait for a click event on a button with that attribute,

$("#allRecords").on("click", "button[data-deletes-record]", function() {

Inside that you can grab the ID with .attr() or .data() and then pass it through AJAX...

Thanks for the reply. I'm not sure if I can write that code but at least now I know what to research online. I'll give it a try. Thank you :) 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.