Skip to content
Advertisement

How to use confirm box in php codeigniter inside a function of a controller?

public function removeSelectedCategory()
      {
          echo "<script>var r=confirm('Are you sure you want to delete this category?');         </script>";

          if($r==true)
          {
              some code goes here
          }

I have written something like this. Now I want to delete something but I want user to confirm it before deletion. This function is inside controller.

Advertisement

Answer

You can simply provide the JavaScript code when generating the button, link, or whatever needs confirmation. Example:

<a href="..." onclick="return confirmDelete();">Delete category</a>
<!-- or -->
<input type="submit" name="delete" value="Delete" onclick="return confirmDelete();" />

<script type="text/javascript">
    function confirmDelete() {
        return confirm('Are you sure you want to delete this category?');
    }
</script>

That way the link / button would be activated only if the user presses ‘OK’ in the confirmation dialog.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement