• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

CodeIgniter Anchor With Onclick

January 31, 2013

CodeIgniter comes with a URL helper that assist you with managing URLs. One function worth examining is called anchor, which creates standard HTML links. Instead of the standard HTML <a href=””>anchor</a> tag most people are familiar with, CodeIgniter coders can take advantage of the simpler anchor function.

To create a standard link, all you have to do is code this:

<?=anchor('blog /index', 'Home');?>
 
// Output is
<a href="http://yourdomain.com/blog/index.php">Home</a>

<?=anchor('blog /index', 'Home');?> // Output is <a href="http://yourdomain.com/blog/index.php">Home</a>

If you want to add a Javascript popup that will ask you “Are you sure?” before proceeding to the link, then all you have to do is add a third option in the anchor function like this:

<?php $onclick = array('onclick'=>"return confirm('Are you sure?')");?>
<?=anchor('blog /index', 'Home', $onclick);?>
 
// Output is
<a href="http://yourdomain.com/blog/index.php" onclick="return confirm('Are you sure?')">Home</a>

<?php $onclick = array('onclick'=>"return confirm('Are you sure?')");?> <?=anchor('blog /index', 'Home', $onclick);?> // Output is <a href="http://yourdomain.com/blog/index.php" onclick="return confirm('Are you sure?')">Home</a>

This function works great especially if you add it to code that delete records. You like to be able to ask the user first if that’s what they really want to do before proceeding.

Filed Under: PHP Tagged With: anchor, codeigniter, javascript, url helper

Search This Website

Subscribe Via Email

  • Home
  • About
  • Archives

Copyright © 2023