Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2003
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help: Holding values in form after error checking!

    Ok,

    I need a little help with keeping the values of my form after I do form validation. If the form returns an error than it wipes out my values in this javascript linked dropdowns I am using.

    First of you can see the form @ www.object5.com/test/javascript.asp

    Do the following to see the problem:

    Fill out the form leaving out the partNumber. Also when you get to ProductType select Pt Items and for brand select other. Once you do this a text box will appear. You can put anything...test,ljjjkj, whatever you like.

    Once you have done this click submit. If an error occurs it wipes out all of the value in the drop own menues. So my question is how do I hold those values? I pasted the javascript code below.


    Code---------------------------------------------
    </head>

    <SCRIPT language="javascript">
    <!-- hide jscript from old browsers ---

    function refresh_screen()
    {
    window.location.reload()
    }
    /****** this object holds all of the key data **********/
    function Link(name, url)
    {
    this.name = name;
    this.title = new Array();
    this.url = new Array();
    }
    /**************************************************************/
    var names = new Array ();
    var temp = new Array ();
    var temp2 = new Array ();
    var link = new Link ();
    var final_list = new Array ();
    /******** array declaration... it holds all of the data for the menus ****/
    var menu = new Array (
    "Bearings*" +
    "|Choose Bearing Brand#" +
    <%objRS6.MoveFirst%><%Do While Not objRS6.EOF%>
    "<%=objRS6("BrandFull")%>|<%=objRS6("BrandFull")%>#" +
    <%objRS6.MoveNext%><%Loop%>
    "4|Other",
    "PT Items*" +
    "|Choose PT Brand#"+
    <%objRS7.MoveFirst%><%Do While Not objRS7.EOF%>
    "<%=objRS7("BrandFull")%>|<%=objRS7("BrandFull")%>#"+
    <%objRS7.MoveNext%><%Loop%>
    "4|Other",
    "Seals*" +
    "|Choose Seals Brand#"+
    <%objRS8.MoveFirst%><%Do While Not objRS8.EOF%>
    "<%=objRS8("BrandFull")%>|<%=objRS8("BrandFull")%>#"+
    <%objRS8.MoveNext%><%Loop%>
    "4|Other");

    /*****************************************************************/
    function updateMenus ( what ) {
    var n = what.selectedIndex;
    what.form.brand.length = final_list[n].title.length;
    for (var x = 0; x < what.form.brand.length; x++)
    {
    what.form.brand.options[x].text = final_list[n].title[x];
    what.form.brand.options[x].value = final_list[n].url[x];
    }
    what.form.brand.selectedIndex = 0;
    }
    /**************************************************************/
    function give_names ()
    {
    document.myForm.producttype.length = names.length;
    document.myForm.brand.length = final_list[0].title.length;
    for ( var i=0; i<names.length; i++ )
    document.myForm.producttype.options[i].text = final_list[i].name;
    for (var x=0; x<final_list[0].url.length; x++)
    document.myForm.brand.options[x].value = final_list[0].url[x];
    for (var x=0; x<final_list[0].title.length; x++)
    document.myForm.brand.options[x].text = final_list[0].title[x];

    }
    /**************************************************************/
    function createMenus () {

    for ( var i=0; i < menu.length; i++ )
    {
    names[i] = menu[i].split("*");

    link = new Link(names[i][0]);
    temp[i] = names[i][1].split("#");
    final_list[i] = link;
    for (var x=0; x<temp[i].length; x++)
    {
    temp2[x] = temp[i][x].split("|");
    final_list[i].url[x] = temp2[x][0];
    final_list[i].title[x] = temp2[x][1];
    }
    }
    give_names();
    }

    /**************************************************************/

    // end jscript hiding -->
    </SCRIPT>




    <body onLoad="createMenus(); document.myForm.PartNumber.focus()">

    -----------------------------------------------------------------------------------


    Any help will be most appreciated!

    Thanks,

    Tsbarnes

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I get some type of 404 from that link
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #3
    New Coder
    Join Date
    Jan 2003
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Corrected Link:

    It would help if I verified the right link I guess. Well here is the correct link:

    http://www.object5.com/test/javascript/postitem.asp

    Sorry about that!

    Thanks,

    Tsbarnes

  • #4
    New Coder
    Join Date
    Jun 2002
    Location
    Northern California
    Posts
    50
    Thanks
    2
    Thanked 0 Times in 0 Posts
    It looks like your doing server side validation with ASP, in which case you probably need to save the variables then put them back when the page returns.

    I usually use client side, add this between your script tags...

    Code:
    function checkFields(){
    frm = document.myForm
    
    if(frm.partnumber.value == ""){alert("Part Number Required");frm.partnumber.focus();return false;}
    if(frm.Quantity.value == ""){alert("Quantity Required");frm.Quantity.focus();return false;}
    return true;
    }
    then put onSubmit="checkFields()" into the <form> tag.

    When they hit the submit button it will check those 2 fields for data, if either is blank it will pop an alert and make them go back, and won't allow the form to go further. The reason I didn't code for the option boxes is that they don't have a choice of leaving it blank, it defaults to the first value in the list.

    Hope this helps some.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is definitely an ASP question.

    The answer is fairly basic, but I made a couple of functions to make this easier:

    Code:
    <%
    Function IsChecked(val1,val2)
         If val1 = val2 Then IsChecked = " checked=""checked"""
    End Function
    
    Function IsSelected(val1,val2)
         If val1 = val2 Then IsSelected = " selected=""selected"""
    End Function
    %>
    Now in your form, you can simply do this (as an example):

    Code:
    <%
    ' Put this somewhere in your ASP...
    Dim Unit : Unit = Request.Form("Unit")
    %>
    
    <select name="Unit">
           <option value="">
           <option  value="EA - Each"<% = IsSelected("EA - Each",Unit) %>>EA - Each
           <option  value="PR - Pair"<% = IsSelected("PR - Pair",Unit) %>>PR - Pair
           <option  value="BX - Box"<% = IsSelected("BX - Box",Unit) %>>BX - Box
           <option  value="FT - Foot"<% = IsSelected("FT - Foot",Unit) %>>FT - Foot
           <option  value="AY - Assembly"<% = IsSelected("AY - Assembly",Unit) %>>AY - Assembly
           <option  value="LK - Link"<% = IsSelected("LK - Link",Unit) %>>LK - Link
           <option  value="IN - Inches"<% = IsSelected("IN - Inches",Unit) %>>IN - Inches
    </select>
    You could also say:

    Code:
    <% If Request.Form("Unit") = "LK - Link" Then Response.Write(" selected=""selected""") %>
    for each option, but as you can see using the functions I wrote is shorter- especially if you populate the dropdowns dynamically from an array or database, since in the latter case you'd only have to use it once as you loop through. Example...:

    Code:
    <% @Language="VBScript" %>
    <% Option Explicit %>
    <%
    Dim Unit : Unit = Request.Form("Unit")
    
    Function IsSelected(val1,val2)
         If val1 = val2 Then IsSelected = " selected=""selected"""
    End Function
    
    Sub WriteUnitDropdown()
       Dim unitstring, unitarray, i
       unitstring = "EA - Each,PR - Pair,BX - Box,FT - Foot,AY - Assembly,LK - Link,IN - Inches"
       unitarray = Split(unitstring,",")
    
       Response.Write("<select size=""1"" name=""Unit"">" & vbCrLf)
       Response.Write("       <option value=""""></option>" & vbCrLf)
    
       For i = 0 to UBound(unitarray)
          Response.Write("       <option value=""" & unitarray(i) & """")
          Response.Write(IsSelected(unitarray(i),Unit))
          Response.Write(">" & unitarray(i) & "</option>" & vbCrLf)
       Next
    
       Response.Write("</select>" & vbCrLf)
    End Sub
    %>
    <form id="myform" action="test.asp" method="post">
    <% Call WriteUnitDropDown() %>
    <input type="submit" value="Submit" />
    </form>
    Last edited by whammy; 01-07-2003 at 10:50 PM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Taking this a step further, you could even create a subroutine to write dropdowns in general, like so - it does slow it down a little bit, but it makes stuff real easy to maintain...


    Code:
    <% @Language="VBScript" %>
    <% Option Explicit %>
    <% Response.Buffer = True %>
    <%
    Dim submitnum ' Number of times form has been submitted
    submitnum = Request.Form("submitnum")
    submitnum = submitnum + 1
    
    Dim Unit       : Unit      = Request.Form("Unit")
    Dim Whatever   : Whatever  = Request.Form("Whatever")
    
    Function IsSelected(val1,val2)
         If val1 = val2 Then IsSelected = " selected=""selected"""
    End Function
    
    Sub WriteDropdown(strName,strValue,strNameValuePair,indent)
       Dim ddCounter, ddArray, ddArray2, ddIndent
       ddIndent = indent & indent & indent
       ddArray = Split(strNameValuePair,",")
       Response.Write(ddIndent & "<select name=""" & strName & """>" & vbCrLf)
       Response.Write(ddIndent & indent & "<option value=""""></option>" & vbCrLf)
       For ddCounter = 0 to UBound(ddArray)
          ddArray2 = Split(ddArray(ddCounter),"|")
          Response.Write(ddIndent & indent & "<option value=""" & ddArray2(0) & """")
          Response.Write(IsSelected(ddArray2(0),strValue))
          Response.Write(">" & ddArray2(1) & "</option>" & vbCrLf)
       Next
       Response.Write(ddIndent & "</select>" & vbCrLf)
    End Sub
    
    Sub WriteAllDropdowns()
       ' Unit
       Dim UnitDropdownValues
       UnitDropdownValues = "EA|Each,PR|Pair,BX|Box,FT|Foot,AY|Assembly,LK|Link,IN|Inches"
       Call WriteDropDown("Unit",Unit,UnitDropDownValues,"   ")
       If submitnum > 1 AND Unit = "" Then Response.Write(" * Required")
       Response.Write("        <br />" & vbCrLf)
       'Whatever
       Dim SomeOtherDropdownValues
       SomeOtherDropdownValues = "1|One,2|Two,3|Three,4|Four,5|Five"
       Call WriteDropDown("Whatever",Whatever,SomeOtherDropdownValues,"   ")
       If submitnum > 1 AND Whatever = "" Then Response.Write(" * Required")
       Response.Write("        <br />" & vbCrLf)
    End Sub
    %>
    <html>
    <head>
       <title>Example</title>
    </head>
    <body>
       <div>
          <form id="myform" action="test.asp" method="post">
    <% Call WriteAllDropdowns() %>
             <input type="hidden" name="submitnum" value="<% = submitnum %>" />
             <input type="submit" value="Submit" />
          </form>
       </div>
    </body>
    </html>


    P.S. The forum put in a few funky line breaks in the code, and a weird space in "UnitDropDownValues"... FYI
    Last edited by whammy; 01-08-2003 at 01:16 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •