PDA

View Full Version : 3 questions about my dropdowns


Codeman0013
09-21-2006, 02:53 PM
Hey I have 3 questions in one here. First off when my dropdowns submit their value and the page refreshes they go back to their original value which says select a value but I want them to stay on the value the user selects. Also if the user refreshes the page or changes the value of one of the dropdowns I want all the corresponding ones below to automatically repopulate. Finally I want them to be disabled until a user selects the value that will populate it and I am having issues with the if loops. Can someone look at this code and help me with this stuff?


<form name="distributers">
<table class="setTbl listTbl" width="90%" cellpadding="0" cellspacing="0" border="0">
<tr><td>Industry:</td><td>

<select name="industry_id" id="industry" onchange="document.distributers.value='industry';document.distributers.submit();" >
<option value="">Select an Industry...</option>
<?php do { ?>
<option value="<?php echo $row_rs_industry['industry_id']; ?>"><?php echo $row_rs_industry['industry_name']; ?></option>

<?php } while ($row_rs_industry = mysql_fetch_assoc($rs_industry)); ?>
</select></td></tr>
<tr><td>Product category:</td><td>


<select name="productCat_id" id="center" onchange="document.distributers.value='productCat_id';document.distributers.submit();" >
<option value="">Select a product category...</option>
<?php do { ?>
<option value="<?php echo $row_rs_productcategory['productCat_id']; ?>"><?php echo $row_rs_productcategory['productCat_name']; ?></option>

<?php } while ($row_rs_productcategory = mysql_fetch_assoc($rs_productcategory)); ?>
</select>
<tr><td>Products:</td><td>
<select name="product" id="center" onchange="document.distributers.value='products_id';document.distributers.submit();" >
<option value="">Select a product...</option>
<?php do { ?>

<option value="<?php echo $row_rs_product['products_id']; ?>"><?php echo $row_rs_product['products_name']; ?></option>
<?php } while ($row_rs_product = mysql_fetch_assoc($rs_product)); ?>
</select>
<tr><td>State:</td><td>
<select name="state" id="center" >
<option value="">Select a state...</option>
<?php do { ?>

<option value="<?php echo $row_rs_state['state']; ?>"><?php echo $row_rs_state['state']; ?></option>
<?php } while ($row_rs_state = mysql_fetch_assoc($rs_state)); ?>
</select>
</td></tr>
<tr><td>&nbsp;</td><td><input class="PythonButton" type="submit" value="Find" name="{Button_Name}"></td></tr>
</table>
</form>

raf
09-21-2006, 03:21 PM
for question1 --> you need to add the
select="selected"
attribute to the selected option-tag when you refresh the page. so inside you're loop, you'll gonnan need to check the value from for example $_GET['industry_id'] with the $row_rs_industry['industry_name'] and add the select="selected" if they are equal. search this forum for dynamically creating dropdowns and you'll find plenty of examplecode for that.
for question 2 --> run a search here or on google for 'interdependent dropdowns php'. there are a few ays to accomplish this. i usually dynamically build a javascript-array for each dropdown using php.
for question 3 --> with interdependent dropdowns, the 2° / 3° dropdown will have an empty optionlist untill you've selected a value for the 1° / 2° dropdown, so that takes care of that.

to be honest: i'd get rid of this code and look for a tutorial on dynamically creating interdependent dropdown, or ask someone to code it for you as an example...

Codeman0013
09-21-2006, 04:44 PM
My main problem is that this is a database driven site... I had one made that was javascript adn worked fine buut they wanted it to run off the database and i do not know how to edit my javascript file to run off the database...

raf
09-22-2006, 07:53 AM
your php script will select the options from the db, and then build the javascript arrays so that the dynamic dropdown-population happens clientsided without any trafic back to the server.
think of it as php dynamically creating the javascript instead of you handcoding it --> you can use your old javascript, but need to add a few lines of php to dynamically create it before the page is sent to the browser.

you could of course submit your form when the first dropdown is changed and then reload the page with the second dropdown on it etc.
or you could set it up in an AJAX framework, but doing it with javascript is by far the easiest and most userfriendly alternative.