Solved

Why is the dropdown list selection not updating form?

Posted on 2003-11-01
9
276 Views
Last Modified: 2006-11-17
Here's my code, I have this working fine when I select the submit button but for some reason it is not submitting when I select an item from the drop down list.

<SCRIPT LANGUAGE=javascript>
<!--

function submitCountry(){
     var objForm = document.forms[0];
     objForm.elements['State'].selectedIndex=0;  'was region
     objForm.submit();
}
function submitState(){
     var objForm = document.forms[0];
     objForm.submit();
}

//-->
</SCRIPT>

</HEAD>
<BODY>
<FORM action="mypage.asp" method=POST id=form1 name=form1>
<SELECT  name="Country" onChange="submitCountry()">
    <%call  makeCountry%>
</SELECT><br>
<SELECT  name="State" onChange="submitState()">
    <%call makeState%>
</SELECT><br>
<p><INPUT type="submit" value="Submit" id=submit1 name=submit1></p>
</FORM>
</BODY>

Thanks
0
Comment
Question by:Chris_Granger
  • 3
  • 3
  • 3
9 Comments
 
LVL 21

Expert Comment

by:ap_sajith
Comment Utility
What do you mean not submitting?? Try the following code...

<SCRIPT LANGUAGE=javascript>
<!--

function submitCountry(){
    var objForm = document.forms[0];
    objForm.elements['State'].selectedIndex=0;  //was region
    objForm.submit();
}
function submitState(){
    var objForm = document.forms[0];
    objForm.submit();
}

//-->
</SCRIPT>

</HEAD>
<BODY>
<FORM action="test.asp" method=POST id=form1 name=form1>
<SELECT  name="Country" onChange="submitCountry()">
   <OPTION>CANADA</OPTION>
   <OPTION>US</OPTION>
   <OPTION>UK</OPTION>
   <OPTION>UAE</OPTION>
</SELECT><br>
<SELECT  name="State" onChange="submitState()">
   <OPTION>ABC</OPTION>
   <OPTION>DEF</OPTION>
   <OPTION>GHI</OPTION>
</SELECT><br>
<p><INPUT type="submit" value="Submit" id=submit1 name=submit1></p>
</FORM>
</BODY>

Cheers!!
0
 

Author Comment

by:Chris_Granger
Comment Utility
Great thanks for this. I think it was just a stupid mistake on my part. Problemo solved. Would love to ask a couple more questions but I guess I should close this and startup a new question., not really sure of ettiquette here. Let me know, if you want to solve the two questions below I'll double the points, otherwise I'll give you the 125 and repost new question...

FYI: Questions will be.
1. Using the working sample above, how do I actually post the form to another page when I wish to?
2. If there are other fields in the form, how do I capture and add these to the form each time it is updated via the selection of country?

Many Thanks
0
 
LVL 21

Expert Comment

by:ap_sajith
Comment Utility
Go through the script posted below. It shows you how to capture data and to post to pages dynamically..

<SCRIPT LANGUAGE=javascript>
<!--
function submitForm(bType)
{
//bType can take in True or False//
if (bType){
document.frmCountry.action='test1.asp'}
else{
document.frmCountry.action='test.asp'}
// Submit the form to the chosen page//
document.frmCountry.submit()
}

function submitCountry(){
document.frmCountry.hidValueToCapture.value=document.frmCountry.Country.options[document.frmCountry.Country.selectedIndex].value;
alert(document.frmCountry.hidValueToCapture.value);
submitForm(true);
}
function submitState(){
document.frmCountry.hidValueToCapture.value=document.frmCountry.State.options[document.frmCountry.State.selectedIndex].value;
submitForm(false);
}

//-->
</SCRIPT>

</HEAD>
<BODY>
<FORM method=POST id=frmCountry name=frmCountry>
<SELECT  name="Country" onChange="submitCountry()">
   <OPTION VALUE=CANADA>CANADA</OPTION>
   <OPTION VALUE=US>US</OPTION>
   <OPTION VALUE=UK>UK</OPTION>
   <OPTION VALUE=UAE>UAE</OPTION>
</SELECT><br>
<SELECT  name="State" onChange="submitState()">
   <OPTION Value=ABC>ABC</OPTION>
   <OPTION Value=DEF>DEF</OPTION>
   <OPTION Value=GHI>GHI</OPTION>
</SELECT><br>
<p>
<INPUT type="hidden" value="" id=hidValueToCapture name=hidValueToCapture>
<INPUT type="button" value="Submit" id=bSubmit name=bSubmit onclick="submitForm();"></p>
</FORM>
</BODY>

Cheers!!
0
 

Author Comment

by:Chris_Granger
Comment Utility
That is fantastic, a million thankyous.  :-)  Just wondering, as far as capturing and reloading (when it posts to itself), how is this done with a standard text field with user input, such as:
<input name="firstname" type="text" size="25" maxlength="25" value="">

Cheers
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 21

Accepted Solution

by:
ap_sajith earned 250 total points
Comment Utility
In an asp page, you can use the request.form method to capture the form data..

eg:

<input name="firstname" type="text" size="25" maxlength="25" value="<%=Request.Form("firstname")%>">

Cheers!!
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
An additional comment here. I am pretty sure that the code in the orginal question came from a post that I had made in the past to show how to have multiple dependant selects. The problems you have are resulting from not working with the complete set of code. I am including it here for reference (it was designed to be tested with the Northwind database:



<%@ Language = VBScript %>
<%Option Explicit%>
<%Response.Buffer = True%>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<%
dim  strDataPath, strConnectString, objConnection, strCountry, strRegion, strCity, objRS, strSelected

'set connection strings for entire application
strDataPath = server.MapPath("NorthWind.mdb")
strConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;"_
                  + " Data Source= " & strDataPath & ";"_
                  + " Mode=Share Deny None;User Id=admin;PASSWORD=;"
                  
if not IsObject("ojbConnection") then
      set objConnection=Server.CreateObject("ADODB.Connection")
      objConnection.ConnectionTimeout = 15
      objConnection.CommandTimeout =  10
      objConnection.Mode = 3 'adModeReadWrite
      if objConnection.state = 0 then
            objConnection.Open strConnectString
      end if
end if

strCountry = Request.Form("Country")
strRegion = Request.Form("Region")
strCity = Request.Form("City")

sub makeCountry()
      if not isObject("objRS") then
            set objRS=Server.CreateObject("ADODB.RecordSet")
      end if
      if objRS.state <> 0 then
            objRS.close
      end if
      objRS.Open "SELECT DISTINCT Country  FROM Customers ORDER BY Country",objConnection,3,3
      Response.Write("<option></option>" & VBCRLF )
      do while not objRS.EOF
            if objRS("Country") = strCountry then
                  strSelected = " Selected "
            else
                  strSelected = ""
            end if
            Response.Write("<option" & strSelected & ">" & objRS("Country") & "</option>" & VBCRLF )
            objRS.MoveNext
      loop
      objRS.Close
      set objRS=Nothing
end sub

sub makeRegion()
      if strCountry <> "" then
            if not isObject("objRS") then
                  set objRS=Server.CreateObject("ADODB.RecordSet")
            end if
            if objRS.state <> 0 then
                  objRS.close
            end if
            objRS.Open "SELECT DISTINCT Region FROM Customers WHERE Country = '" & strCountry & "' ORDER BY Region",objConnection,3,3
            if objRS.eof then
                  Response.Write("<option>No Regions Found</option>")
            else
                  Response.Write("<option>Select Region Now</option>" & VBCRLF )
                  do while not objRS.EOF
                        if objRS("Region") = strRegion then
                              strSelected = " Selected "
                        else
                              strSelected = ""
                        end if
                        Response.Write("<option" & strSelected & ">" & objRS("Region") & "</option>" & VBCRLF )
                        objRS.MoveNext
                  loop
            end if
            objRS.Close
            set objRS=Nothing
      else
            Response.Write("<option>Select a Country First</option>")
      end if
end sub

sub makeCity()
      if strRegion <> "Select a Country First" AND  strRegion <> "Select Region Now" AND strRegion <>"" then
            if not isObject("objRS") then
                  set objRS=Server.CreateObject("ADODB.RecordSet")
            end if
            if objRS.state <> 0 then
                  objRS.close
            end if
            objRS.Open "SELECT DISTINCT City FROM Customers WHERE Region = '" & strRegion & "' ORDER BY City",objConnection,3,3
            if objRS.eof then
                  Response.Write("<option>No Cities Found</option>")
            else
                  Response.Write("<option>Select City Now</option>" & VBCRLF )
                  do while not objRS.EOF
                        if objRS("City") = strCity then
                              strSelected = " Selected "
                        else
                              strSelected = ""
                        end if
                        Response.Write("<option" & strSelected & ">" & objRS("City") & "</option>" & VBCRLF )
                        objRS.MoveNext
                  loop
            end if
            objRS.Close
            set objRS=Nothing
      else
            Response.Write("<option>Select a Region First</option>")
      end if
end sub
%>

<SCRIPT LANGUAGE=javascript>
<!--

function submitCountry(){
      var objForm = document.forms[0];
      objForm.elements['Region'].selectedIndex=0;
      objForm.elements['City'].selectedIndex = 0;
      objForm.submit();
}
function submitRegion(){
      var objForm = document.forms[0];
      objForm.elements['City'].selectedIndex = 0;
      objForm.submit();
}

//-->
</SCRIPT>

</HEAD>
<BODY>
<FORM action="MultiSelect.asp" method=POST id=form1 name=form1>
<SELECT  name="Country" onChange="submitCountry()">
      <%call  makeCountry%>
</SELECT><br>
<SELECT  name="Region" onChange="submitRegion()">
      <%call makeRegion%>
</SELECT><br>
<SELECT  name="City">
      <%call makeCity%>
</SELECT><br>

<p><INPUT type="submit" value="Submit" id=submit1 name=submit1></p>
</FORM>
</BODY>
<%
objConnection.Close
set objConnection = Nothing
%>

</HTML>


As for submitting the data once the form was complete, I would recommend, as has already been discussed above, just changing the action of the form via javascript.

Fritz the Blank
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
This is how I dealt with the final submission:

function submitForm(){
      var objForm = document.forms[0];
      objForm.action = "http://www.FairfieldConsulting.com/processform.asp"
      return true;
}


Then on your form tag, do this:

<FORM action="" method=POST id=form1 name=form1 onSubmit="return submitForm()">


FtB
0
 

Author Comment

by:Chris_Granger
Comment Utility
Thanks fritz, yes i modified some of that Northwind example to get the dropdown lists operating, added a request.form statement to populate an array to keep the form fields state when subbmitting to itself and added the following for the two types of submit it uses:

function submitForm(bType)
{
if (bType){
document.form1.action='myself.asp'}
else{
document.form1.action='nextpage.asp'}
document.form1.submit()
}

I guess adding the 'return' to the function is good practice?

cheers.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
If you call it from the form tag with the onSubmit method, adding a return true or return false is the best way to get predictable results.

The way that I had it origianally should have maintained state. Did that not work for you?


--FtB
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now