Sometimes the GET method is not very appreciated because it puts in the URL (query string) all information that is passed. In case of logging there is a security risk: all the GET imformation is logged. Also some user could change the query string by hand and you'll have to work it out.
Well, in most of the cases youll want to use the POST method. Take this example:
Code: Select all
<?
// Lets say you get employeeID from session...
// here I will just assign a value to it:
$employeeID = 278;
// Just another var
$recordID = 5;
// Now we set an array of "options" for this example:
$optionsArray = array(
"Main Page" => "main.php",
"Edit Record" => "edit.php",
"Validate Password" => "validate.php",
"Send e-mail" => "mail.php",
"Sign Out" => "signout.php"
);
?>
<html>
<head>
<title>Test</title>
<script language="JavaScript1.2" type="text/javascript">
function submitForm (formObj, selTag) {
formObj.action = selTag.options[selTag.selectedIndex].value;
formObj.submit();
return;
}
</script>
</head>
<body>
<form name="form1" action="" method="POST">
<!-- We set some hidden inputs to store $employeeID and $recordID-->
<input type="hidden" name="employeeID" value="<?=$employeeID?>">
<input type="hidden" name="recordID" value="<?=$recordID?>">
<select name="menu" onchange="javascript:submitForm(form1, this);">
<?// Now lets "draw" the various <option> tags as many fileds in $optionsArray:
foreach ($optionsArray as $key => $value)
echo "\t" . '<option value="' . $value . '">' . $key . '</option>' . "\n"; // \t and \n for identation purposes
?>
</select>
</form>
</body>
</html>
Note I've changed the $optionsArray, the JavaScript function, the form tag (method and action) and added 2 hidden inputs to hold employeeID and recordID and pass it ahead via POST. Note also that the JavaScript function sets the form action on the fly and then tell it to be submited.
Here is the output:
Code: Select all
<html>
<head>
<title>Test</title>
<script language="JavaScript1.2" type="text/javascript">
function submitForm (formObj, selTag) {
formObj.action = selTag.optionsїselTag.selectedIndex].value;
formObj.submit();
return;
}
</script>
</head>
<body>
<form name="form1" action="" method="POST">
<!-- We set some hidden inputs to store $employeeID and $recordID-->
<input type="hidden" name="employeeID" value="278">
<input type="hidden" name="recordID" value="5">
<select name="menu" onchange="javascript:submitForm(form1, this);">
<option value="main.php">Main Page</option>
<option value="edit.php">Edit Record</option>
<option value="validate.php">Validate Password</option>
<option value="mail.php">Send e-mail</option>
<option value="signout.php">Sign Out</option>
</select>
</form>
</body>
</html>
Note: for form.submit() works (in fact for it to exists in memory) there should be no <input name="submit"> in your form. Otherwise you'll get and error saying it is not a function.
Best regards,
Scorphus.