Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old September 15th, 2006, 03:54 PM
Registered User
Join Date: Sep 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to retain form values from dependent list ?

Hello p2p,

I have a html page (ad_form_online.html) with 2 selection lists. There is a parent list (Category) and the other one (Subcategory) is dependent on this parent. The second list is populated depending on the parent list selection.

My Problem: I want to retain the values on the 2 selection lists after submit. I am able to retain the first list values and the second list's select element, but not second list's array (It defaults to the first of five arrays).

How can I retain the 2nd selection list array values after submitting a form? Any suggestions would be really helpful...

Thank you in advance,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~

The JavaScript code:

<script language="javascript" type="text/javascript">
<!-- Begin category select
// declare arrays
var Business = new Array("Bus. Cards","Employment","Services", "", "", "", "", "");
var Household = new Array("Antiques", "Appliances", "Collectibles", "Electronics",
"Furniture", "Musical Instr.", "Other HH", "Sporting Goods");
var Miscellaneous = new Array("Baby / Kids", "Jewelry", "Office Equipment", "Other Misc.",
"Pets / Animals", "Tools", "Wanted", "Yard Equipment");
var Real_Estate = new Array("Home / Commercial", "Land / Acreage", "Rentals", "Vacation", "", "", "", "");
var Vehicles = new Array("Antique / Classic", "Autos / MiniVans", "Cycle / OHRVs", "Parts /
Accy", "RVs / Trailers", "Trucks / Vans / SUVs", "Watercraft / Accy", "");

function swapOptions(the_array_name)
//ex. If you selected "Business," the_array_name will equal the string "Business"
  var numbers_select = window.document.FrontPage_Form1.O2_Subcategory;
//Sets a variable to refer to the second list, the Subcategory.

  var the_array = eval(the_array_name);
//The result of this line is that the variable the_array will equal one of the arrays //defined above. If the_array_name is Business, the_array will equal the Business array.

  setOptionText(window.document.FrontPage_Form1.O2_S ubcategory, the_array);
//Does the work of setting the values of the list select to the contents of the_array.

function setOptionText(the_select, the_array)
var deel1 = 0
  for (loop=0; loop < the_select.options.length; loop++)
    the_select.options[loop].text = the_array[loop];
//The options property of a select element is an array of all that select's options. Because //it's an array, you can find out how many options are in a select using the length property //of arrays.

//The first time you hit the loop, the loop variable equals 0. If you selected "Household", //the_array[0] will be "Antiques", so this line will change the first option in the list //select to "Antiques". The next time through the loop, loop[1] second option in the list //select will equal "Appliances", etc.

//End category select-->

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~

My form code:

form enctype="multipart/form-data" method="POST" onsubmit="return FrontPage_Form1_Validator(this)" language="JavaScript" name="FrontPage_Form1">

<table width="640" bgcolor="#dedede" align="center" valign="middle" cellpadding="8"

cellspacing="0" border="1" bordercolor="#111111">

<td align="right"><b>Category & Subcategory </b>*
<br />
(select a category and a subcategory) </td>

<td align="center">
  <table width="94%" border="1" cellpadding="5" cellspacing="0" style="border-collapse:

  collapse;" bordercolor="#111111">
      <td width="120" height="26" align="center">
      <td width="190" height="26" align="center">
      <td width="120" align="center">
      <select size="5" name="O1_Category"

onChange="swapOptions(window.document.FrontPage_Fo rm1.O1_Category.options[selectedIndex].text

);" />
      <option>Business </option>
      <option>Household </option>
      <option>Miscellaneous </option>
      <option>Real_Estate </option>
      <option>Vehicles </option>
      <td width="190">
      <select size="5" name="O2_Subcategory" />
       <option>Bus. Cards</option>
      <option> .</option>


Similar Threads
Thread Thread Starter Forum Replies Last Post
form submit problem/dependent list menus alexscan Pro PHP 0 January 1st, 2008 06:29 PM
Retain form value from dependent list box qhong Dreamweaver (all versions) 18 November 4th, 2004 03:36 AM
Dynamic Dependent List Box improvement SoC Classic ASP Basics 1 August 16th, 2004 01:02 AM
Retain form values qhong Javascript 1 May 13th, 2004 02:02 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.