Jump to content

Help with dependent selects


lingo5

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.