Page 1 of 1

PHP jump menu (without JavaScript??)

Posted: Mon Aug 25, 2008 12:14 pm
by duno
Hey guys,

I can't for the life of me find help on this anywhere so thought I'd try my luck here! So here goes...

ISSUES:

First issue:

I have created a simple jump menu to dynamically switch content via the use of php variables. But the jump menu itself relies on JavaScript.

Is there any way of creating a PHP replacement for the Javascript? Currently if Javascript is turned off, the jump menu doesn't work so that's no good for the users.

Second issue:

When you select a link from the jump menu, lets say 'Link 02', it works fine and dynamically displays the Link 02 content via the use of variables but then the jump menu list switches back to LINK rather than showing the current selected item, which should be 'Link 02' in this case.

I know you can simply use the "selected="selected" value but I need a way to add this value dynamically only when a particular link from the jump menu has actually been selected. I presume the use of a simple <?php echo 'selected="selected"'; ?> can be added to the jump menu item but it needs to be echoed only IF a particular item is selected so the use of a conditional statement might be in order but I'm pretty new to PHP so its proving difficult. Would appreciate a hand?

CODE:

Template.php (main page and without additional HTML markup):

Code: Select all

<?php
//Include menu
include('menu.php');
 
//Get the page variable
$page = $_GET['page'];
 
//Our switch statement to get the right content
switch($page) {
 
case "01":
$content = "01.html";
break;
 
case "02":
$content = "02.html";
break;
 
case "03":
$content = "03.html";
break;
 
case "04":
$content = "04.html";
break;
 
default: //If the variable didn't match any of the above cases do this.
$content = "01.html";
break;
}
 
//Include the selected content.
include($content);
 
?>
Menu.php (the jump menu itself is pretty simple and is included via a php include):

Code: Select all

<form>
<select name="LINK" onChange="jumpMenu('parent',this,0)">
<option value="template02.php">LINK</option>
<option value="template02.php?page=01">Link 01</option>
<option value="template02.php?page=02">Link 02</option>
<option value="template02.php?page=03">Link 03</option>
<option value="template02.php?page=04">Link 04</option>
</select>
</form>
The JavaScript used to make the jump menu function (created automatically via Dreamweaver) is:

Code: Select all

<!-- JUMP MENU
function jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
 
If any of you guys could shed some light on this or help out or even point me in the right direction I would really appreciate it.

Cheers in advance,

Darren

Re: PHP jump menu (without JavaScript??)

Posted: Mon Aug 25, 2008 1:54 pm
by onion2k
Give the form an action, say 'jump.php', then make a script called jump.php that redirects the user to whatever $_GET['LINK'] is. Eg

Code: Select all

<?php
 
header("Location: ".$_GET['LINK']);
exit;
 
You might what to think about the security of that though. Some validation or something. People could send someone a link to your site like "www.yoursite.com/jump.php?LINK=http://www.porn.com/" and it'd look like you're redirecting people to some dodgy URL.

Re: PHP jump menu (without JavaScript??)

Posted: Mon Aug 25, 2008 1:57 pm
by califdon
Darren, for your information and use in future postings, please enclose all PHP code within BBcode tags like [ code=php] ... and ... [ /code] so it is more readable.

You need to understand that Javascript is a client side language that is run in the browser (yes, that's how users can disable it). PHP is a server side language that is run at the server, before the resulting script is sent to the browser. Therefore, PHP can do nothing about any user actions because it is no longer running (it is not even present) by the time the browser receives the page.

Re: PHP jump menu (without JavaScript??)

Posted: Mon Aug 25, 2008 2:22 pm
by Ziq
Maybe something like this?

menu.php

Code: Select all

 
<form action="template02.php" name="linker">
<select name="page" onchange="document.forms.linker.submit()">
    <option value="01"<?if ($_GET['page'] == '01') echo ' selected'?>>Link 01</option>
    <option value="02"<?if ($_GET['page'] == '02') echo ' selected'?>>Link 02</option>
    <option value="03"<?if ($_GET['page'] == '03') echo ' selected'?>>Link 03</option>
    <option value="04"<?if ($_GET['page'] == '04') echo ' selected'?>>Link 04</option>
    <option value="05"<?if ($_GET['page'] == '05') echo ' selected'?>>Link 05</option>    
</select>
<input type="submit" value="go">
</form>
 

Re: PHP jump menu (without JavaScript??)

Posted: Mon Aug 25, 2008 2:36 pm
by Zoxive
Ziq wrote:Maybe something like this?

menu.php

Code: Select all

 
<form action="template02.php" name="linker">
<select name="page" onchange="document.forms.linker.submit()">
    <option value="01"<?if ($_GET['page'] == '01') echo ' selected'?>>Link 01</option>
    <option value="02"<?if ($_GET['page'] == '02') echo ' selected'?>>Link 02</option>
    <option value="03"<?if ($_GET['page'] == '03') echo ' selected'?>>Link 03</option>
    <option value="04"<?if ($_GET['page'] == '04') echo ' selected'?>>Link 04</option>
    <option value="05"<?if ($_GET['page'] == '05') echo ' selected'?>>Link 05</option>    
</select>
<input type="submit" value="go">
</form>
 
That example still requires JavaScript (onchange). The only way without JavaScript is if you have a submit button to click after you choose your option.

Edit: blah, just skimmed over it, so if javascript is turned off don't expect it to auto-submit when you choose your link then. :lol:

Re: PHP jump menu (without JavaScript??)

Posted: Mon Aug 25, 2008 2:42 pm
by onion2k
Zoxive wrote:That example still requires JavaScript (onchange). The only way without JavaScript is if you have a submit button to click after you choose your option.
It does have a submit button. :twisted:

Re: PHP jump menu (without JavaScript??)

Posted: Mon Aug 25, 2008 2:50 pm
by RobertGonzalez
You are going to need to handle this on the server. My recommendation is to create what you want on the server first then enhance it with JS. That way you know it works without question.

Re: PHP jump menu (without JavaScript??)

Posted: Mon Aug 25, 2008 3:39 pm
by duno
Thanks ever so much for the responses guys :)

@ onion2k Tested this and it works very well thanks, only thing I don't like is that it requires a submit button...and I'm not sure I want to use a submit button :S

Seems like a catch 22 situation since califdon's comment seems to rule out a PHP replacement for the JavaScript due to the client-side/server-side issue (thanks for clearing that up by the way, califdon, it makes much more sense now) which is a shame as I am not a fan of using JavaScript.

@ Ziq Also tried this and it works great so thanks a lot for that :) One down, one to go!

Using the JavaScript version might not be so bad anyway, since the reason for the jump menu is simply to "filter" some of the images in my portfolio but the full list of images will be shown on a single page anyway so the user can access the image, filtered or not, either way - if that makes sense!

Which brings me to another point that I hope you genius's can help me with...I've expanded the jump menu (while awaiting some advice from this forum) so there are now four jump menus consisting of Client, Industry, Platform and Service where users can select from any of the jump menus to dynamically display the relevant content - basically filtering and narrowing down the images shown in a portfolio (template.php).

Again this works beautifully, the only issue is that all four content includes are displayed on the page at the same time when I only want one include to be displayed at any given time, with this content include area dynamically changing according the the selection made by the users from the four jump menus.

Basically it needs something like a conditional statement to display the relevant include depending on what the user selects from the jump menu/s. But since the code is a switch statement, it is more or less alsready an if else statement hence why I can't seem to work it out. I think I've done okay for a PHP novice mind you! : P Ha

I hope this makes sense? Let me know if it doesn't.

Anyway here is the code:

Template.php

Code: Select all

 
<?php
//Include page header
include('menu.php');
 
/////////////////////////////////////////FIRST JUMP MENU CONTENT///////////////////////////////////////////////////
 
//Get the page variable
$client = $_GET['client'];
 
//Our switch statement to get the right content
switch($client) {
 
case "c01": //For each page of content just add a case block
$contentc = "c01.html"; //The file with the page contents. DO not include the header and footer HTML in these files it is already added.
break; //Stop the case block, if you do not have this you will get an error.
 
case "c02":
$contentc = "c02.html";
break;
 
case "c03":
$contentc = "c03.html";
break;
 
case "c04":
$contentc = "c04.html";
break;
 
default: //If the variable didn't match any of the above cases do this.
$contentc = "c01.html";
break;
}
 
//Include the selected content.
include($contentc);
 
/////////////////////////////////////////SECOND JUMP MENU CONTENT///////////////////////////////////////////////////
 
$industry = $_GET['industry'];
 
switch($industry) {
 
case "i01": //For each page of content just add a case block
$contenti = "i01.html"; //The file with the page contents. DO not include the header and footer HTML in these files it is already added.
break; //Stop the case block, if you do not have this you will get an error.
 
case "i02":
$contenti = "i02.html";
break;
 
case "i03":
$contenti = "i03.html";
break;
 
case "i04":
$contenti = "i04.html";
break;
 
default: //If the variable didn't match any of the above cases do this.
$contenti = "i01.html";
break;
}
 
include($contenti);
 
/////////////////////////////////////////THIRD JUMP MENU CONTENT///////////////////////////////////////////////////
 
$platform = $_GET['platform'];
 
switch($platform) {
 
case "p01": //For each page of content just add a case block
$contentp = "p01.html"; //The file with the page contents. DO not include the header and footer HTML in these files it is already added.
break; //Stop the case block, if you do not have this you will get an error.
 
case "p02":
$contentp = "p02.html";
break;
 
case "p03":
$contentp = "p03.html";
break;
 
case "p04":
$contentp = "p04.html";
break;
 
default: //If the variable didn't match any of the above cases do this.
$contentp = "p01.html";
break;
}
 
include($contentp);
 
/////////////////////////////////////////FOURTH JUMP MENU CONTENT///////////////////////////////////////////////////
 
$service = $_GET['service'];
 
switch($service) {
 
case "s01": //For each page of content just add a case block
$contents = "s01.html"; //The file with the page contents. DO not include the header and footer HTML in these files it is already added.
break; //Stop the case block, if you do not have this you will get an error.
 
case "s02":
$contents = "s02.html";
break;
 
case "s03":
$contents = "s03.html";
break;
 
case "s04":
$contents = "s04.html";
break;
 
default: //If the variable didn't match any of the above cases do this.
$contents = "s01.html";
break;
}
 
include($contents);
 
?>
 
menu.php

Code: Select all

<p>How would you like to filter the projects?</p>
<p>By
<form name="form1">
  <select name="CLIENT" onChange="jumpMenu('parent',this,0)">
    <option value="template03.php" <?php if ($_GET['client'] == 'template03.php') echo 'selected' ?>>CLIENT</option>
    <option value="template03.php?client=c01" <?php if ($_GET['client'] == 'c01') echo 'selected' ?>>Client 01</option>
    <option value="template03.php?client=c02" <?php if ($_GET['client'] == 'c02') echo 'selected' ?>>Client 02</option>
    <option value="template03.php?client=c03" <?php if ($_GET['client'] == 'c03') echo 'selected' ?>>Client 03</option>
    <option value="template03.php?client=c04" <?php if ($_GET['client'] == 'c04') echo 'selected' ?>>Client 04</option>
  </select> 
  </form>
  or 
  <form name="form2">
  <select name="INDUSTRY" onChange="jumpMenu('parent',this,0)">
    <option value="template03.php" <?php if ($_GET['industry'] == 'template03.php') echo 'selected' ?>>INDUSTRY</option>
    <option value="template03.php?industry=i01" <?php if ($_GET['industry'] == 'i01') echo 'selected' ?>>Industry 01</option>
    <option value="template03.php?industry=i02" <?php if ($_GET['industry'] == 'i02') echo 'selected' ?>>Industry 02</option>
    <option value="template03.php?industry=i03" <?php if ($_GET['industry'] == 'i03') echo 'selected' ?>>Industry 03</option>
    <option value="template03.php?industry=i04" <?php if ($_GET['industry'] == 'i04') echo 'selected' ?>>Industry 04</option>
  </select> 
  </form>
  or
  <form name="form3">
  <select name="PLATFORM" onChange="jumpMenu('parent',this,0)">
    <option value="template03.php" <?php if ($_GET['platform'] == 'template03.php') echo 'selected' ?>>PLATFORM</option>
    <option value="template03.php?platform=p01" <?php if ($_GET['platform'] == 'p01') echo 'selected' ?>>Platform 01</option>
    <option value="template03.php?platform=p02" <?php if ($_GET['platform'] == 'p02') echo 'selected' ?>>Platform 02</option>
    <option value="template03.php?platform=p03" <?php if ($_GET['platform'] == 'p03') echo 'selected' ?>>Platform 03</option>
    <option value="template03.php?platform=p04" <?php if ($_GET['platform'] == 'p04') echo 'selected' ?>>Platform 04</option>
  </select> 
  </form>
  or
  <form name="form4">
  <select name="SERVICE" onChange="jumpMenu('parent',this,0)">
    <option value="template03.php" <?php if ($_GET['service'] == 'template03.php') echo 'selected' ?>>SERVICE</option>
    <option value="template03.php?service=s01" <?php if ($_GET['service'] == 's01') echo 'selected' ?>>Service 01</option>
    <option value="template03.php?service=s02" <?php if ($_GET['service'] == 's02') echo 'selected' ?>>Service 02</option>
    <option value="template03.php?service=s03" <?php if ($_GET['service'] == 's03') echo 'selected' ?>>Service 03</option>
    <option value="template03.php?service=s04" <?php if ($_GET['service'] == 's04') echo 'selected' ?>>Service 04</option>
  </select> 
</form>
I know the way I have done it is probably over-complicated but hey - if it ain't broken, don't fix it! : P let me know what you guys think nonetheless.

Thanks again guys, your help is appreciated.

Darren

Re: PHP jump menu (without JavaScript??)

Posted: Mon Aug 25, 2008 4:21 pm
by RobertGonzalez
So you are ok with everything not working is someone has JS turned off?

Re: PHP jump menu (without JavaScript??)

Posted: Mon Aug 25, 2008 5:25 pm
by duno
So you are ok with everything not working is someone has JS turned off?
Not exactly, I try to avoid the use of JavaScript where I can, hence my post here. But as I described above, everything is still accessible whether the user has JavaScript turned on or off as the jump menu is simply to 'filter' the content should a user be looking for a specific content area. But technically the jump menu will therefore not work so you are correct. It will just mean the user has to scroll further down to find what they are looking for.

I'm still trying to solve my latest issue (see latest post above) so once I have resolved this, I might simply go with the submit button that a few of you guys have mentioned and all will be resolved since JavaScript will therefore not be required.

Alternatively, I can simply remove the form elements and incorporate the links into a list that can be 'dropped down' via the old css while still retaining my PHP switch code etc.

Cheers,

Darren