Sign in to follow this  

[web] Help with Select Box Problem

This topic is 3624 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, I am working on a website and I am encountering a problem. So this website page lets people Order Replacement Parts. I have been told to make it a specific way, everything worked up until the point I need help with. The screen first asks how many parts you would like to order. The entered information gets sent to $PartNum and then it executes for($i=1;$i<=$PartNum;$i++) inside of the for loop, there are two select boxes. The first select box runs a mysql query that displays all products through $sql='select * from service_products'; $rresult=mysql_query($sql); while($arr=mysql_fetch_row($rresult)){ echo '<option value="'.$arr[1].'">'.$arr[1].'</option>'; The value is Product. This all works. The part I am having trouble with is the second select box. Using Java code: function submitform() { document.myform.submit(); } I make it so that after you select the product it updates the page. Then I have to display the second select box with this query: $sql2='select * from service_parts where Product ='.$Product; Except because it is in a for loop, it executes the query before it can store information in $Product. How can I make it so that after you select a product from the first select box, the second select box will then fill with the query that selects parts?

Share this post


Link to post
Share on other sites
That's not Java by the way, it's javascript.

"The first select box runs a mysql query" - no it doesn't. Browsers don't run database queries. They tell the server to run the query. It is important to get your descriptions accurate so that it becomes easier to see where you need to insert the logic. Probably what happens is that the client asks for this page, the server runs that sql query, and fills up the select box with the results, then that page is returned to the client.

Once the first option is chosen from the select box, the client can submit the form. The server sees the submitted form and looks at the value in the first select box. It uses that value to run a query to find the values for the second select box. Then it can return a page that contains both the select boxes filled in.

All that stuff about what is inside for loops is a red herring. Everything on the server is completed before the data goes back to the client, and when a client submits a form to a PHP page, that page is run from the beginning. If you need a 2 part process, you essentially need 2 processes - either 2 separate PHP pages, or some cunning if-statements inside one page.

A common method uses these days is to use AJAX, where instead of submitting the form and loading a new version of that form, you send some data in the background, get some data back, and fill in the 2nd select box based on that. But it's likely to be more complicated for you to do that until you fully understand how to do it the normal way.

Share this post


Link to post
Share on other sites
Quote:
but is there anyway besides that to get the boxes to work properly?


There is one more way, but you should use it only if the number of possible options for the second select box is very limited. You could have PHP generate a two-dimensional array containing all the possible values for select box 2. Then add an onchange() event to select box 1. When it changes, find the appropriate values for select box 2 in the array and fill it.

This quickly leads to a large block of generated javascript and should only be used of the total number of possible options for select box 2 is very low.

Share this post


Link to post
Share on other sites

This topic is 3624 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this