Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Self Submitting a form 1

Status
Not open for further replies.

Deadline

Programmer
Feb 28, 2001
367
US
Hi,

I have a countrylist drop-down box. Whenever the user changes the country, I need to populate the textboxes and other controls in the SAME page with the relevant info. The country list is from the Database.

What is the best strategy to do this ?

Thank you very much in advance.

RR
:-(

Keywords
self
self submitting
self submit
self submission
form submission
itself

 
You can have the form submit to itself by putting its own address in the action portion of your form declaration.

You can then pick up the value of the wanted form element by asking for request.form("elementName"), and then populate the other elements accordingly.

--OR--

You could populate a javascript array with the values that you want to show up (probably a multidimensional) 1st element corresponding to US, and the subsequent entries being the states or whatever, 2nd being the UK, and so on -- you could use the data from a database to do this embedding asp in the javascript function to fill them up.

Then, you could have an onClick (or onChange) event attached to the form element that would trigger a javascript function to repopulate the form elements on the fly without having to submit the form to itself...

Of the two, I sort of like the second because the page doesn't flicker or anything when the user makes the selection... the change happens very quickly.

good luck! :)
Paul Prewett
 
Thank you very much for the neatly written response.

I plan to use the second method, because of the obvious advantage.

Actually, I haven't gone beyond 2 dimensions in Javascript. Can you give a small example for that ? And since this being Intranet task, client side VBScript is ok.

In my case, the countries and country codes will be stored in a Country Master Table. The other info such as Contact Person, States etc. are in another table. It is going to be one to many here.

Thank you once again.

RR
:-I


 
Ahhhhhh... client side vbscript it is!!

Scrap the javascript idea -- If you can use vbscript, then let's do that.

First, you'll need to make sure that the dropdown box that you are going to dynamically populate has enough elements in it (initially defined) to hold the maximum number of elements. There's probably a way around this, but I don't know what it is, so...

Then, you have your recordset... the master table, I'm assuming will have multiple entries of countries and unique state entries. The code below will assume that that recordset exists and it will be called 'rs', and have two fields, 'country', and 'state'

Ok, so that's done. Now let's look at some code to populate the array:
Code:
<script language=vbscript>
dim stateArray(5, 50) 
'make sure that the second number, again, will be big enough to hold the max # of states.  The first number is the number of countries you will have

dim keyValue, i, whileCounter
for i = 1 to 5   'the number of countries to do (5, here)
  keyValue = rs(&quot;country&quot;)
  whileCounter = 1
  while keyValue = rs(&quot;country&quot;)
    stateArray(i, whileCounter) = rs(&quot;state&quot;)
    rs.movenext
    keyValue = rs(&quot;country&quot;)
    whileCounter = whileCounter + 1
  wend
next
</script>

So done -- array populated. Now, you will have to check the value of the country dropdown each time it is clicked in order to populate the state dropdown:
Code:
<select name=&quot;country&quot; onClick=&quot;popStates&quot;>
  <!-- insert all your options here -->
</select>

And then you need your 'states' element
Code:
<select name=&quot;states&quot;>
  <!-- insert all your initial options here (blank ones, if you like)-->
</select>

Then, you have to make that popStates function to re-do the states element each time. It would look something like:
Code:
<script language=vbscript>
  function popStates()
    if document.formName.country.value = 1 then
      for i = 1 to numOfElementsInStates
        document.formName.states(i - 1).value = i
        document.formName.states(i - 1).text = stateArray(1, i)
      next
    elseif document.formName.country.value = 2 then
      for i = 1 to numOfElementsInStates
        document.formName.states(i - 1).value = i
        document.formName.states(i - 1).text = stateArray(2, i)
      next
    elseif document.formName.country.value = 3 then
      for i = 1 to numOfElementsInStates
        document.formName.states(i - 1).value = i
        document.formName.states(i - 1).text = stateArray(3, i)
      next
    elseif document.formName.country.value = 4 then
      for i = 1 to numOfElementsInStates
        document.formName.states(i - 1).value = i
        document.formName.states(i - 1).text = stateArray(4, i)
      next
    elseif document.formName.country.value = 5 then
      for i = 1 to numOfElementsInStates
        document.formName.states(i - 1).value = i
        document.formName.states(i - 1).text = stateArray(5, i)
      next
    end if
  end function
</script>

The values of the state entries will be the number of iterations you have taken in the loop, and the text will be whatever was in the recordset for that entry. You could probably afford to get a little more creative in how you give the values (like some value from the recordset), but that would involve (at least in this example) building a string when you populate the array, and then parsing it when you populate the element. Actually, your performance would probably suffer too much from that approach.

If there aren't the total number of entries in the array, it will just output a blank for both the value and the text of the option... no harm, no foul.

Let me qualify the above by saying it's not tested, and I just wrote it sitting here by reasoning it out... so more likely than not, it's probably got a syntax error or two in it, but I think you get the idea.

let me know how it works out :)
Paul Prewett
 
Glad I could help --

and thanks for the article. It was a good read. I actually do alot of that type of thing in my work, so it will be very useful.

Thanks! :)
paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top