Solved

Assigning ASP variables to Java/form values

Posted on 2001-06-13
11
288 Views
Last Modified: 2008-03-03
My newness is showing.  I know how to assign values to Java variables and to Input boxes on the form.  I need to go the other way... I need to get the value of a variable defined in JavaScript into a variable in ASP (<% %> - for server-side execution).  While I'm at it, I know how to get/set a value using JavaScript.  How do I access that value with ASP?

For example:

function DoIt()
{
var  cab1Sel = document.form1.sel1.options[document.form1.sel1.selectedIndex].text;

<% dim MyVal
MyVal= ???cab1Sel???%>
}
How do I assign MyVal to the Java (cab1Sel) value?


ALSO

function DoIt()
{
document.form.txtInput.text="DESIRED TEXT"

<% dim MyVal
MyVal= ???document.form.txtInput.text???%>
}
How do I assign MyValue to the value that is in the Input text box?
0
Comment
Question by:rwfjr
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 7

Expert Comment

by:daniel_c
Comment Utility
Use <input type=hidden> to store your Javascript's value.
0
 
LVL 7

Expert Comment

by:daniel_c
Comment Utility
Wants example? Here you go:

<HTML>
<BODY>
<SCRIPT LANGUAGE=JAVASCRIPT>
function DoIt()
{
var  cab1Sel = document.form1.sel1.options[document.form1.sel1.selectedIndex].text;
document.form1.tValue = cab1Sel;
}
</SCRIPT>
<form name=form1 action="test.asp" method = "post">
<select name=sel1 OnChange="DoIt()">
<option value=1 selected>One
<option value=2>Two
</select>
<input type=submit>
<input type=hidden name=tValue>
</form>
</BODY>
</HTML>

On test2.asp:
<%
   Response.write "Javascript value: " & Request("tValue")
%>

Hope this helps!

Cheers,

<%=Daniel%>
0
 
LVL 3

Expert Comment

by:yas022100
Comment Utility
No, since ASP <% %> is created before cashed into client's page, you cannot get the value directly from Javascript variables.  You can however, pass the value as parameter or part of <Form> input value and pass back to the page to get the value. like..

Page1.asp
**************
<%
   sValue1 = Request.form("hdn1")
   sValue2 = Request.querystring("value2")
%>
<HTML>
<BODY>
<Form Name=frmInput Method=post Action=Page1.asp onSubmit=PopulateIt()>
<Input Type=Hidden Name=hdn1 Value="">
</Form>
<Script Langauge=javascript>
   var sVariable1 = "ThisValue";
   var sVariable2 = "That Value";
   function PopulateIt()
   {
      frmInput.hdn1.value = sVariable1
      frmInput.action = 'Page1.asp?value2=' + escape(sVariable2)
   }
</Script>

This is simple example that will pass the value from Javascript back to ASP's server-side script variable.

You will use similar method to pass value back

Does this make sense?
0
 
LVL 1

Author Comment

by:rwfjr
Comment Utility
Specifically what I'm trying to do is get a value from an option (drop down) box.  I would like to change the value of another box based on the user's selection.  I would PREFER if at all possible to stay on the form so that when the user clicks back they go out of my page altogether.

For Daniel C - I tried putting the value in a hidden box, but I can't seem to GET that value from the form (without submitting it).

For yas, the method would need to be GET since you are forming the response on the action line as a get.  Will this cause the browser to "stay put" or will it "advance" a page causing the back to stay on my page?

In either event, short of passing the value through a form GET/POST I don't see the value going from the form field (or Java) into ASP (<% %>).  

If I assign it I use:
txtInput="<%=varASP%>"

What I need is something like:
"<%varASP=%>"=txtInput
Which, of course, throws an error.

0
 
LVL 7

Expert Comment

by:daniel_c
Comment Utility
Post your code, and let's fix it for you. ^_^
0
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

 
LVL 19

Accepted Solution

by:
webwoman earned 100 total points
Comment Utility
Submitting the form and passing the value to the asp file is the ONLY way you're going to get it there. As has been stated previously, asp runs on the SERVER, BEFORE the browser even gets to the javascript. If you want to do something else in asp, you have to pass the value from your current page in the browser (client) and back to another asp page (server), then output the results to another HTML/javascript (client) page for the user to see it.

Two separate things. Once it's done running on the server, that page is DONE -- you can't run it again without submitting it back to the server.
0
 
LVL 3

Expert Comment

by:yas022100
Comment Utility
Depending on the amount of data, you could store all in Client as variables then, retrieve them as user selects.  That would be a typical way to avoid the trip back to the server to retrieve the data.

Something like..

<%

  '** get data, then..
  sVariableString = "var aValue = new Array();"
  sOptionString = ""

  Do While Not rs.eof
      sValue = rs("ValueField")
      nID = rs("IDField")
      sOptionString = sOptionString & "<Option ID=" & nID & ">" & nID & "</Option>"
      sVariableString = sVariableString & "aValue[" & nID & "] = """ & sValue & """;"
  Loop
%>

<HTML>
<BODY>
<FORM Name=frmInput ID=frmInput>
<Select Name=slctThis onClick=ChangeIt(this)><%=sOptionString%></Select><BR>
<Input Type=Text Name=txtThis ID=txtThis>
</Form>
<Script Language=Javascript>
<%=sVariableString%>
   function ChangeIt(obj)
   {
      var nID = obj[obj.selectedIndex].id
      var sValue = aValue[aID]
      frmInput.txtThis.value = sValue
   }
</Script>

There, you will dynamically changing text box according to user selection on dropdown list

Something like this?
0
 
LVL 9

Expert Comment

by:TTom
Comment Utility
Actually, I believe there IS a way to do this using remote scripting.  I think that allows you to run script on the server in response to client side actions, but it is not something for the faint of heart.

If you want to pursue this, check out msdn.microsoft.com/scripting (the Remote Scripting link).  It may be more trouble than it is worth, but I believe it's the only possible way.

Tom
0
 
LVL 1

Author Comment

by:rwfjr
Comment Utility
Thanks webwoman.  I was afraid of that.  If that's what it takes, it's good to know.

Yas, you lost me.  Sorry.

Tom, I will check it out tomorrow.

Daniel C, here is code that simulates what I need:

<%@Language=VBScript%>
<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD>


<BODY>
<FORM name="form1" >
<input type="hidden" name="txtCab" value="" size="44">

<Table align="Left" cellspacing="5" cellpadding="5" border="0">
   <TR>
      <TD>Select one</TD>
      <TD> <select class="INPUT" name="sel1" onChange="sel1Change();">
     <option value="0">Select Cabinet</option>
     <option value="1">Item 1</option>
     <option value="2">Item 2</option>
     <option value="3">Item 3</option>
     <option value="4">Item 4</option>
     </select></TD>
   </TR>
</Table>
<input type="text" name="txtSelected" value="" size="44">

</FORM>

</BODY>
</HTML>
<SCRIPT language="JavaScript">

function sel1Change()
{
         
     var  strSel1 = document.form1.sel1.options[document.form1.sel1.selectedIndex].text;
     //the following javascript works.
     document.form1.txtSelected.value=strSel1;
     //However, I need to perform a server side function using the selected value..
     <%dim strSelected
      '--------------need the value for Server calculation -------------
      %>
      //"XXX" needs to be serverfunction(strSel1) but I don't know how to pass the value strSel1
     document.form1.txtSelected.value="<%="XXX"%>";
}
</SCRIPT>


0
 
LVL 7

Expert Comment

by:daniel_c
Comment Utility
Okay, in your case, you have to submit the value anyway.

Probably, this is what you want.... ^_^

<%@Language=VBScript%>
<%
     if Request("from_sel") <> "" then
          sel_1 = Cint(Request("from_sel")) * 10          
     else
          sel_1 = 0
     end if
     Response.Write "sel_1: " & sel_1
     
%>
<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD>
<BODY>
<SCRIPT language="JavaScript">
function sel1Change()
{
         
    var  strSel1 = document.form1.sel1.options[document.form1.sel1.selectedIndex].value;
    document.form1.action = "a.asp?from_sel=" + strSel1;
    document.form1.submit();
   
    //the following javascript works.
    //document.form1.txtSelected.value=strSel1;
    //However, I need to perform a server side function using the selected value..
    //<%dim strSelected
     //'--------------need the value for Server calculation -------------
     //%>
     //"XXX" needs to be serverfunction(strSel1) but I don't know how to pass the value strSel1
    //document.form1.txtSelected.value="<%="XXX"%>";
}
</SCRIPT>

<FORM name="form1" method=post action="to_other_page.asp">
<input type="hidden" name="txtCab" value="" size="44">

<Table align="Left" cellspacing="5" cellpadding="5" border="0">
  <TR>
     <TD>Select one</TD>
     <TD>
     <select class="INPUT" name="sel1" onChange="sel1Change();">
    <option value="0">Select Cabinet</option>
    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
    <option value="3">Item 3</option>
    <option value="4">Item 4</option>
    </select></TD>
  </TR>
</Table>
<input type="text" name="txtSelected" value="<%=sel_1%>" size="44">

<br>
<input type=submit>
</FORM>

</BODY>
</HTML>

Just simple example.
Let me know if you still need help...

Cheers,

<%=daniel%>

0
 
LVL 20

Expert Comment

by:Silvers5
Comment Utility
her's an example of what you are trying to do:

http://www.programmersresource.com/articles/listbox.asp
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

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 have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now