close ad
Install the LAtest Updates to Work with CC 2017 and CC 2018
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Populating text fields from dropdown

Thread began 9/27/2012 5:45 pm by rogersds267040 | Last modified 3/30/2015 12:29 pm by Jason Byrnes | 5824 views | 20 replies |

rogersds267040

Populating text fields from dropdown

I'm trying to accomplish the following:

I have 1 dropdown that selects all products. Then I have 2 text fields(Product Price, Product Name). When select a product from the dropdown, the price and name change according to the product I select in the dropdown?

Any hints going about using dynamic dropdown?

Sign in to reply to this post

Jason ByrnesWebAssist

dynamic dropdowns wont be able to accomplish this, but it can be done with a little hand coding. By using an onchange event on the elect list, you can pass the product list lable and value to text fields.

here is an example of doing this using a static select list:

php:
<form id="prodForm" name="prodForm" method="post"  action="">

  <select name="products" id="products" onchange="document.prodForm.prodName.value = document.prodForm.products[document.prodForm.products.selectedIndex].text;document.prodForm.prodPrice.value = document.prodForm.products[document.prodForm.products.selectedIndex].value">
    <option value="">--select a product--</option>
    <option value="14.99">B.B. King - Live at the Regal</option>
    <option value="22.99">The Band - The Last Waltz</option>
    <option value="19.99">Alvin Bishop - Let It Flow</option>
    <option value="17.99">Dr. John - Gris Gris Gumbo</option>
    <option value="15.99">The Paul Butterfield Blues Band</option>
  </select>
  <br />
  <label for="prodName">Product Name:</label>
  <input type="text" name="prodName" id="prodName" />
  <br />
  <label for="prodPrice">Product Price:</label>
  <input type="text" name="prodPrice" id="prodPrice" />
</form>
Sign in to reply to this post

rogersds267040

Populating text fields from dropdown

Thank you but problem is trying to do it dynamical:

See below for code, any suggestions is welcome:


<form action="orders_Insert.php" method="post" name="WADAInsertForm" id="WADAInsertForm">
<div class="WADAHeader">Add Material/Equipment for Service Request - <?php echo $row_WAREQ['reqid']; ?></div>
<table class="WADADataTable" cellpadding="0" cellspacing="3" border="0">
<tr>
<th nowrap bgcolor="#CCCCCC" class="WADADataTableHeader">* Material/Equipment:</th>
<td class="WADADataTableCell"><label for="ProductName"></label>
<select name="ProductName" id="ProductName"
onchange="document.WADAInsertForm.ProductPrice.value = document.WADAInsertForm.products[document.WADAInsertForm.products.selectedIndex].text;
document.WADAInsertForm.SKU.value = document.WADAInsertForm.products[document.WADAInsertForm.products.selectedIndex].value">
<option value="">Select a Material/Equipment</option>
<?php
do {
?>
<option value="<?php echo $row_WAProducts['ProductID']?>"><?php echo $row_WAProducts['ProductName']?></option>
<?php
} while ($row_WAProducts = mysql_fetch_assoc($WAProducts));
$rows = mysql_num_rows($WAProducts);
if($rows > 0) {
mysql_data_seek($WAProducts, 0);
$row_WAProducts = mysql_fetch_assoc($WAProducts);
}
?>
</select></td>
</tr>
<tr>
<th nowrap bgcolor="#CCCCCC" class="WADADataTableHeader">Price:</th>
<td class="WADADataTableCell"><input type="text" name="ProductPrice" id="ProductPrice" size="32" /></td>
</tr>
<tr>
<th nowrap bgcolor="#CCCCCC" class="WADADataTableHeader">SKU:</th>
<td class="WADADataTableCell"><input type="text" name="SKU" id="SKU" value="" size="32" /></td>
</tr>
</table>

<div class="WADAButtonRow">
<table class="WADADataNavButtons" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="WADADataNavButtonCell" nowrap="nowrap"><input type="image" name="Insert" id="Insert" value="Insert" alt="Insert" src="../../../WA_DataAssist/images/Pacifica/Refined_insert.gif" /></td>
</tr>
</table>
<input name="WADAInsertRecordID" type="hidden" id="WADAInsertRecordID" value="">
</div>
</form>

Sign in to reply to this post

Jason ByrnesWebAssist

it's the same concept doing it dynamically as doing it statically.

set the value of the select list to use the price, and the label of the select to use the product name.

then use the onchange event to set the text field values.

Sign in to reply to this post

rogersds267040

Populating text fields from dropdown

Jason, I followed your instructions but still doesn't work.

Please see attachment.

Attached Files
orders_Insert.zip
Sign in to reply to this post

Jason ByrnesWebAssist

the onChange code is not correct.

change:

onChange="document.WADAInsertForm.ProductSKU.value = document.WADAInsertForm.ProductName[document.WADAInsertForm.ProductSKU.selectedIndex].text;document.WADAInsertForm.ProductPrice.value = document.WADAInsertForm.ProductName[document.WADAInsertForm.ProductPrice.selectedIndex].value"




to:

onChange="document.WADAInsertForm.ProductSKU.value = document.WADAInsertForm.ProductName[document.WADAInsertForm.ProductName.selectedIndex].text;document.WADAInsertForm.ProductPrice.value = document.WADAInsertForm.ProductName[document.WADAInsertForm.ProductName.selectedIndex].value"




also, the select list value is still set to use the ProductID column, it needs to use the Product Price column for this to work.

Sign in to reply to this post

rogersds267040

Populating text fields from dropdown

Thank you Jason.

Sorry about the mix up but I got the value and the text to work. I must of sent you the wrong file whiles trying different senarios.

What I really want is, using the way you specified only display a value and a text. But want I want to display is at least 5 column values from the products table. e.g price,sku,weight,binNumber, and ProductStartDate. The values should change when I select the dropdown to correspond with the product.

Thanks

Sign in to reply to this post

Jason ByrnesWebAssist

well, that's a horse of a completely different color, and requires a completely different approach.


going back to my previous static example, the option values would need to pass a comma seperated list of "price,sku,name,etc..", for example:
value="15.99,982,The Paul Butterfield Blues Band"

then the onchange event calls a javascript function to convert the comma separated list to an array, and set the form elements from the array pieces

php:
<script type="text/javascript">

function setProdData(opts) {
    var optArray = new Array;
    optArray = opts.split(",");
    var theForm = document.prodForm;
    var prodNameField = theForm.prodName;
    var prodPriceField = theForm.prodPrice;
    var prodSKUField = theForm.prodSKU;
    prodPriceField.value = optArray[0];
    prodSKUField.value = optArray[1];
    prodNameField.value = optArray[2];
    

}
</script>
<form id="prodForm" name="prodForm" method="post"  action=""> 
  <select name="products" id="products" onchange="setProdData(document.prodForm.products[document.prodForm.products.selectedIndex].value)"> 
    <option value="">--select a product--</option> 
    <option value="14.99,123,B.B. King - Live at the Regal">B.B. King - Live at the Regal</option> 
    <option value="22.99,324,The Band - The Last Waltz">The Band - The Last Waltz</option> 
    <option value="19.99,423,Alvin Bishop - Let It Flow">Alvin Bishop - Let It Flow</option> 
    <option value="17.99,658,Dr. John - Gris Gris Gumbo">Dr. John - Gris Gris Gumbo</option> 
    <option value="15.99,982,The Paul Butterfield Blues Band">The Paul Butterfield Blues Band</option> 
  </select> 
  <br /> 
  <label for="prodName">Product Name:</label> 
  <input type="text" name="prodName" id="prodName" /> 
  <br /> 
  <label for="prodSKU">Product SKU:</label> 
  <input type="text" name="prodSKU" id="prodSKU" /> 
  <br /> 
  <label for="prodPrice">Product Price:</label> 
  <input type="text" name="prodPrice" id="prodPrice" /> 
</form>
Sign in to reply to this post

rogersds267040

Populating text fields from dropdown

Thank you Jason.

Best company to get support ever.

Sign in to reply to this post

Jason ByrnesWebAssist

Glad to hear the example made sense.

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...