I have a select dropdown, with '+' and '-' buttons that add and remove items before submission.

I would like to be able to remove the elements with the '-' button after they have been added to the database.

I'm not sure how to go about doing this, other than removing prior entries and rewriting the options again.

Ideally, it would be reusable and simple to figure for the other developer on the team, or anyone who may replace me down the line. Can someone point me in the right direction?

  • I appreciate this is context dependent, but the most obvious thing that jumps out at me from your description, is that this problem could be avoided if you didn't insert anything into the DB until you submitted the form. – user96303 Jul 31 '14 at 18:03
  • Sorry if this wasn't clear, but this is for an edit form. You are correct in only submitting necessary data, but when revisiting the form, how do you remove the unnecessary data. – user3746654 Jul 31 '14 at 19:13

So many ways to do this, especially if you have JS. On click of a negative call a function that looks up the id of that record and adds it to an array of hidden inputs then hides it from the list of records.

<input type='hidden' name='remove[]' value=$id>

When you submit the form have it walk the remove array and do the delete queries. With PDO you could do it with a single statement and bindParam().

Without JS it is harder but doable. Have the x be a submit button on it's own with a value the PHP is looking for seperate from an full form update, delete the individual record from the db then reload the forms.

Another way is to use CSS to reskin a checkbox with a deactivated and activated delete icon. Then you can look for checked inputs it in your form processing. Of the 3 I have not actually implemented it myself.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.