CodeIgniter Forums
[newbie] function after anchor click - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: [newbie] function after anchor click (/showthread.php?tid=6278)



[newbie] function after anchor click - El Forum - 02-20-2008

[eluser]phusiondesign[/eluser]
Sorry for the hand holding... all of the ci fog hasn't cleared yet. Ok, I have this anchor which when clicked I want to perform a function then redirect to the url in the anchor. Example below...

Code:
anchor($ci->link_click($link->url,$link->id,$link->clicks),$link->title)

Now as you can probably tell, the 'link_click' function is executing when the page loads. How do I prevent this and make it so that it only executes when I click that anchor.

Thanks in advance.


[newbie] function after anchor click - El Forum - 02-20-2008

[eluser]Armchair Samurai[/eluser]
Pass your three variables as part of the url, then redirect from the function:
Code:
anchor("foo/bar/$link->url/$link->id/$link->clicks", $link->title);

/**
|
| In your controller
|
*/

function bar()
{
    $url = $this->uri->segment(3);
    $id = $this->uri->segment(4);
    $clicks = $this->uri->segment(5);

    // YOUR CODE HERE

    redirect($url, 'Location');
}



[newbie] function after anchor click - El Forum - 02-20-2008

[eluser]phusiondesign[/eluser]
Thanks. I think this will work except that I am running into a problem when I am passing a full URL, I believe because of the slashes.

Code:
http://ex.com/foo/bar/2/5/http://www.google.com

It gets all the way to the redirect and then... redirects to 'http:'

Any thoughts?


[newbie] function after anchor click - El Forum - 02-20-2008

[eluser]Armchair Samurai[/eluser]
Yeah... out of the box, CI doesn't allow slashes. You can either modify your config.php to allow slashes, or pass the url without the "http://" and add it in your function, something like

Code:
// You'll need to have the URL Helper loaded if you want to use prep_url()
header('Location: '.prep_url($url));



[newbie] function after anchor click - El Forum - 02-20-2008

[eluser]phusiondesign[/eluser]
Thanks for the help... but I avoided the issue altogether by just passing the entry id and accessing the database to get the url and clicks.