Trongate Docs
switch to dark modeswitch to dark mode
Deleting Records

Deleting Records

Please keep in mind that the method described here produces webpages that are wide open.  So, anyone would be able to update your records!  In real-life situations, please make sure your webpages are secure.  For a Trongate admin panel, this could be as simple as adding the following two lines of code onto your methods:


​If you're a developer who prefers video tutorials, here's everything you need to know about deleting records:​

Video Tutorial

In this video I'm going to demonstrate a graceful way to handle deleting records

For those of you who prefer written docs, here's a summary of everything that gets covered in the video:

General Guidelines

Deleting database records with Trongate is easy.  All we have to do is call upon the Trongate's in-built delete() method and with just one line of code, database records can be easily deleted.


Just To Let You Know
The Trongate delete method is covered in detail at:

However, whilst deletion of records is easy, it's a bad practice to simply add a 'Delete' button onto a page and then perform a delete whenever it gets clicked.  Such a crude system would be prone to accidental deletions.

So, our goal here will be to have a delete button that opens a pop-up modal, when clicked.  The pop-up modal will give the user an opportunity to confirm if they wish to delete the record.

Step 1: Pass The Update ID Into The View

Let's modify our create method.  If you'll followed along then your create method should have an $update_id already declared.  So, let's pass that into our create.php view file by adding:

$data['update_id'] = $update_id;

Step 2: Add an IF Statement

Now, open up your create.php view file inside your text editor.  Immediately after the 'Cancel' button we're going to add an IF statement that checks to see if the $update_id is greater than zero.  If the answer is 'yes' then we'll draw a delete button.

if ($update_id>0) {
    $delete_attr['class'] = 'danger go-right';
    $delete_attr['onclick'] = 'openModal(\'confirm-delete\')';
    echo form_button('delete', 'Delete', $delete_attr);

As you can see, we are adding some attributes onto our delete button.  We have:

  • a 'danger' class to make the button go red
  • a 'go-right' class to send the delete button to the right hand side of the page
  • an 'onclick' attribute that attempts to open a modal when clicked

Step 3: Building The Modal

The Trongate admin panel comes with some pre-build CSS and javascript to assist you in building attractive looking modals.

Did You Know?
In web development, a 'modal' is a word that gets used to represent an HTML element that pops-out and appears 'in front of' other page elements.  In simple terms, you can think of a modal as being a like looking pop-up window.

In Trongate, modal activation requires:

  • a button (or other clickable element) that opens the modal
  • the HTML code for the modal the should appear on the page

We've already dealt with the business of creating a button that opens a modal.  The key to making this work is to have an onclick event set to 'openModal()'.  The openModal() method is a javascript method that accepts one argument: the ID of a modal element.

In our case, we're effectively telling Trongate that when our 'Delete' button is clicked then a modal with an ID of 'confirm-delete' should appear on the screen.

This brings us onto:


Here's the code that we'll be using for our delete confirmation modal:

<div class="modal" id="confirm-delete" style="display: none;">
  <div class="modal-heading danger">Delete Record</div>
  <div class="modal-body">
    <p>You are about to delete a record. Are you sure?</p>
    echo form_open('books/submit_delete/'.$update_id, array('class' => 'text-center'));
    echo form_submit('submit', 'Yes - Delete', array('class' => 'danger'));
    $cancel_attr['class'] = 'alt';
    $cancel_attr['onclick'] = 'closeModal()';
    echo form_button('cancel', 'Cancel', $cancel_attr);
    echo form_close();

Some Things To Keep In Mind Regarding The Modal

  • The modal element must have an ID that matches the first argument that gets passed into then openModal() onclick event.
  • The modal element should be a div that contains a class of 'modal'
  • Modal elements typically contain two other divs: one with class of 'modal-heading' and another with a class of 'modal-body'
  • Apart from one or two CSS class declarations, everything that happens inside our modal is perfectly normal HTML and PHP.  So, as you can see, we are treating our modal like an ordinary form - complete with a submit button.
  • Our 'cancel' button contains an onlick event that invokes closeModal().  This is a javascript method - that comes with the 'admin' template, that closes the modal when clicked

Onto The Business Part Of Deletion

Once a user has clicked 'Delete' and then clicked a second time (on the modal) to confirm deletion, this will lead users to the submit_delete() method.  The code for this is simple.  Here we simply:

  • read an update ID from the URL
  • convert out update ID into an integer
  • invoke the delete() method to delete the database record
  • add some flashdata to let the user know what has happened
  • redirect the user to the 'manage' page

Here's the code:

function submit_delete() {
  $update_id = segment(3, 'int');
  set_flashdata('The record was successfully deleted');


If you have a question or a comment relating to anything you've see here, please goto the Help Bar.