PDA

View Full Version : Using javascript variables


gator88
10-03-2002, 12:19 AM
Hello, i attempt to learn more about javascript.

I am trying to see with the javascript how i can read and use a variable in a form.

Here is the asp code i use to do this with a database.



<%=Server %>forms/add=1&field1=<%= myvariable %>



I try see this with the javascript now.


I placed the whole thing in <script> and use variable name in brackets as i read earlier in place of the <% %>, but this doesn't work out.

How can i read myvariable using javascript instead of asp for this things?

Thanks for any pointers or links!

Les :confused:

whammy
10-03-2002, 01:35 AM
Basic example (assuming you're using ASP, from the tags you posted):

<script language="JavaScript" type="text/javascript">
<!--
alert("<% = variablename %>");
// -->
</script>

Keep in mind that javascript is (in the way you are using it) a client-side language and cannot read variables from the server, unless you have first created the variable in ASP as shown above and sent it to the client as HTML/Text. :)

You CAN use javascript on the server side in conjunction with ASP/VBScript - but I don't think that's what you're asking.

:D

gator88
10-03-2002, 09:59 AM
Hi Whammy, thanks for the reply :thumbsup:

You were right, this isn't what i am essentially trying to achieve.

I have form which takes variables using from database asp, i wish to convert this to javascript.



forms/add=1&field1=<%= myvariable %>



The bit about the server address doesn't matter now for this.

Looking at your reply, I think about how i can write my variable out in javascript, and realise this is more what i do.



<script language="JavaScript1.2" type="text/javascript">document.write(myvariable(Page[0], false)());</script>


This writes out variable to screen, how build it into form submit.

Not trying to use both ASP and JS variables in same form submit, just learning how to replace with JS for this kind of function.

Any further infos or links to try appreciated.

Thx

whammy
10-04-2002, 02:57 AM
Hmm... I don't know about any links, except you might want to look up the difference between client-side scripting and server-side scripting... basically any variable you get in asp can be written to HTML or javascript using the <% %> tags.

But you can't do the opposite!

I don't think I can explain it any more clearly than my first example... here's a couple more examples:

<p>
<% = somevariable %>
</p>

<input type="hidden" name="myhiddenASPvariable" value="<% = "myhiddenASPvariable %>" />

As you can see from those examples, you can send ASP variables from the server-side to the client side. But the only way you can then retrieve those variables is if you then submit them to the server-side again!

There are a couple of tricks where you can get around this sort of thing (in certain circumstances), but you generally want to use this method.

After reading your most recent post, I'm actually less clear on what you're trying to acheive. If this post doesn't answer your question, perhaps you should explain what you're trying to do in plain English (as clearly as possible), instead of code.

:D

gator88
10-04-2002, 12:04 PM
Hi Whammy, thanks a lot for replying, I think I have got confused issue.


field1=<%= Variable1 %>&field2=<%= Variable2 %>


This I am working on does not use ASP :o I try to find how to do the same thing using javascript.

I have a series of javascript variables which i can view on my page using:


<script language="JavaScript1.2" type="text/javascript">document.write(myvariable);</script>


I like to include the output of myvariable in the form using javascript, this is what i try so far:

field1=
<script language="JavaScript1.2" type="text/javascript">document.write(myvariable);</script>


See what i do wrong?

So, no ASP or anything, just in javascript how can this work? at the moment it just displays the form button with the variables underneath.

Thx for any help

Roy Sinclair
10-04-2002, 03:48 PM
Are you simply wanting to set the value of a form field using javascript? That's very easy:


<form name="myForm">
<input type="text" name="myText">
</form>
<script type="text/javascript">
document.forms.myForm.myText.value = "Some Text";
</script>

whammy
10-05-2002, 02:16 AM
I can't explain it any better without suggesting learning the difference between client-side and server-side scripting. Sorry... :eek:

gator88
10-05-2002, 10:50 AM
Originally posted by Roy Sinclair
Are you simply wanting to set the value of a form field using javascript? That's very easy:


<form name="myForm">
<input type="text" name="myText">
</form>
<script type="text/javascript">
document.forms.myForm.myText.value = "Some Text";
</script>


:thumbsup:

Roy's got it!, thats more or less exactly what i needed to see.

Thanks Roy and Whammy, i will let you know if it works :)

Thx