RE: Dynamic Select Lists - 1st Selection Effects 2nd!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> -----Original Message-----
> From: Rahul S. Johari [mailto:sleepwalker@xxxxxxxxxxxxxxxx]
> Sent: Thursday, July 31, 2008 11:40 AM
> To: php-general@xxxxxxxxxxxxx
> Subject:  Dynamic Select Lists - 1st Selection Effects 2nd!
> 
> Ave,
> 
> What I have is two Select (Drop-Down) lists (State & County) and I'm
> populating them from a mySQL table. What I want is when the user
> selects the State from the State List, the County List should only
> pull out counties associated with that State.
> 
> Now I know that you can create such an effect using Javascript or AJAX
> and have nothing to do with PHP/mySQL - and I was able to accomplish
> that - but only for lists where you could define data manually. I'm
> not able to accomplish this at all with Lists that are pulling out
> option data from a mySQL table.
> 
> I'm also giving the User the opportunity to add as many State/County
> combinations as possible in a box.
> 'tis my code:
> 
> 			  <!-- *********** STATE/COUNTY BOX ************
-->
> 			  <TR>
> 				<TD CLASS="blackText">State: </TD>
> 				<TD CLASS="blackText"><SELECT
NAME="d_state">
> 				  <?php do {  ?>
> 				  <OPTION VALUE="<?php echo
> $row_D_STATE['STATE']?>"><?php echo
> $row_D_STATE['STATE']?></OPTION>
> 				  <?php
> 					} while ($row_D_STATE =
> mysql_fetch_assoc($D_STATE));
> 					  $rows =
mysql_num_rows($D_STATE);
> 					  if($rows > 0) {
>
mysql_data_seek($D_STATE, 0);
> 						  $row_D_STATE =
> mysql_fetch_assoc($D_STATE);
> 					}
> 				  ?>
> 				</SELECT>
> 				</TD>
> 			  </TR>
> 			  <TR>
> 				<TD CLASS="blackText">County: </TD>
> 				<TD CLASS="blackText">
> 				<SELECT NAME="d_county">
> 				<OPTION VALUE="ALL"
SELECTED>All</OPTION>
> 				  <?php	do {  ?>
> 				  <OPTION VALUE="<?php echo
> $row_D_COUNTY['COUNTY']?>"><?php echo
> $row_D_COUNTY['COUNTY']?></OPTION>
> 				  <?php
> 					} while ($row_D_COUNTY =
> mysql_fetch_assoc($D_COUNTY));
> 					  $rows =
mysql_num_rows($D_COUNTY);
> 					  if($rows > 0) {
>
mysql_data_seek($D_COUNTY, 0);
> 						  $row_D_COUNTY =
> mysql_fetch_assoc($D_COUNTY);
> 					  }
> 					?>
> 				</SELECT> <INPUT NAME="add_btn_1"
TYPE="button"
> VALUE="[+] Add"
> onClick="document.AD_INVENTORY_FORM.d_state_county.value
> +=document.AD_INVENTORY_FORM.d_state.value
> +'/'+document.AD_INVENTORY_FORM.d_county.value+'\n';">
> 				</TD>
> 			  </TR>
> 			  <TR>
> 				<TD CLASS="blackText"
COLSPAN="2"><TEXTAREA
> NAME="d_state_county"
> COLS="26" ROWS="3"></TEXTAREA></TD>
> 			  </TR>
> 			  <!-- *********** STATE/COUNTY BOX ************
-->
> 
> I'm not able to understand exactly how to manipulate the SQL Query or
> otherwise force the 2nd Select List to only show records that match
> the selected State.
> 
> Any pointers?

The page referenced by an AJAX XmlHttpRequest() doesn't have to be
static. You can even use the query string to supply the AJAX call with
parameters (or perhaps post a form instead). This way, you can pass the
chosen state to the AJAX-requested page and use PHP on the other end to
construct the appropriate counties selection list. AJAX could then push
this result into a DIV that had, up until now, contained an empty
selection list with no available options.

Summary: Page has two DIVs: one for state list, one for county list
(which is empty). User clicks first DIV's selection box, onChange JS
method fires AJAX call to getCounties.php?state=XX (where XX is the
chosen state). PHP on the other end builds a selection list for the
given state and returns it to the AJAX call. The AJAX result is then
assigned to the second div, which now contains the list of counties for
the given state.

HTH,


Todd Boyd
Web Programmer



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux