Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set an ASP variable in javascript

Status
Not open for further replies.

nmath

Programmer
Dec 12, 2003
47
US
I am using javascript and ASP together and I am having a hard time setting a variable. I want to set an ASP varible to a certain value inside of a javascript function. Here is my code:

function setMeOR() {
<%
selection = &quot;OR&quot;
%>
//do some error checking to see if I get in this function
if(1==1)
alert(&quot;I am in the or function&quot;);
}

I execute the function but the variable doesn't seem to get set. [santa]
 
What you want to do is place a hidden text box on your asp page. Something like:

<form name=yourForm id=yourForm>
<input type=&quot;hidden&quot; id=&quot;txtSelection&quot; name=&quot;txtSelection&quot;>
</form>

Then in your javascript:

function setMeOR() {

document.yourForm.txtSelection.value = &quot;OR&quot;;
//do some error checking to see if I get in this function
if(1==1)
alert(&quot;I am in the or function&quot;);
}

You can then check the value of the hidden text box throught the request object.
 
nmath,

To follow up cglinn's comments...

Assuming that you are using your javascript for client-side coding, you cannot set an ASP variable in client-side code. The ASP code is first compiled on the server and is never seen by the browser (which is where the client-side code operates). If you wanted to set a variable in client-side (javascript) code, it needs to be as cglinn suggested, using an HTML variable such as a hidden variable.

-----------------------------------------------------------------------------------------------------
&quot;If you can't explain something to a six-year-old, you really don't understand it yourself.&quot;
-- Albert Einstein
 
Thank you! I have decided that using javascript is going to be way more trouble than it is worth for this particular application. The client/server issues are creating too many problems. I am going to stick to ASP and post variables. Thanks again! Have a wonderful holiday! [santa]
 
JavaScript is great for doing client-side data entry testing. Otherwise, all data testing has to be done on the server-side and if it's bad, then you have to send another page reflecting that failure back to the client's browser. If there are slow connections, it is awkward.

For example, if the user hits the submit button but some or all of the required data isn't entered, you can detect that with JavaScript functions, halt the submit, give a descriptive alert of the problem and what to do, then return the page's control back to the browser, all without consulting your busy server.

On the negative side, JavaScript is very finicky and and can be a hassle to code correctly. Assume the wrong data type and it can fail without a hint of what's wrong and you have to test repeatedly until you track down what's wrong. Make one tiny adjustment and a working function can fail.

Keep at it and you'll get more comfortable with each language's strengths and weaknesses.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top