ajoo Posted April 12, 2015 Share Posted April 12, 2015 Hi all ! The following piece of code, I believe , is supposed to hide the links when any of them is clicked. The links should reappear when the window is refreshed or reloaded. This however is not happening. Can someone please see the code and get it working. Also kindly explain the code action since I don't have much knowledge about JS or Jquery. The code: <?php //start the session session_start(); //set the attribute $_SESSION['hide'] = false; ?> <!DOCTYPE html> <html> <head> <script> //function to hide all class='test' elements function hide(h){ if(h){ $('.test').hide(); } else { $('.test').show(); } } /*do this always when page loads * verify with the value stored in session to hide or not the links */ window.onload = hide(<?php echo $_SESSION['hide']; ?>); //onready $(function() { //when link class='test' is clicked $('.test').click(function(){ //fadeOut or just $(this).hide(); $(this).hide(); //set the session to hide = true <?php $_SESSION['hide'] = true; ?> }); }); </script> </head> <body> <div class = "button"> <ul> <li> <a href="#" title="Link" class="test">I am link 1</a> </li> <li> <a href="#" title="Link" class="test">I am link 2</a> </li> <li> <a href="#" title="Link" class="test">I am link 3</a> </li> <li> <a href="#" title="Link" class="test">I am link 4</a> </li> </ul> </div> </body> </html> Thanks loads ! Quote Link to comment https://forums.phpfreaks.com/topic/295473-hiding-showing-links/ Share on other sites More sharing options...
Solution Barand Posted April 12, 2015 Solution Share Posted April 12, 2015 To use jquery you need the jquery library EG <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> Also PHP has finished by the time the javascript executes so you cannot use php expressions within javascript <?php $_SESSION['hide'] = true; ?> Quote Link to comment https://forums.phpfreaks.com/topic/295473-hiding-showing-links/#findComment-1508850 Share on other sites More sharing options...
ajoo Posted April 12, 2015 Author Share Posted April 12, 2015 Hi Guru Barand, Thank you very much for the help. Very silly of me to overlook the js library. I would like to ask that If we cannot use <?php $_SESSION['hide'] = true; ?> inside JS to notify php that the buttons are now hidden, then where should this information be inserted so that on a page refresh or reload this information may be looked up, even if only to check the state of the buttons. Thanks loads ! Quote Link to comment https://forums.phpfreaks.com/topic/295473-hiding-showing-links/#findComment-1508851 Share on other sites More sharing options...
Barand Posted April 12, 2015 Share Posted April 12, 2015 You said they should be visible when the page reloads so you know they start off visible. If you need to know the state store it in a JS variable (which would be initially set to "visible") and set to "hidden" when a link is clicked. Quote Link to comment https://forums.phpfreaks.com/topic/295473-hiding-showing-links/#findComment-1508853 Share on other sites More sharing options...
ajoo Posted April 13, 2015 Author Share Posted April 13, 2015 Thanks Guru Barand, One last thing, how to read this JS variable from within PHP to retrieve and use it's value ? Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/295473-hiding-showing-links/#findComment-1508877 Share on other sites More sharing options...
Barand Posted April 13, 2015 Share Posted April 13, 2015 If you are submitting form data you can put it in a hidden field, otherwise you can use AJAX call to pass it to the server Quote Link to comment https://forums.phpfreaks.com/topic/295473-hiding-showing-links/#findComment-1508889 Share on other sites More sharing options...
ajoo Posted April 13, 2015 Author Share Posted April 13, 2015 (edited) Hi Guru Barand, Thanks very much ! Please may I request you to kindly take a look at another of my earlier questions with the heading : 100+ Columns in a table. Good or Bad? Would be much obliged. Thanks again ! Edited April 13, 2015 by ajoo Quote Link to comment https://forums.phpfreaks.com/topic/295473-hiding-showing-links/#findComment-1508934 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.