Uly.me

cloud engineer

  • Home
  • About
  • Archives
Home/PHP/CodeIgniter Anchor With Onclick

January 31, 2013

CodeIgniter Anchor With Onclick

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

Have content delivered to your mail. Subscribe below.

About Me

I'm Ulysses, a Cloud Engineer at Cardinal Health based in Columbus, Ohio. I’m a certified AWS Solutions Architect. This website is my way of documenting the things I have learned in the Cloud. When off the grid, I enjoy riding my electric skateboard. I have surfed, snowboarded and played the saxophone in the past. I hope you will find this site helpful. It's powered by WordPress and hosted in AWS LightSail.

  • Cloud
  • Linux
  • Git

Copyright © 2012–2021