Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop down menu Simple question

Status
Not open for further replies.

sanjdhiman

Programmer
Jan 15, 2003
189
GB
hi there all,

I have a quick question.
What i want to do is have a webpage where when i click on a drop down menu item and change the value in it for example

<select name=&quot;type&quot;>
<option value=&quot;1&quot;>For Sale</option>
<option value=&quot;2&quot;>To Let</option>
</select>

if i choose &quot;To Let&quot; on the drop down menu, it will instantly on the same page give me a form with say the following:-

<select name=&quot;minpric&quot;>

<option value=&quot;1&quot;>10000</option>
<option value=&quot;2&quot;>100000</option>
</select>

and if they selected the other (For Sale) then it would show the following on the same page


<select name=&quot;minpric&quot;>

<option value=&quot;1&quot;>10000000</option>
<option value=&quot;2&quot;>1000000000</option>
</select>

Can anyone help?

I think its a bit of javascript but not sure how to do it

Thanks in advance

Sanj
 
Have an event trigger from the list being changed and call a function to write the extra code to the browser. Only problem is if the user then selects another one. You could have the screen cleared (forgot the command of the top of my head) and then re-write everything to it from js with the option they selected now set as the default for the list to keep the continuity.
 
ok, have u got some sample code to show me how this is done. im new to php and not totally familar with javascript either?

thanks in advance
 
I used to do javascript, but I'm out of practice now, I know this can be done because I did something similar, but I can't remember how anymore.
 
do u know where to look to get this info..

oh and if i have a multi-select drop down menu and need to get al lthe values (the whole array) how would i get the whole array to display e.g.

<select name=features size=4 mulitple>
<option value=gch>Gas Central Heating</option>
<option value=garage>Garage</option>
....
...

on the next page i echo the variable $features (the name of the drop down) but only gives me last value in the list.

how do i get it to give me all the values?

thanks in advance

sanj
 
Word of advice... You can't use the JavaScript solution if you are using PHP to populate any of the select boxes. PHP evaluates all at once before the page is loaded. A JS event trigger may generate more PHP code but it will not be evaluated. If you're funneling this through PHP you need to set up a series of conditions to see if each preceeding variable has been set and just submit the form back to itself. I'll explain that if need be, but if you're not using PHP to populate the boxes then it doesn't matter.
 
Hi there yea im generating what goes into the drop down menu. what i need it to do is to show info on what ever is in the drop down when selected..

how do u do this u spoke about re-submitting the PHP agpage again

thanaks

sanj

(im a beginner)
 
There are a number of ways to do this. It would be a whole lot easier using includes, but if you're a beginner then I had better stick to the basics. Just please bear in mind that this is not the most efficient method. Once you get a handle on what is actually happening you can go through and clean it up quite a bit.

Let's say your page is called homes.php and you want three select boxes each generated by php. The boxes will be, in order, htype, hmin, and hmax. The first thing you want to do is create a variable to hold the destination of the form. Let's call it $act.

if(!isset $hmin)
{$act = &quot;processform.php&quot;;}
else
{$act = &quot;homes.php&quot;;}

This will make sense later.

As the form is generated you are going to use $act to set the action for your form.

echo &quot;<form name=\&quot;homes\&quot; action=\&quot;&quot;.$act.&quot;\&quot;>

Now we have it set up so that if the user has made a selection in all three boxes they will be taken to processform.php when they click the submit button. However, if they have not completed all three boxes then the form will just take them back to itself. That was actually the hard part. The form itself is simple. You do have to decide, though, if you want to show blank boxes or no boxes at all as the user proceeds through the form. My preference is to just omit the empty boxes entirely and add them as they are needed, but you can do it either way. I'll assume you're using blank boxes.

When you go to generate the first box, htype, you first check to see if that has already been set. If it has you use the value selected as the option selected generate the rest of the box before you proceed on down. This allows users to change their mind without using the BACK button. If not, then you set it up as normal.

if(isset $htype)
{ code to generate the htype select box with the value of $htype as the option selected }
else
{generate the htype select box}

You would go down to the $min box. You will again check to see if $htype is set. If it is set you will populate the box for min based on the value of $htype. If not, you can leave it blank. Like I said, if it were me I would not even generate the empty box if it they had not yet selecte a type.

if(isset $htype)
{ generate the values for the second box, min, based on the value selected in the first box. You also want to check to see if $hmin has already been set and use it as option selected if has been set.}
else
{ do whatever }

Then, for the final box, you check to see if $hmin has been set.

if(isset $hmin)
{ generate the values for the third box, max, based on the value selected in the second box. Remember that $hmin is now set, so when the user makes a selection for the max box the form will submit to your proccessform.php or whatever you called the processing page back at the top. }
else
{do whatever}

Here's how it works. The user hits the page homes.php the first time, $hmax is not set so the form action is generated as homes.php. $htype is not set so the &quot;type&quot; select box is generated as normal, and the min and max boxes are blank.

The user makes a selection from type. The form submits to itself. $hmax is still not set, but now $htype is set. Now the type select box is pre-set to the value the user just selected, and the min select box is populated with choices based on the type selection.

The user makes a selection from min. The form submits back to itself again and now both $htype and $hmin are set. Their previous selections are displayed and the max box is now full of appropriate choices as well. The form action is now set to the processing page.

The user makes a selection from max. The form submits to the processing page.

You have to remember to call the OnChange event handler in the select boxes as you generate them.

<select name=&quot;htype&quot; onChange=&quot;submit()&quot;>

Those are the basics. It's not the most clean or efficient, but it's a start. Once you learn a little more you can clean it up a lot. I would look at includes.

 
thanks i think i get it. but will let u know how it goes. hopefully i will get it to work..

thanks for the help

sanj
 
hi there i get the process to what ur tryin to achieve but finding it difficult to write the code for it..

i have made my first page the home.php
and made the process.php

i have created the select (drop down boxes) the three u stated to make in ur example

just finding it hard to put where the if statements go etc..

can u help?

thanks in advance

sanj
 
jimo...

Remote scripting would be one way to handle this, but probably not a solution for a beginner. Besides, you have browser support issues with iframes, and a lot of the other rpc solutions are really no faster than the one I described, which is a basic PHP solution with refreshes. Ideally, I would look at encapsulating the three scenarios in include statements and just running the appropriate include. Most browsers will store the majority of graphics etc in cache, so it's not as if you have to download every image again and again.

sanj,

Where you would put the statements would really depend on how you are serving the page. You need to determine if $hmin is set before you echo the form in order to set the action, but other than that you can just break out of the echo whenever you want to evaluate. I'll use a simple &quot;<p>&quot; to separate the boxes here as an example.

if(isset $hmin)
{$act = &quot;processform.php&quot;;}
else
{$act = &quot;homes.php&quot;;}

echo &quot;<form name=\&quot;h\&quot; action=\&quot;&quot;.$act.&quot;\&quot;>
<select name=\&quot;htype\&quot; onChange=\&quot;submit()\&quot;>&quot;;

if(isset $htype)
{echo &quot;<option SELECTED> &quot;.$htype;}

// Here is where you would put the code that generates the options for the htype select box. You should fill in the options rater $htype is set or not, so the only conditional statement is the one above that sets the pre-selected option to what the user has already chosen.//

echo &quot;</select><p>
<select name=\&quot;hmin\&quot; onChange=\&quot;submit()\&quot;>&quot;;

if(isset $htype)
{if(isset $hmin)
{echo &quot;<option SELECTED> &quot;.$hmin;}

// Here is where you would generate the contents of the hmin select box using the value in $htype. If $htype is not set the contents of this box will not be generated and it will be left empty. Be sure, though, to close the select tag AFTER you close the if() statement that checks to see if $htype is set.//

}
echo &quot;</select><p>
<select name=\&quot;hmax\&quot; onChange=\&quot;submit()\&quot;>&quot;;

if(isset $hmin)
{
// Here is where you generate the contents of hmax based on the values of $hmin and/or $htype. As you can see, it will not be generated unless both $hmin and $htype are already set. //
}
echo &quot;</select>&quot;;









 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top