#1
  1. No Profile Picture
    Contributing User
    ASP Discoverer (100 - 499 posts)

    Join Date
    Jan 2005
    Posts
    233
    Rep Power
    14

    Firing javascript from asp form


    I have the following code which works fine on the onclick however I want to automate things further and am having difficulty firing the javascript. The idea is that when someone selects their size the onclick javascript/ajax script them displays items for that size.

    However, their size might be known on entry to this page, held in a session variable (session("mysize"). If this is the case I want the size to be selected/displayed in the drop down box but also the items displayed.

    Can this be done?
    Code:
    <select name="sizegrid"  onchange="showstyle(this.value)">
     <option value="Select Your Size"  >Select Your Size</option>
    <%do while not rssize.eof%>
     
       <option value="<%=rssize("sizedesc")%>"
      <%if session("mysize") <> "" then
           if lcase(rssize("sizedesc")) = lcase(session("mysize")) then
    	   %>	   
    	    selected		
    	   <%end if
        end if%>
       ><%=rssize("sizedesc")%> </option>
       <%rssize.movenext
       loop
    %></select>
    I would like the showsize function to fired off if the size is known

    Thanks
    Moris
  2. #2
  3. Contributing User
    ASP Hero (2000 - 2499 posts)

    Join Date
    Nov 2003
    Location
    UK
    Posts
    2,280
    Rep Power
    314

    Firing javascript from asp form


    in the onchange i wouldn't use this.value a some browsers won't work with this.

    try something like this:

    Code:
    <select name="sizegrid" onchange="javascript:showstyle(this[this.selectedIndex].value);">
    	<option value="">Select Your Size</option>
    	<%do while not rssize.eof
    		%><option value="<%=rssize("sizedesc")%>"<%if CStr(lcase(rssize("sizedesc"))) = CStr(lcase(session("mysize"))) then%> selected<%end if%>><%=rssize("sizedesc")%> </option><%
    		rssize.movenext
    	loop%>
    </select>
    
    <! -- JAVASCRIPT -->
    <script language="javascript">
    <!--//
    function showstyle(field) {
    	if (field.length > 0) {
    		alert(field);
    	}
    }
    //-->
    </script>
    Hope this advise helps.

    If so please show your appreciation by adding reputation points (click gauge image on top right of this post and score).
  4. #3
  5. No Profile Picture
    Contributing User
    ASP Discoverer (100 - 499 posts)

    Join Date
    Jan 2005
    Posts
    233
    Rep Power
    14
    Hi
    Thanks for that tip.

    Were you addressing my question or just fixing the existing code. ( my javascript is limited)

    Moris

    Originally Posted by markoc
    in the onchange i wouldn't use this.value a some browsers won't work with this.

    try something like this:

    Code:
    <select name="sizegrid" onchange="javascript:showstyle(this[this.selectedIndex].value);">
    	<option value="">Select Your Size</option>
    	<%do while not rssize.eof
    		%><option value="<%=rssize("sizedesc")%>"<%if CStr(lcase(rssize("sizedesc"))) = CStr(lcase(session("mysize"))) then%> selected<%end if%>><%=rssize("sizedesc")%> </option><%
    		rssize.movenext
    	loop%>
    </select>
    
    <! -- JAVASCRIPT -->
    <script language="javascript">
    <!--//
    function showstyle(field) {
    	if (field.length > 0) {
    		alert(field);
    	}
    }
    //-->
    </script>
  6. #4
  7. Wizard Down Under
    ASP Discoverer (100 - 499 posts)

    Join Date
    Dec 2006
    Location
    Australia
    Posts
    250
    Rep Power
    221
    Code:
    <select name="sizegrid"  onchange="showstyle(this.value);showsize(variables);">
     <option value="Select Your Size"  >Select Your Size</option>
    <%do while not rssize.eof%>
     
       <option value="<%=rssize("sizedesc")%>"
      <%if session("mysize") <> "" then
           if lcase(rssize("sizedesc")) = lcase(session("mysize")) then
    	   %>	   
    	    selected		
    	   <%end if
        end if%>
       ><%=rssize("sizedesc")%> </option>
       <%rssize.movenext
       loop
    %></select>
    or to do it on loadup you could add:

    Code:
    <%if session("mysize") <> "" then%>
          <script language="javascript">showsize(variables);</script>
    	  <% end if%>
    or to do it a little neater, add this within the head:
    Code:
    <script language="javascript">
    function init() {
    <%if session("mysize") <> "" then%>
          showsize(variables);
    	  <% end if%>
    }</script>
    <body onload="init();">
    Last edited by Dr_Rock; February 12th, 2007 at 08:37 PM.
  8. #5
  9. Contributing User
    ASP Hero (2000 - 2499 posts)

    Join Date
    Nov 2003
    Location
    UK
    Posts
    2,280
    Rep Power
    314

    Firing javascript from asp form


    do you mean that if they select a size you want the page to reload and you have items to be displayed below the selectbox depending on the size selected?

    if so try this:

    Code:
    <%@ LANGUAGE=VBSCRIPT %>
    <%'Option Explicit%>
    
    <%
    if session("mysize") = "" then
    	if Request("mysize") <> "" then
    		session("mysize") = Request("mysize")
    	end if
    end if
    
    %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>New Page 2</title>
    </head>
    <script language="javascript">
    <!--//
    function showstyle(field) {
    	if (field.length > 0) {
    		var jumpto = "your_page_name.asp?mysize=" + field;
    		window.location.href = jumpto;
    	}
    }
    //-->
    </script>
    <body>
    <select name="sizegrid" onchange="javascript:showstyle(this[this.selectedIndex].value);">
    	<option value="">Select Your Size</option>
    	<%do while not rssize.eof
    		%><option value="<%=rssize("sizedesc")%>"<%if CStr(lcase(rssize("sizedesc"))) = CStr(lcase(session("mysize"))) then%> selected<%end if%>><%=rssize("sizedesc")%> </option><%
    		rssize.movenext
    	loop%>
    </select>
    
    </body>
    
    </html>
  10. #6
  11. Contributing User
    ASP Hero (2000 - 2499 posts)

    Join Date
    Nov 2003
    Location
    UK
    Posts
    2,280
    Rep Power
    314

    Firing javascript from asp form


    sorry got a bit wrong change the session part to:

    Code:
    <%
    if Request("mysize") <> "" then
    	session("mysize") = Request("mysize")
    end if
    %>
  12. #7
  13. No Profile Picture
    Contributing User
    ASP Discoverer (100 - 499 posts)

    Join Date
    Jan 2005
    Posts
    233
    Rep Power
    14
    Thanks Dr Rock

    I think it definitely needs to be in the onload() but I cannot seem to get it right i have modified your bit of code as follows
    and placed in the header section.
    function init() {
    <%if session("mysize") <> "" then%>
    showstyle(<%=session("mysize")%>);

    <%end if%>
    }

    I did not realize you could mix and match like that. Anyhow the above code is not working

    any suggestions

    Moris

    Originally Posted by Dr_Rock
    Code:
    <select name="sizegrid"  onchange="showstyle(this.value);showsize(variables);">
     <option value="Select Your Size"  >Select Your Size</option>
    <%do while not rssize.eof%>
     
       <option value="<%=rssize("sizedesc")%>"
      <%if session("mysize") <> "" then
           if lcase(rssize("sizedesc")) = lcase(session("mysize")) then
    	   %>	   
    	    selected		
    	   <%end if
        end if%>
       ><%=rssize("sizedesc")%> </option>
       <%rssize.movenext
       loop
    %></select>
    or to do it on loadup you could add:

    Code:
    <%if session("mysize") <> "" then%>
          <script language="javascript">showsize(variables);</script>
    	  <% end if%>
    or to do it a little neater, add this within the head:
    Code:
    <script language="javascript">
    function init() {
    <%if session("mysize") <> "" then%>
          showsize(variables);
    	  <% end if%>
    }</script>
    <body onload="init();">
  14. #8
  15. Wizard Down Under
    ASP Discoverer (100 - 499 posts)

    Join Date
    Dec 2006
    Location
    Australia
    Posts
    250
    Rep Power
    221
    Makesure you call the onload function within your body tag

    <body onload="init()">

    If that doesnt work, check your source of that page and make sure the javascript is being written by the asp.

    If it is then it has to be a javascript error.
  16. #9
  17. Wizard Down Under
    ASP Discoverer (100 - 499 posts)

    Join Date
    Dec 2006
    Location
    Australia
    Posts
    250
    Rep Power
    221
    you may need to add the following also (in red)
    Code:
    <%if session("mysize") <> "" then%>
    showstyle('<%=session("mysize")%>');
    
    <%end if%>
    }
  18. #10
  19. No Profile Picture
    Contributing User
    ASP Discoverer (100 - 499 posts)

    Join Date
    Jan 2005
    Posts
    233
    Rep Power
    14
    Hi

    Thanks guys

    Could not get it to work using the javascript but managed to think a little laterally and solved the problem using asp

    Thanks again

    Moris

    Originally Posted by Dr_Rock
    you may need to add the following also (in red)
    Code:
    <%if session("mysize") <> "" then%>
    showstyle('<%=session("mysize")%>');
    
    <%end if%>
    }

Similar Threads

  1. Javascript form validation help
    By jbruso in forum HTML, JavaScript And CSS Help
    Replies: 11
    Last Post: November 2nd, 2005, 02:21 AM
  2. Javascript Form validation not working
    By shadowspell in forum HTML, JavaScript And CSS Help
    Replies: 4
    Last Post: October 31st, 2005, 05:46 PM
  3. Javascript problem Trying to validate a form
    By mmarx in forum HTML, JavaScript And CSS Help
    Replies: 7
    Last Post: December 6th, 2004, 10:34 AM
  4. Form Validation using Javascript, JScript in ASP file
    By Barnes in forum ASP Development
    Replies: 7
    Last Post: July 1st, 2004, 01:22 PM
  5. Javascript limit date difference in form
    By seplix in forum HTML, JavaScript And CSS Help
    Replies: 0
    Last Post: March 21st, 2004, 06:52 PM

IMN logo majestic logo threadwatch logo seochat tools logo