Form Combobox multiple links...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
3.14
Forum Commoner
Posts: 28
Joined: Mon Sep 08, 2003 12:17 am

Form Combobox multiple links...

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Something like this will give you ideas to work with.

Code: Select all

<?php
if (isset($_POST['options'])) {
    echo $_POST['options'].'.php';
}
?>
3.14
Forum Commoner
Posts: 28
Joined: Mon Sep 08, 2003 12:17 am

Post 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?
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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');
}
?>
3.14
Forum Commoner
Posts: 28
Joined: Mon Sep 08, 2003 12:17 am

Post 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.

:?: :?: :?:
3.14
Forum Commoner
Posts: 28
Joined: Mon Sep 08, 2003 12:17 am

Post 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* :?
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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.
3.14
Forum Commoner
Posts: 28
Joined: Mon Sep 08, 2003 12:17 am

Post 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
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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.
3.14
Forum Commoner
Posts: 28
Joined: Mon Sep 08, 2003 12:17 am

Post 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)
Post Reply