lockdownd7 Posted September 23, 2009 Share Posted September 23, 2009 Hi, I want to use php to do something similar to javascripts onclick function. Basically I don't want to generate the link until a user clicks on it, and depending on what they clicked is what the url will be. My first thought was to have all links go to something like redirect.php, and use a session variable to track which link they had clicked then use metarefresh.... but that won't work unless there's a way to uniquely identify each link.... and I couldn't come up with anything. So if anyone has any suggestions, I'd be very grateful. Quote Link to comment https://forums.phpfreaks.com/topic/175270-solved-how-to-emulate-javascript-onclick/ Share on other sites More sharing options...
nrobi Posted September 23, 2009 Share Posted September 23, 2009 I'm not sure what you're trying to accomplish exactly.... if you're trying to dynamically change page contents AJAX might be the answer for you. Quote Link to comment https://forums.phpfreaks.com/topic/175270-solved-how-to-emulate-javascript-onclick/#findComment-923752 Share on other sites More sharing options...
Alex Posted September 23, 2009 Share Posted September 23, 2009 You're thinking about PHP in the wrong way. PHP is a server-side language. JavaScript is client-side. You're wording your question completely wrong so it's hard to know exactly what you want.. PHP is a preprocessor (hence the name, PHP Hypertext Preprocessor), it processes everything and then outputs the result to the browser. So it wouldn't handle anything done after the page has already loaded; so an onclick event in PHP essentially makes absolutely no sense. As I said before I'm not completely sure what you want.. But you can echo JavaScript and have it be handled by the Browser.. echo "<a href='http://www.google.com' onclick='somefunction();'>Stuff</a>"; Quote Link to comment https://forums.phpfreaks.com/topic/175270-solved-how-to-emulate-javascript-onclick/#findComment-923761 Share on other sites More sharing options...
lockdownd7 Posted September 23, 2009 Author Share Posted September 23, 2009 Yes, and I know javascript is what would be best for this task but I didn't think it would be a good idea to rely on JS because users can disable it. I guess I worded it very poorly before. I'm trying to make a redirection script that dynamically selects the destination based on the source. In other words, if the link meets certain parameters, it links to one place, if it meets other parameters, it link somewhere else, etc. Preferrably using php because: a) I want it to work for all users b) I have other php scripts I would like it to work with Quote Link to comment https://forums.phpfreaks.com/topic/175270-solved-how-to-emulate-javascript-onclick/#findComment-923798 Share on other sites More sharing options...
PHP_Newbie1 Posted September 24, 2009 Share Posted September 24, 2009 Would ActionScript help? Quote Link to comment https://forums.phpfreaks.com/topic/175270-solved-how-to-emulate-javascript-onclick/#findComment-923859 Share on other sites More sharing options...
Alex Posted September 24, 2009 Share Posted September 24, 2009 I'm pretty sure he doesn't want to do anything flash related.. You should give specific examples of what you wish to achieve for better help. Quote Link to comment https://forums.phpfreaks.com/topic/175270-solved-how-to-emulate-javascript-onclick/#findComment-923860 Share on other sites More sharing options...
.josh Posted September 24, 2009 Share Posted September 24, 2009 i have no idea what you're trying to accomplish either... you say you don't want to use js because users can disable it. But you say that you want it to change based on what a link source is. Well how is this link source being generated? By js? If so, then you're already using js... with server-side scripting? Well the html output is coming from the server in the first place. What part of your script is generating the html? Quote Link to comment https://forums.phpfreaks.com/topic/175270-solved-how-to-emulate-javascript-onclick/#findComment-923886 Share on other sites More sharing options...
lockdownd7 Posted September 24, 2009 Author Share Posted September 24, 2009 Sorry for the confusion. I should have mentioned earlier that the main point is that I want to mask the link. I've got so much crap I'm working on right now I can't keep it all straight. I'm sorry. *Slams head against wall* Anyway, yes, the html output is being generated with php. But if I just straight link then the destination url will be visible. I'd like to redirect with meta refresh so I can rel nofollow the redirect page and keep serp. However what I'm trying to avoid is having a separate file on the server for each link. Yet in order to make sure the link ultimately goes to the right place I need a way to track where it came from. Also, http_referer won't really work in this case because there are many links on each page. This is a bitch, I know. Quote Link to comment https://forums.phpfreaks.com/topic/175270-solved-how-to-emulate-javascript-onclick/#findComment-923937 Share on other sites More sharing options...
.josh Posted September 24, 2009 Share Posted September 24, 2009 okay so basically you're going to have to have a unique id for each link, and instead of having the link point to the real url, have it point to a single page, with the id attached to the url, and on that page, forward the user to the real url based on that id. Example: pagea.php <a href='controller.php?id=123'>link A</a> <a href='controller.php?id=456'>link B</a> controller.php <?php // example of id & link list. can hardcode it as array or store in a text file or db or whatever $links = array('default' => 'someDefaultPage.php', '123' => 'realpageA.php', '456' => 'realpageB.php'); // get the real url based on the id passed. If it's not in the array, assign a default $dest = (isset($links[$_GET['id']))? $links[$_GET['id'] : $links['default']; // rediret user to real page header("Location: $dest"); exit(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/175270-solved-how-to-emulate-javascript-onclick/#findComment-924141 Share on other sites More sharing options...
lockdownd7 Posted September 24, 2009 Author Share Posted September 24, 2009 That's perfect. Exactly what I needed. Quote Link to comment https://forums.phpfreaks.com/topic/175270-solved-how-to-emulate-javascript-onclick/#findComment-924252 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.