...

View Full Version : Help with menu and template concept



ScottInTexas
05-28-2007, 03:56 PM
I have my web site set up in a typical 2 column/header format. The left column being the location for a menu and other items. The right column carrying the content of the selected page. When I call index.php I have a check to see if the $_POST['submit'] was fired take the appropriate action by using an invisable field within any form the user might be filling out and in my menu so that everytime index is called I can check the menuaction and use a switch to perform the action.



if (isset($_POST['submit']))
switch ($_POST['menuaction']){
case 'login':
{
$SESSION['UserName']=$_POST['UserID'];
$SESSION['pswd']=md5($_POST['Password']);
break;
}
case 'urlrequest':
{
$thisPage=$POST['menuItem'];
break;
}
default:
$thisPage="Home";
}

So in the above case the third variable passed with $_POST would be "login" and the user would be set in the session. In another case it maybe "WriteMe" and the session set wouldn't go but the index.php would make the "Content" another form for contacting me.

For the login form I don't have a problem since it is a plain form. But, for the menu, I have to use javascript to insert a value into a hidden form field and then fire submit with code. But I put an onclick="menuOps" in the page and it's not calling it. I tried it in the <li tag and the <a tag. Then I have to set the hidden field and I can't seem to make that work either.



<script language="Javascript" type="text/javascript">
<!--
function menuops(choice){
var rqstPage;
var subbtn;
var frmField;
alert "Menuops called with " + choice;
frmField=document.getElementByID("menuForm");
frmField.menuaction.value=choice;
menuForm.submit;

}
//-->
</script>

<div id="menu">
<ul>
<li<?php if ($thisPage=="Home")
echo " id=\"currentpage\""; ?>>
<a href="#" onclick="menuops('Home')">Home</a></li>
<li<?php if ($thisPage=="Page Two")
echo " id=\"currentpage\""; ?>>
<a href="#">Services</a></li>
<li<?php if ($thisPage=="Page Three")
echo " id=\"currentpage\""; ?>>
<a href="#">Place Order</a></li>
<li<?php if ($thisPage=="Page Four")
echo " id=\"currentpage\""; ?>>
<a href="#">Contact Us</a></li>
</ul>
</div>
<div id="menuFormDiv" visible="false">
<form id="menuForm">
<input type="hidden" name="menuaction">
<input type="submit" value="" name="submit">
</form>
</div>


Sorry for the lengthy post but I wanted you to have everything you needed to make an informed response.

Do you see a better way? Or can you point out my obvious errors?

iLLin
05-28-2007, 05:43 PM
Your javascript seems screwy:

menuForm.submit;

That should be

document.menuForm.submit

In your switch the case: login, does not have a var $thisPage. Should it need one?

ScottInTexas
05-28-2007, 06:50 PM
No I don't need $thispage in login since it is a straight forward process and the page is not going to change, just the status of the viewer. $thispage is used to highlight the menu option showing whcich page you are on.

My javascript is screwy. It's not firing at all. The error says "Object expected on line 61." Can't really tell what line I'm looking at, but the alert should have fired before any errors so it has to be in the function declaration. I can't see anything wrong with that.

iLLin
05-28-2007, 09:52 PM
Use firefox and it will debug js for you. Also install a plugin called firebug. That will help a lot with developing with js.

Ima try to clean up your function some with js.



function menuops(choice){
alert(choice); //for debugging
var menuForm = document.getElementByID('menuForm'); //your form
menuForm.menuaction.value=choice; //menuaction = name of field
menuForm.submit; //if this don't work use document.menuForm.submit;
}
</script>


If this doesn't work or you can't track your error. Post up your form. Also why are you using a form for this? I know I am limited on "exactly" why your doing this but I'm sure there is a better way than a form?

Also I'm not positive in this, but I guess you have <input type="submit" name="submit" value="Submit" /> for you submit button? I don't think that with using javascript to submit your form that it will "set" that button as if you were pressing that button. I never use the submit button as a reference if the form has been submitted or not. I use a hidden field and when the form is posted, its posted too. So if your still having trouble, try adding a hidden field <input type="hidden" name="submit" value="set" />.

Like I said I'm not positive.

ScottInTexas
05-29-2007, 01:11 AM
This turned out fine. I had some stupid syntax errors. The code looks like this.


function menuops(strchoice)
{
var frmField=document.getElementById? document.getElementById("menuForm"): document.all.menuForm;
frmField.menuaction.value=strchoice;
frmField.submit();
}
//-->
</script>

<div id="menu">
<ul>
<li<?php if ($thisPage=="Home")
echo " id=\"currentpage\""; ?> onclick="menuops('Home')">
Home</li>
<li<?php if ($thisPage=="Register")
echo " id=\"currentpage\""; ?> onclick="menuops('Register')">
Register</li>
<li<?php if ($thisPage=="Page Three")
echo " id=\"currentpage\""; ?> onclick="menuops('Login')">
Login</li>
<li<?php if ($thisPage=="Page Four")
echo " id=\"currentpage\""; ?> onclick="menuops('Home')">
<a href="#">Contact Us</a></li>
</ul>
</div>
<div id="menuFormDiv" visible="false">
<form action="" id="menuForm" method="post">
<input type="hidden" name="menuaction">
</form>
</div>


Then in my index.php my switch picks it up and runs with it.

Thanks for your input.

iLLin
05-29-2007, 01:32 AM
Im curious on why you just dont do this?

index.php?menuaction=login
index.php?menuaction=urlrequest

You wouldn't have to deal with javascript as some have it turned off. But I code certain projects with js and I disable actions if they have it turned off.


I'm glad its working for you now.

ScottInTexas
05-29-2007, 07:21 PM
I don't know what you mean. I can just issue the command "index.php?menuaction" from within the code? Don't I have to have something 'fire' in order to have a header sent with the post information?

iLLin
05-29-2007, 09:40 PM
Dont use POST, use GET. If thats all your doing with a menu. Just seems less hassle than going through all that.

if(isset($_GET['menuaction'])) {...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum