Loading options in a select box after a previous selection

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

User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

f=document.getElementByID("form");
[...]
<form method='POST' name='form' action='#'>
The element has a name but not an id.
function send()
[...]
onchange="trimitere()"
document.getElementById("camp").value=.options[mylist.selectedIndex].text;
try

Code: Select all

<html>
	<head>
		<title>...</title>
		<script language="javascript">
			function trimitere()
			{
				mylist=document.getElementById("test1");
				mytext = document.getElementById("camp1")
				
				if ( null==mylist || null==mytext ) {
					alert("An error has occured, where sorry for the incovience!");
					return;
				}
						
				mytext.value = mylist.value; // options[mylist.selectedIndex].value;
			}
		</script>
	</head>        
	<body>
		<form method="POST" action="#">
			<select id="test1" onchange="trimitere()">
				<option value="Test 1" >Test 3</option>
				<option value="Test 2">Test 4</option>
			</select>
			<br />
			Result: <input type="text" id="camp1" value="" />
		</form> 
	</body>
</html>
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Sorry about these, I was in a hurry to put the code, and I got distracted :
function send()
[...]
onchange="trimitere()"
Quote:
document.getElementById("camp").value=.options[mylist.selectedIndex].text;
Thanks man for all the help.
Now it works in the test page let' s see in the actual page how it works.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

It partially works for selected values and loading and displaying the others dropdown menus. There still some bugs to fix:
what if the user what' s to change in mid selection the category, problems with displaying the data selected in the menu.
And I still hadn' t made the part for retrieving data from cookies if the user comes from a product page and wants to compare it with another.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

I don' t know how to display the selected value after it' s been selected and the form has been submitted i.e. user selects name of the category from the dropdown menu, the form is submited through JavaScript because of the onchange event in the dropdown menu and the user see the selected value in the menu and the new menus that appeared because of his selection.
EDIT: I don' t have any good ideas for doing this: I tried echoing the selected value with PHP, using the selected property with JS but after the reloading the page it switch to the first value in the menu.
Any ideas, snippets of code (JS or PHP) will be very much appreciated
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

If you mark an option as <option selected="selected"> browsers show this option as current value of a select element.

As for the whole topic: viewtopic.php?t=55508#309978
bokehman wrote:Just in case you want to Google it, the proper terminology is a "chained select".
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Thank you once again for the help. Need time to process the information given and then to finally finish this page.
Post Reply