Page 1 of 1

Form Combobox multiple links...

Posted: Wed Oct 01, 2003 8:29 pm
by 3.14
Ok, heres the deal:

Code: Select all

<?php
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">

<select name="options">
<option value="Option1">Page 1</option>
<option value="Option2">Page 2</option>
<option value="Option3">Page 3</option>
<option value="Option4">Page 4</option>
<option value="Option5">Page 5</option>
<option value="Option6">Page 6</option>
</select>
<input type="submit" value="Go">

</form>
?>
How do I get the above code to navigate to the desired page (assuming each option directs the user to a separate page)? I realise the

Code: Select all

<?php
$_SERVER['PHP_SELF'] ?>
isn't correct, but it works for demonstration purposes.

Posted: Wed Oct 01, 2003 8:39 pm
by JAM
Something like this will give you ideas to work with.

Code: Select all

<?php
if (isset($_POST['options'])) {
    echo $_POST['options'].'.php';
}
?>

Posted: Wed Oct 01, 2003 9:51 pm
by 3.14
Ok,

Code: Select all

<?php
if (isset($_POST['options'])) { 
    echo $_POST['options'].'.php'; 
}
?>
This outputs the various options such as:

option1.php
option2.php
etc

So I changed the following:

Code: Select all

<?php
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST"> 
?>
into

Code: Select all

<?php
<form action="<?= $_POST['options'] . '.php' ?>" method="POST">
?>
But alas, every time I click on any particular option it comes up with .php but not the option ahead of it, thus not taking me to the page.

What other possible hint can you give me?

Posted: Wed Oct 01, 2003 10:11 pm
by scorphus
JavaScript could be a nice solution:

Code: Select all

<html>
<head>
<title>Test</title>
<script language="JavaScript1.2" type="text/javascript">
function changeLocation (selTag) &#123;
	document.location = selTag.options&#1111;selTag.selectedIndex].value;
	return;
&#125;
</script>
</head>

<body>

<form name="form1">
	<select name="menu" onchange="changeLocation(this)">
		<option value="option1.php">Option1</option>
		<option value="option2.php">Option2</option>
		<option value="option3.php">Option3</option>
	</select>
</form>

</body>
</html>
Hope it helps,
Scorphus.

Posted: Thu Oct 02, 2003 9:29 am
by JAM
Well, using $_POST that way wont work, as the php parsed at that time hasn't seen any form information. You might be able to use the header() in redirecting users based on the forms values.

Code: Select all

<?php
if (isset($_POST['options'])) {
    header('Location: '.$_POST['options'].'.php');
}
?>

Posted: Thu Oct 02, 2003 6:30 pm
by 3.14
Javascript works perfectly. I'd rather try and get the php combobox working, but I don't quite understand how it works. :?

I tried using the...

Code: Select all

<?phpif (isset($_POST['options'])) { 
    header('Location: '.$_POST['options'].'.php'); 
} 
?>
But it wasn't echoing the correct (or any for that matter) option.php to the address bar to take the user to the next page.

:?: :?: :?:

Posted: Thu Oct 02, 2003 6:36 pm
by 3.14
8O Oh crap... just realised, the javascript isn't going to help me.

This is because my php page shows one record at a time. The menu down the bottom is for options to edit that particular record. If I use the javascript, it will take the user to a general page and not a user specific page. So I will need to pass a employeeID variable through the selection as well.

Something tells me I should have probably just made a table with some hyperlinks. *sigh* :?

Posted: Thu Oct 02, 2003 11:19 pm
by scorphus
I think there is no problem at all. Tell PHP to write the <select>'s <option> s tags. See an 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?recordID=$recordID",
	"Validate Password" => "validate.php?employeeID=$employeeID",
	"Send e-mail" => "mail.php",
	"Sign Out" => "signout.php"
);
?>
<html>
<head>
<title>Test</title>
<script language="JavaScript1.2" type="text/javascript">
function changeLocation (selTag) {
	document.location = selTag.options[selTag.selectedIndex].value;
	return;
}
</script>
</head>

<body>

<form name="form1">
<select name="menu" onchange="changeLocation(this)">
<?// Now let's "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>
The above code outputs:

Code: Select all

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Test&lt;/title&gt;
&lt;script language="JavaScript1.2" type="text/javascript"&gt;
function changeLocation (selTag) &#123;
	document.location = selTag.options&#1111;selTag.selectedIndex].value + ;
	return;
&#125;
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;form name="form1"&gt;
&lt;select name="menu" onchange="changeLocation(this)"&gt;
	&lt;option value="main.php"&gt;Main Page&lt;/option&gt;
	&lt;option value="edit.php?recordID=5"&gt;Edit Record&lt;/option&gt;
	&lt;option value="validate.php?employeeID=278"&gt;Validate Password&lt;/option&gt;
	&lt;option value="mail.php"&gt;Send e-mail&lt;/option&gt;
	&lt;option value="signout.php"&gt;Sign Out&lt;/option&gt;
&lt;/select&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
This is just an illustration to give you an idea of what can be done.

Hope it helps. Cheers,
Scorphus.

Posted: Thu Oct 02, 2003 11:41 pm
by 3.14
scorphus you legend. That works perfectly. Plus you've not taught me how to pass variables with the ?variablename=$variablename.

Marvellous I say!!! :D

Posted: Fri Oct 03, 2003 12:27 am
by scorphus
I'm glad it helps! :)
Plus you've not taught me how to pass variables with the ?variablename=$variablename.
This method of passing variables is called GET. You can access every variable passed by GET using the predefined variable $_GET. For example, take showvars.php:

Code: Select all

<?
print_r($_GET);
?>
Calling showvar.php?name=maria&status=married&foo=bar results:

Code: Select all

Array
(
    &#1111;name] =&gt; maria
    &#1111;status] =&gt; married
    &#1111;foo] =&gt; bar
)
Another way is using the import_request_variables() function. New showvars.php:

Code: Select all

<?
print_r($_GET);

import_request_variables('g', 'get_');

echo $get_name . "\n";
echo $get_status . "\n";
echo $get_foo . "\n";
?>
The same call results:

Code: Select all

Array
(
    &#1111;name] =&gt; maria
    &#1111;status] =&gt; married
    &#1111;foo] =&gt; bar
)
maria
married
bar
Just choose the way you like most.

Regards,
Scorphus.

Posted: Fri Oct 03, 2003 12:59 am
by scorphus
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

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Test&lt;/title&gt;
&lt;script language="JavaScript1.2" type="text/javascript"&gt;
function submitForm (formObj, selTag) &#123;
	formObj.action = selTag.options&#1111;selTag.selectedIndex].value;
	formObj.submit();
	return;
&#125;
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;form name="form1" action="" method="POST"&gt;
&lt;!-- We set some hidden inputs to store $employeeID and $recordID--&gt;
&lt;input type="hidden" name="employeeID" value="278"&gt;
&lt;input type="hidden" name="recordID" value="5"&gt;
&lt;select name="menu" onchange="javascript:submitForm(form1, this);"&gt;
	&lt;option value="main.php"&gt;Main Page&lt;/option&gt;
	&lt;option value="edit.php"&gt;Edit Record&lt;/option&gt;
	&lt;option value="validate.php"&gt;Validate Password&lt;/option&gt;
	&lt;option value="mail.php"&gt;Send e-mail&lt;/option&gt;
	&lt;option value="signout.php"&gt;Sign Out&lt;/option&gt;
&lt;/select&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
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.

Posted: Fri Oct 03, 2003 1:09 am
by 3.14
Plus you've not taught me how to pass variables with the ?variablename=$variablename.
slight correction, I meant "now" instead of "not"


But your extended definition has actually helped even more. Thanks 8)