Jump to content
ajoo

condiationally select / deselect menu buttons

Recommended Posts

Hi all,

I have the following menu :

    <li> <a href='index.php?page=page1'>Home</a> </li>
    <li> <a href='index.php?page=page2'>Games</a>  </li>
    <li> <a href='index.php?page=page3'>Gallery</a> </li>
    <li> <a href='index.php?page=page4'>Print</a></li>   

Of these, I want the Print menu to be disabled and invisible for all pages except when it is invoked via a button called View on the Gallery page. The landing page / URL in that case would be   index.php?page=page4

and I want the Print menu to be disabled (the link )and made invisible when the user navigates away from the Print page. 

I know I have to use something like the following for disabling the link but all variations i tried on it seem to fail. 

$(window).on('hashchange', function(e){

  $('a #page4').bind('click', function(e){
    e.preventDefault();
  });

.
.
.

});

Kindly help to resolve this.

Thanks !

Edited by ajoo

Share this post


Link to post
Share on other sites

Why are you doing this in Javascript? If you only want the menu to appear on certain pages then make your PHP only output the menu on certain pages.

Share this post


Link to post
Share on other sites

HI requinix,

Thanks for the reply. That's how i had left that page a long time ago with a $_SESSION['print'] for enabling and disabling the menu, but that did not seem to work when i came back to finishing the print page now. I'll look into my earlier logic and see if I can debug it and implement it using PHP.

Just for the sake of discussion, isn't using JQuery a good way to go about this ?

Thanks !

Share this post


Link to post
Share on other sites

jQuery is a tool. A means to an end. The question is whether Javascript is the right way to show/hide the menu, and I would say no.

Share this post


Link to post
Share on other sites

HI requinix,

Thanks for the reply

My menus are set on user login and depend upon the user login. Like separately for the admin and other users. The are set using a navbar and then the pages are included in the page area below it.

In order for the additional menu to display on a required page and not on any other I had thought i would use a session variable. However this would have to be set and evoked on a button submit which includes the printable page. Wouldn't I still have to use jQuery to set the value of the session variable on a button submit for the menu to be displayed on the landing page?

I know this is a different strategy from the one in the question above although it would still useJQ or JS. Is there a better all PHP way to do this ?

Thank you.

Share this post


Link to post
Share on other sites

If you have people log into your site then you have to use the session. That's how you know they're logged in when they go to different pages.

Maybe I'm not understanding exactly what you want to do.

A user is on a page, either Home or Games or Gallery. They don't see the Print link/menu. When do you want that Print link/menu to be visible? Under what conditions, and on what pages?

Share this post


Link to post
Share on other sites

Ya all right, I'll try and be clearer. 

When a user logsin say as an admin, his menu is set for all pages.

As of now he has this menu : HOME GAMES GALLERY

with submenus. There is one page print page that is invoked not  via menu but via a submit button on a form as below

User1 --- data1 --- data2 --- data3 ---- VIEW

User2 --- data4 --- data5 --- data6 ---- VIEW

.

.

UserN --- dataX --- dataY --- dataZ ---- VIEW

Now when the Admin presses the VIEW submit button , he lands an a page which can be printed and so now I want the menu on this page to be

HOME GAMES GALLERY  PRINT.

I feel that here when the button is submitted i have to use JS or JQ to set the session variable to print so that the following code can invoke the PRINT menu

 	<li> <a href='index.php?page=page1'>Home</a> </li>
    <li> <a href='index.php?page=page2'>Games</a>  </li>
    <li> <a href='index.php?page=page3'>Gallery</a> </li>

	<?php if(isset($_SESSION['print']) && $_SESSION['print'] == "print"): ?>
		<li> <a href='flogin.php?page=childprints'>Print</a> 
	<?php endif; ?>

Is that correct? 

I hope I have been able to make it clear. Also is there an all PHP way to do this ?

Thanks !

 

Share this post


Link to post
Share on other sites
2 minutes ago, ajoo said:

he lands an a page

Which means they went to a new page. Which means PHP got involved.

PHP knows the form was submitted, right? That means PHP can know whether the menu it prints for the page should have the Print option.

That code you have is close, in that you're wrapping the menu option in an if block. However the condition should not be on a session variable, which by definition is something that should persist between multiple pages, but on whether the form was submitted.

Share this post


Link to post
Share on other sites

Hi Requinix,

I really do wish that sometimes you would solve the problem without handing me out another one to work on !!😊

This

$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']

seems to be the way to go about it. Is this what you had in mind ? It does as required.

Thanks

Share this post


Link to post
Share on other sites

The way to check for the landing page URL and display the print menu accordingly.

Thanks.

Share this post


Link to post
Share on other sites

Oh, is this running from some included file?

Don't try to detect the page. Have the page tell you what to do. Could be as simple as

$SHOW_PRINT_MENU = true;
include "whatever.php";
	<?php if(!empty($SHOW_PRINT_MENU)): ?>
		<li> <a href='flogin.php?page=childprints'>Print</a> 
	<?php endif; ?>

Share this post


Link to post
Share on other sites

HI requinix,

I do not think that this, as you suggested, would work in my case requinix because my program structure and page flow is like this.
 

include "header.php";

include"navbar.html"    // --- The menus are decided and displayed here.

. . .

if(isset($_GET['page']){
$page = $_GET['page'];
  swicth($page)
  {
	//	all pages go here 
	//	...

      case "home":
      ...
      break;

      case "gallery":
      ...
      break;

      case "childprints"
      include("fra_prints.php");
      break;

      ...

	}
}

The menu is displayed before the page is included. So setting the variable in the relevant case comes after the menu is already displayed.

What do you think ? Is that correct or am i reading you wrong ?

Thanks !

Share this post


Link to post
Share on other sites

That's a problem: you need to alter the menu depending on the page, but you don't determine the page until after the menu has gone out.

I would say you should move the page display stuff into the page content stuff. index.php will serve as a "bootstrap", meaning it can include common files (database and such) and determine the page to load, but that's it. The page it includes controls what it displays - it is the one that includes the menu. Which then means, of course, you can do the variable thing.

Share this post


Link to post
Share on other sites

Hi requinix,

Thanks for the reply !

Would it be a problem if I instead check for the Page URL like i did earlier because your suggestion, must definitely be better, is a bit long drawn out it at this stage of the project.

If the above solution, of checking the url and displaying the menu, has no security implications I would rather use that for this page and maybe come back to it later and make the changes you recommend. That solution works fine but i don't know if there is a serious downside to it. 

Please let me know.

Thank you !

Share this post


Link to post
Share on other sites

Okay, but the longer you put it off the harder it will be to fix.

Duplicate the logic you already use to determine the page to show. You don't need SERVER_NAME (which is wrong anyways) or REQUEST_URI for this.

Share this post


Link to post
Share on other sites

hmmm, you see this is the last and the only one page of it's sort that requires this conditional menu appearance / disappearance, as of now, that I need to fix before I put up the application. So the question of longer really doesn't arise. I am at the last.

Quote

Duplicate the logic you already use to determine the page to show.

Could you please elaborate? If you are saying that I should do as I have been doing for the pages to display the menu so far, then that cannot work for this, because the menus are set for the user on login and remain the same for the length of the session. This print page is the only rogue page in that sense.

Quote

You don't need SERVER_NAME (which is wrong anyways) or REQUEST_URI for this.

Then how do I do it ?

Thanks loads !

Share this post


Link to post
Share on other sites

What are the precise conditions required for the print menu to appear?

Share this post


Link to post
Share on other sites

Hi requinix,

My answer #7 describe the precise conditions which I will put down again for your convenience.

I have a page layout like this

HOME GAMES GALLERY                                                     // Menu

User1 --- data1 --- data2 --- data3 ---- VIEW                       // Table with View Buttons. On page say Gallery.

User2 --- data4 --- data5 --- data6 ---- VIEW

.

.

UserN --- dataX --- dataY --- dataZ ---- VIEW

When the view submit button is pressed, the user lands on childprints page, which has information laid out for printing and the PRINT menu needs to appear when it lands on this page.

I hope that is clear. Please let me know if you need any further clarification.

Thank you.

Share this post


Link to post
Share on other sites

See, "when the button is pressed" doesn't tell me how you detect that. "User lands on the childprints page" doesn't either, but I assume it has to do with $_GET["page"].

Maybe another method.

Write code usable on the childprints page to detect whether the menu should be shown. Presumably covering the "when the button is pressed" condition. Then combine that code with the code on index.php that currently detects whether to show the childprints page. Then put that whole thing into your menu.

Share this post


Link to post
Share on other sites

ok I get what you want to know.

The View submit is a part of the form therefore

<form action='flogin.php?page=childprints' method='post' >

the action takes it to that page.

Then it falls through the code shown also shown previously user_pages.php which is included in flogin.php

include "header.php";

include"navbar.html"    // --- The menus are decided and displayed here.

. . .

if(isset($_GET['page']){
$page = $_GET['page'];
  swicth($page)
  {
	//	all pages go here 
	//	...

      case "home":
      ...
      break;

      case "gallery":
      ...
      break;

      case "childprints"
      include("fra_prints.php");
      break;

      ...

	}
}

So correct it has to d\go through the $_GET['page'];

Quote

Write code usable on the childprints page to detect whether the menu should be shown

hmmm With the menu already shown by navbar.php wouldn't the detection on childprints.php be futile ? The condition needs to be set before the navbar.php is encountered.

Quote

Presumably covering the "when the button is pressed" condition. Then combine that code with the code on index.php that currently detects whether to show the childprints page. Then put that whole thing into your menu.

Really sorry but i do not understand  what you mean by this. Maybe you can show me how to go about it.

Thanks.

 

 

 

Share this post


Link to post
Share on other sites
13 hours ago, ajoo said:

hmmm With the menu already shown by navbar.php wouldn't the detection on childprints.php be futile ? The condition needs to be set before the navbar.php is encountered.

It won't be deciding this value ahead of time. You're basically copying and pasting code. Whatever code childprints.php will use, you copy that into the menu.

13 hours ago, ajoo said:

Really sorry but i do not understand  what you mean by this. Maybe you can show me how to go about it.

You'll end up with

<?php if($_GET["page"] == "childprints" && /* the logic from childprints page */): ?>
	<li> <a href='flogin.php?page=childprints'>Print</a> 
<?php endif; ?>

I think. Suddenly I'm not sure about the page name anymore.

  • Like 1

Share this post


Link to post
Share on other sites

Hi requinix,

Oh great ! I wish you had told me in #7 to replace session with get, that would have solved it.

tI did occur to me to do so, but then I thought choosing

$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']

was a better option because here I got the complete URL for comparison. Simply using the GET can be manipulated because then someone can send $_GET['childprints'] from a different page and trigger the PRINT menu.

However you said that SERVER_NAME and REQUEST_URI was not a good idea. I wish you would tell me why that is not a good idea.

In any case I have dropped using the print menu altogether and have instead created a PRINT button on childprints which would eliminate the need to go this route altogether. However I thank you for the discussion which is always there for learning.

If you can answer the last question ( requested as a wish above ) I would be grateful.

Thank you.

 

 

 

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.