lingo5 Posted April 29, 2010 Share Posted April 29, 2010 Hi, I have 2 select menus that I would like to be dependent. I want the second select to only show some entries depending on the selection made on the first. This is the code of my 2 selects: <td bgcolor="#ECEACE"><select name="id_categoria" class="CP_loginFormFields"> <option value="value">Seleccione familia</option> <?php do { ?> <option value="<?php echo $row_categories_RS['id_categoria']?>"><?php echo $row_categories_RS['categoria_esp']?></option> <?php } while ($row_categories_RS = mysql_fetch_assoc($categories_RS)); $rows = mysql_num_rows($categories_RS); if($rows > 0) { mysql_data_seek($categories_RS, 0); $row_categories_RS = mysql_fetch_assoc($categories_RS); } ?> </select></td> </tr> <tr valign="baseline"> <td align="right" nowrap="nowrap" bgcolor="#85765F" class="CP_TableHeaders"> </td> <td bgcolor="#ECEACE"><select name="id_subcategoria" class="CP_loginFormFields" id="id_subcategoria"> <option value="value">Seleccione sub-familia</option> <?php do { ?> <option value="<?php echo $row_subcategorias_RS['id_subcategoria']?>"><?php echo $row_subcategorias_RS['subcategoria_esp']?></option> <?php } while ($row_subcategorias_RS = mysql_fetch_assoc($subcategorias_RS)); $rows = mysql_num_rows($subcategorias_RS); if($rows > 0) { mysql_data_seek($subcategorias_RS, 0); $row_subcategorias_RS = mysql_fetch_assoc($subcategorias_RS); } ?> </select></td> Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/200197-help-with-dependent-selects/ Share on other sites More sharing options...
Philip Posted April 29, 2010 Share Posted April 29, 2010 In order to do this you'll need to use javascript, or show the second select on another page. Since PHP is server side, you can't do via just PHP Quote Link to comment https://forums.phpfreaks.com/topic/200197-help-with-dependent-selects/#findComment-1050659 Share on other sites More sharing options...
lingo5 Posted April 29, 2010 Author Share Posted April 29, 2010 Thanks for your reply. Could anyone help with whatever JS is neded?. I don't know where to start. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/200197-help-with-dependent-selects/#findComment-1050661 Share on other sites More sharing options...
Philip Posted April 29, 2010 Share Posted April 29, 2010 Something like: http://www.mattkruse.com/javascript/dynamicoptionlist/ Search around for "Dynamic options javascript" Quote Link to comment https://forums.phpfreaks.com/topic/200197-help-with-dependent-selects/#findComment-1050684 Share on other sites More sharing options...
lingo5 Posted April 29, 2010 Author Share Posted April 29, 2010 WOW, that's a bit to complex for a newie..... Quote Link to comment https://forums.phpfreaks.com/topic/200197-help-with-dependent-selects/#findComment-1050766 Share on other sites More sharing options...
Infinitive Posted April 29, 2010 Share Posted April 29, 2010 Here's the deal lingo5, in a hopefully newbie-friendly way: When your browser loads a web page, your computer isn't in constant communication with theirs. You connect, and download the page, and then view/type/use it at your leisure. For example, right now while you're reading this you could reach over and unplug the network connection from your computer, and it wouldn't interfere with reading because you've already downloaded the page. It would only be a problem when you wanted to reply or go onto the next page. OK so mindful of that, think of what you're asking your page to do. A user will connect to your website and the server will do all the PHP work and come up with a page. ... then it sends it to the user's computer ... then the user's computer has it. Now he can read, type, select, etc. But the page is on his computer. The server is done, it already sent the page. This is referred to as "server side" vs "client side". And when you want your page to do something, you have to decide which side it will happen on (i.e. which computer will be doing it). PHP is a server-side language. All the work you do in PHP happens before the page leaves the server. Once it leaves, PHP can't help you anymore. So what you want to do is send a page to a user, let them make a change, and have them see the result of that change. But by the time they are able to use your select, the page is already client side. Too late for your server, too late for PHP. So there are 3 options: 1) Have the first select. Send that form back to the server. The server (using PHP) will calculate what the second select will show. And then it will send that second page back to the user's computer. This is basically using 2 pages. 2) Use a different language that works on the client side. JavaScript is a fine choice, and KingPhilip's link is a good one. But it's a separate language, a little different from PHP. 3) Use AJAX, which is an Asynchronous connection to the server. It's like loading a new page without actually loading a new page. It has been rather popular lately but is very complex, easily the toughest of these 3 methods. I only mention it for completeness. So I suggest you decide between method 1, which is just to have a second page for the second select. Or method 2, which is more elegant but more complicated. Quote Link to comment https://forums.phpfreaks.com/topic/200197-help-with-dependent-selects/#findComment-1050786 Share on other sites More sharing options...
lingo5 Posted April 30, 2010 Author Share Posted April 30, 2010 Thanks very much Infinitive for you explanation. I'm going to try the JS approach and will let you know. Thanks all Quote Link to comment https://forums.phpfreaks.com/topic/200197-help-with-dependent-selects/#findComment-1050951 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.