Forum Moderators: coopster

Message Too Old, No Replies

2 drop down list and submit button in PHP

         

jaspreet

12:03 pm on Nov 29, 2003 (gmt 0)

10+ Year Member



Hi,
I have two drop down lists in a form on a PHP web page, the first drop down list contains different colors of a shirt, the second list contains sizes of the shirt. What I am looking for is that when a user selects a color from the first drop down list, using this a query is run on MySQL database and all relevant sizes are selected and the second drop down list is populated.

After the user selects the size and clicks on the submit button, a new page opens and the values of color and size are passed to a new page.

How do I implement this. I am looking for a sample code that could help me out. The main problem is that if after the color is selected and I run a query, I need to reload the same page with the first drop down list retaining the value of the selected color, and the second list populated with the sizes for that color. Further on clicking the submit button, a new page is to open. How can two different actions be done on the same form ,i.e on selection from first drop down list and on click of submit button

dcrombie

5:12 pm on Nov 29, 2003 (gmt 0)



You can't run an SQL query from JavaScript - the data is sitting on the web server and the only way to communicate with it is to reload the page (or call a new page).

One option might be to pre-load all the possible colour/size combinations into a JS array and use that to change the second option-list when the a colour is chosen.

;)

lorax

6:20 pm on Nov 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld jaspreet,

What I do in this situation is use javascript to initiate a form submit when the onChange event is triggered for the first select. The form action is the same page.

How can this be done? I'm not going to lay it all out for you but you can accomplish the goal by using if statements and checking for the existence of variables.

For example. Say your select box is named "color". When the page is first loaded I'd set the MySQL query for color within an if statement that checks for the existence of the var $color. If not then I'd run query and populate the first select box.

When the color is selected the var $color is set and passed to the form. SO this time $color exists and then I'd run the second query and populate the second select box.

Make sense?

Check out this PHP function:

isset [us3.php.net]

jaspreet

8:04 am on Nov 30, 2003 (gmt 0)

10+ Year Member



Thanks for the reply.

I know that I can populate the 'color' list when the page loads for the first time, and then when a color is selected, I can reload the same page and populate the 'size' list. But the problem is that once that is done, and the user has selected the color and size and then hits the submit button, how do I go to some other page, remember I have set the form action="" or form action=$PHP_SELF, coz I want the page to reload after the selection from the 'color' list.
So how will hitting the submit result in an action that takes me to a new page say buy.php

:)

lorax

4:08 pm on Nov 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Two methods come to mind. You can either create 2 seperate javascript functions and call one or the other of them depending upon which select box is chosen. These two functions will define the form's submit action. OR simply place them in seperate forms and define the form action for the size.

jaspreet

11:04 am on Dec 1, 2003 (gmt 0)

10+ Year Member



Thanks lorax,

I think thats good idea. Having two different forms would make it easier.

Thanks a lot.