Skip to main content
gamedev.net
🔒 Locked 🤖 Godot

[web] Help with Select Box Problem

Started by TravisMarshall 4 replies 750+ views
Original Post
TravisMarshall
TravisMarshall
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 ''; 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?
Kylotan
Kylotan
That's not Java by the way, it's &#106avascript.<br/><br/>"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.<br/><br/>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.<br/><br/>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.<br/><br/>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.
TravisMarshall
TravisMarshall
ya sorry i meant to put &#106avascript<br><br>anyways kylotan, that is basically what I am trying to do.<br><br>My boss did mention maybe doing AJAX<br><br>but is there anyway besides that to get the boxes to work properly?
Sander
Sander
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 &#106avascript and should only be used of the total number of possible options for select box 2 is very low.
Sander
Sander
That's perfectly doable. Here are two examples that use &#106avascript arrays for dependent select boxes:<br><br><a href="http://www.houseofscripts.com/scripts/&#106avascripts/dynamsbox.htm">http://www.houseofscripts.com/scripts/&#106avascripts/dynamsbox.htm</a><br><a href="http://www.roubaixinteractive.com/PlayGround/&#106avascript/Dependent_List_Box.asp">http://www.roubaixinteractive.com/PlayGround/&#106avascript/Dependent_List_Box.asp</a><br><br>There are hundreds more if you Google.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.