ruddie Posted January 18, 2011 Share Posted January 18, 2011 Hi, I got a little problem with calling a PHP function from a href. I got my site all set up, and am calling the function like this: <?php echo '<a href="'.changeRecentPosts().'" class="image">' ?> This does indeed work, but it calls the function every time I refresh my page.. Which isn't the effect I was trying to achieve. I tried to change it to : <?php echo '<a href="#" onClick="'.changeRecentPosts().'" class="image">' ?> But that didn't work either. It also seems it only calls it when I refresh my page, but never when I click it (I guess because PHP is handeled before HTML or something similar?). If you can think of any way (even in AJAX, or whatever other language...) feel free to, I think I can figure it all out. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/224815-php-function-calling-in/ Share on other sites More sharing options...
trq Posted January 18, 2011 Share Posted January 18, 2011 onClick events are client side, php executes server-side. The only way you can do this is to have your onClick event trigger a Javascript function that in turn makes an Ajax request. Quote Link to comment https://forums.phpfreaks.com/topic/224815-php-function-calling-in/#findComment-1161231 Share on other sites More sharing options...
ruddie Posted January 18, 2011 Author Share Posted January 18, 2011 I already assumed something like that, but I am not certain how to do as such. I will try googleing a bit, and I'll get back to you Quote Link to comment https://forums.phpfreaks.com/topic/224815-php-function-calling-in/#findComment-1161265 Share on other sites More sharing options...
trq Posted January 18, 2011 Share Posted January 18, 2011 Check out the jQuery framework. It'll make your life alot easier. http://api.jquery.com/jQuery.ajax/ Quote Link to comment https://forums.phpfreaks.com/topic/224815-php-function-calling-in/#findComment-1161266 Share on other sites More sharing options...
cyberRobot Posted January 18, 2011 Share Posted January 18, 2011 What is "changeRecentPosts()" supposed to do? Quote Link to comment https://forums.phpfreaks.com/topic/224815-php-function-calling-in/#findComment-1161343 Share on other sites More sharing options...
ruddie Posted January 18, 2011 Author Share Posted January 18, 2011 What is "changeRecentPosts()" supposed to do? It is supposed to minimalize/maximalize (show or not show) a div that uses PHP to collect a bit of data from my database. So basically, it just adds a cookie that says whether it is active or not. Quote Link to comment https://forums.phpfreaks.com/topic/224815-php-function-calling-in/#findComment-1161391 Share on other sites More sharing options...
cyberRobot Posted January 18, 2011 Share Posted January 18, 2011 You could use some Javascript and CSS to hide the <div>. Add some CSS to the page: <style type="text/css"> .active { display:block; } .inactive { display:none; } </style> If you're <div> doesn't already have an ID, create one. You'll also need to default the class to "active" if you want it to be shown by default. For example: <div id="contentToBeHidden" class="active">...</div> Now you just need to create a JavaScript funtion to deal with the onclick (Example: <a href="#" onclick="changeRecentPosts()">Test</a>): <script type="text/javascript"> function changeRecentPosts() { //IF THE DIV IS CURRENTLY SHOWING, HIDE IT if(document.getElementById('contentToBeHidden').className == 'active') { document.getElementById('contentToBeHidden').className='inactive'; //ELSE...THE DIV IS HIDDEN, SHOW IT } else { document.getElementById('contentToBeHidden').className='active'; } } </script> Here my entire test script: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript"> function changeRecentPosts() { //IF THE DIV IS CURRENTLY SHOWING, HIDE IT if(document.getElementById('contentToBeHidden').className == 'active') { document.getElementById('contentToBeHidden').className='inactive'; //ELSE...THE DIV IS HIDDEN, SHOW IT } else { document.getElementById('contentToBeHidden').className='active'; } } </script> <style type="text/css"> .active { display:block; } .inactive { display:none; } </style> </head> <body> <p><a href="#" onclick="changeRecentPosts()">Test</a></p> <div id="contentToBeHidden" class="active">Content to be hidden</div> </body> </html> Let me know if you have any questions. Quote Link to comment https://forums.phpfreaks.com/topic/224815-php-function-calling-in/#findComment-1161408 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.