Jump to content

Hiding Showing Links


Go to solution Solved by Barand,

Recommended Posts

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 !

Link to comment
https://forums.phpfreaks.com/topic/295473-hiding-showing-links/
Share on other sites

  • Solution

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; ?>

 

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 !

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.

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 by ajoo
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.