Jump to content

3 questions about my dropdowns


Codeman0013

Recommended Posts

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?


[code]<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>
[/code]
Link to comment
Share on other sites

Use a ternary operator to test if the select option value has been posted and matches the current select box building loop to * re-select * a selected value! Also you would do the same thing in the <select> to assgin a CSS disable when the option is disabled or enabled!


examples

change this...

[code]<option value="<?php echo $row_rs_industry['industry_id']; ?>"><?php echo $row_rs_industry['industry_name']; ?></option>[/code]

to this...

[code]<option value="<?=$row_rs_industry['industry_id'];?>"<?=(isset($_POST['industry_id']) && $_POST['industry_id'] == $row_rs_industry['industry_id'] ? " selected='selected'" : null);?>><?=$row_rs_industry['industry_name'];?></option>
[/code]

change this...

[code]<option value="<?php echo $row_rs_productcategory['productCat_id']; ?>"><?php echo $row_rs_productcategory['productCat_name']; ?></option>[/code]

to this...

[code]<option value="<?=$row_rs_productcategory['productCat_id'];?>"<?=(isset($_POST['productCat_id']) && $_POST['productCat_id'] == $row_rs_productcategory['productCat_id'] ? " selected='selected'" : null);?>><?=$row_rs_productcategory['productCat_name'];?></option>[/code]


change this...

[code]<option value="<?php echo $row_rs_product['products_id']; ?>"><?php echo $row_rs_product['products_name']; ?></option>[/code]

to this...

[code]<option value="<?=$row_rs_product['products_id'];?>"<?=(isset($_POST['product']) && $_POST['product'] == $row_rs_product['products_id'] ? " selected='selected'" : null);?>><?=$row_rs_product['products_name'];?></option>[/code]


me!
Link to comment
Share on other sites

Thanks so much that got it!!! Accept for one thing it defaults back to the original value aka in this case Agriculture instead of taking like number 2 or 3 and making it work.. any suggestions here is my code in full...

[code]<?php require_once('Connections/conn_dj.php'); ?>

<?php

mysql_select_db($database_conn_dj, $conn_dj);
$query_rs_industry = "SELECT * FROM tbl_industry ORDER BY industry_name ASC";
$rs_industry = mysql_query($query_rs_industry, $conn_dj) or die(mysql_error());
$row_rs_industry = mysql_fetch_assoc($rs_industry);
$totalRows_rs_industry = mysql_num_rows($rs_industry);

mysql_select_db($database_conn_dj, $conn_dj);
$query_rs_productcategory = "SELECT * FROM `tbl_productcat` WHERE `industry_id` = '$_GET[industry_id]' ORDER BY `productCat_name`";
$rs_productcategory = mysql_query($query_rs_productcategory, $conn_dj) or die(mysql_error());
$row_rs_productcategory = mysql_fetch_assoc($rs_productcategory);
$totalRows_rs_productcategory = mysql_num_rows($rs_productcategory);

mysql_select_db($database_conn_dj, $conn_dj);
$query_rs_product = "SELECT * FROM `tbl_products` WHERE `productCat_id` = '$_GET[productCat_id]' ORDER BY `products_name`";
$rs_product = mysql_query($query_rs_product, $conn_dj) or die(mysql_error());
$row_rs_product = mysql_fetch_assoc($rs_product);
$totalRows_rs_product = mysql_num_rows($rs_product);

mysql_select_db($database_conn_dj, $conn_dj);
$query_rs_outsidena = "SELECT * FROM intl_countries ORDER BY intl_countries.name";
$rs_outsidena = mysql_query($query_rs_outsidena, $conn_dj) or die(mysql_error());
$row_rs_outsidena = mysql_fetch_assoc($rs_outsidena);
$totalRows_rs_outsidena = mysql_num_rows($rs_outsidena);

mysql_select_db($database_conn_dj, $conn_dj);
$query_rs_state = "SELECT * FROM tbl_states ORDER BY tbl_states.`state`";
$rs_state = mysql_query($query_rs_state, $conn_dj) or die(mysql_error());
$row_rs_state = mysql_fetch_assoc($rs_state);
$totalRows_rs_state = mysql_num_rows($rs_state);


?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>DICKEY-john Corporation | Distributors</title>
<link href="_lib/sub.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="keywords" content="Distributor, Sales, Purchase, U.S., International" />
<meta name="description" content="Enter your zip or postal code to find the DICKEY-john distributor nearest you." />
</head>

<body>



<div id="black">&nbsp;</div>
<div id="siteContainer">
<?php include("_inc/topNav.inc"); ?>
<div id="headerContainer">Distributors</div>
<div class="divContainer"><div class="horizDiv"></div><img src="/_img/divEnd.png" alt="div" height="16" width="4" align="right" /></div>
<div id="contentContainer">
<div id="fullContainer">
<div id="stretchCrumbs">
<a href="/index.php">Home</a>
&#187; <a class="high" href="/distributors/">Distributors</a>
</div>
<h1>Distributors</h1>
<p>Search for DICKEY-john distributers by specifying any combonation of the<br />
following classifications: industry, product category, product, and state.</p>
<br />

<br />




<h2>North America</h2>
<form name="distributers">
  <table class="setTbl listTbl" width="90%" cellpadding="0" cellspacing="0" border="0">
    <tr><td width="15%">Industry:</td>
    <td width="85%">
    <select name="industry_id" id="industry" onchange="document.distributers.value='industry';document.distributers.submit();" >
     
      <?php do { ?>
    <option value="<?=$row_rs_industry['industry_id'];?>"<?=(isset($_POST['industry_id']) && $_POST['industry_id'] == $row_rs_industry['industry_id'] ? " selected='selected'" : null);?>><?=$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="<?=$row_rs_productcategory['productCat_id'];?>"<?=(isset($_POST['productCat_id']) && $_POST['productCat_id'] == $row_rs_productcategory['productCat_id'] ? " selected='selected'" : null);?>><?=$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="<?=$row_rs_product['products_id'];?>"<?=(isset($_POST['product']) && $_POST['product'] == $row_rs_product['products_id'] ? " selected='selected'" : null);?>><?=$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>

<br />
<h2>Outside North America </h2>
<table class="setTbl listTbl" width="90%" cellpadding="0" cellspacing="0" border="0">
<tr><td>  <select name="outna" id="center" onchange="javascript:document.centerform.submit()">
      <option value="">Select a country...</option>
      <?php do { ?>
      <option value="<?php echo $row_rs_ousidena['name']; ?>"><?php echo $row_rs_outsidena['name']; ?></option>
      <?php } while ($row_rs_outsidena = mysql_fetch_assoc($rs_outsidena)); ?>
     
      </select></td>
    </tr></td></tr>
      </table>
</form>
</div>
</div>
<?php include("_inc/footer.inc"); ?>
</body>
</html>
<?php
mysql_free_result($rs_industry);

mysql_free_result($rs_productcategory);

mysql_free_result($rs_product);

mysql_free_result($rs_outsidena);

mysql_free_result($rs_state);
?>
[/code]
Link to comment
Share on other sites

[quote author=Codeman0013 link=topic=108922.msg438789#msg438789 date=1158853937]
Thanks so much that got it!!! Accept for one thing it defaults back to the original value aka in this case Agriculture instead of taking like number 2 or 3 and making it work.. any suggestions here is my code in[/quote]
http://www.google.com/search?hl=en&q=define%3A+aka
Link to comment
Share on other sites

Incorrect usage of the term "aka".
Aka means "Also known as", used in reference to where something is called something, but at the same time, it is called something else.  If someone has one name, and also another, there name is whatever, also known as whatever.  You used it in a sentence that had nothing to do with the original, or urban meaning.
Link to comment
Share on other sites

do you mean making it default
I think it's
selected="selected" or something, I think that is how you set up default, if you need it to be defaulted based on something else, use php to create that line of option.  for instance if you have a drop down menu, with say 2 options
if ($whatever == "whatever") {
echo "your select";
}else {
echo "the other";
}
so it shows what you want as default.
Link to comment
Share on other sites

now i get ... Parse error: syntax error, unexpected '{', expecting ',' or ';' in /u/internet/com/dickeyjohn/test.php on line 76



and [code] <?php do { ?>
    <option value="<?=$row_rs_industry['industry_id'];?>"<?=(isset($_GET['industry_id']) && $_GET['industry_id'] == $row_rs_industry['industry_id']) { ?> selected<?php } ?>><?=$row_rs_industry['industry_name'];?></option>[/code]


thats the line of code
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.