Welcome Guest, Not a member yet? Register   Sign In
[newbie] function after anchor click
#1

[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.
#2

[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');
}
#3

[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?
#4

[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));
#5

[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.




Theme © iAndrew 2016 - Forum software by © MyBB