Skip to main content
Participant
December 28, 2013
Question

Delete items unchecked from a form

  • December 28, 2013
  • 2 replies
  • 1644 views

I have a list from query that populate checkboxes (checked) in a form.

However, when a user deselects some of the items from, I want them to be deleted from the database for that user.

Here is the ex: this is how the form looks like:

                                         Category

Item                                  80      626

1. HS # 1 (87)                   chk    chk

2. HS #2  (59)                   chk    chk

This is the list of items already n their preferences (db ids)

80|87,  626|87,   80|59,   626|59

Now from a form, if they deselect one of the items (80|87), I want it to be deleted from the database.

Like these, DELETE FROM ITEMS_TBL WHERE ITEM_ID = 80, Category_ID = 87

How can we do it? Can we check ListContains or something else?

This topic has been closed for replies.

2 replies

Inspiring
December 29, 2013

it would be a lot easier to do it the other way round, have a checkbox for records you want to be deleted.

Or have a delete button and do it realtime in ajax.

If you really do want to do it this way, then have some javascript that detects when boxes are unchecked, and adds their values to a hidden form field, then you have a list of the records to be deleted.

Inspiring
December 29, 2013

The only issue with using a hidden form field is that users can modify this value before it is submitted.  So you'll have to do extra cross checking to ensure that the IDs that are there are ones that were initially displayed or that the user had access to delete regardless.

Inspiring
December 29, 2013

Aegis, anyone can modify checkboxes or indeed any form field if they feel so inclined, so that is completely moot.

Inspiring
December 29, 2013

Let's say your query contained 5 records, ID's 1-5.  When output, the user unselects ID 2 and 5.  When they submit the form, you want to delete IDs 2 and 5 right?  But since they are not selected, their IDs are not available in the FORM scope, correct?

What I would do is run the same query on submit, but add an addendum that uses a ValueList() on the FORM variable that holds the selected IDs.  Like:

SELECT

ids,

...

WHERE ids NOT IN (#FORM.ids#)

That would basically get you back a query of record IDs that are up for deletion.  Then you could run another query right after it that goes:

DELETE FROM tableName

WHERE IDs in (#ValueList( firstQueryName.ids )#)