Solved

Why is the dropdown list selection not updating form?

Posted on 2003-11-01
9
280 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
ID: 9664912
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
ID: 9665020
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
ID: 9665256
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Chris_Granger
ID: 9665288
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
 
LVL 21

Accepted Solution

by:
ap_sajith earned 250 total points
ID: 9665411
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
ID: 9666523
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
ID: 9666619
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
ID: 9668090
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
ID: 9670536
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

685 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