How can I refresh page with values from form fields

  • Follow


I am trying to get a page to refresh based on the number of minutes and 
seconds entered into a form on the same page. If I change the values of the 
form fields I want the new values to take effect during the next scheduled 
refresh.

I just started learning javascript so there are probably a bunch of things 
wrong with the following but hopefully it will give an idea of the code I am 
trying to write. Basically I want some functions that read the minutes and 
seconds from a form and calculate the refresh rate.

<script type="text/javascript">
function secondsValue(){
var numSeconds = document.forms[myForm].elements[seconds].value;
return numSeconds;
}
function minutesValue(){
numMinutes = document.forms[myForm].elements[minutes].value;
return numMinutes;
}
function refreshRate(){
var refreshRate = (((minutesValue()*60)+secondsValue())*1000);
return refreshRate;
}
</script>

and then use those functions output to refresh the page using either a meta 
refresh or setTimeout method. Something to the effect of either of the 
following methods which I have no idea if they are correct:

<script type="text/javascript">
setTimeout("location.replace('page?minutes=minutesValue()&seconds=secondsValue()')",refreshRate());
</script>

or

<meta http-equiv="REFRESH" content="<script>refreshRate()</script>; 
URL=page.php?minutes=<script>minutesValue()</script>&seconds=<script>secondsValue()</script>" 
/>

Appreciate any help in getting this to work using either of the above 
methods or any alternatives.
 

0
Reply Jay 10/14/2007 7:19:11 PM

Jay wrote:
> I am trying to get a page to refresh based on the number of minutes and 
> seconds entered into a form on the same page. If I change the values of the 
> form fields I want the new values to take effect during the next scheduled 
> refresh.
> 
> I just started learning javascript so there are probably a bunch of things 
> wrong with the following but hopefully it will give an idea of the code I am 
> trying to write. Basically I want some functions that read the minutes and 
> seconds from a form and calculate the refresh rate.
> 
> <script type="text/javascript">
> function secondsValue(){
> var numSeconds = document.forms[myForm].elements[seconds].value;

Where are `myForm' and `seconds' defined?  These are *identifiers* here,
you know, *placeholders* for values that are expected to be strings or be
convertible to string.  If you want to access the form named `myForm', you
have to write

  document.forms["myForm"]


instead.  Much the same for (named) form controls:

  document.forms["myForm"].elements["seconds"].value

However, usually you would not need most of this as you can pass a reference
to the form object or to the control object to the function/method.  See my
example.

> return numSeconds;
> }
> function minutesValue(){
> numMinutes = document.forms[myForm].elements[minutes].value;

See above.

> return numMinutes;
> }
> function refreshRate(){
> var refreshRate = (((minutesValue()*60)+secondsValue())*1000);
> return refreshRate;
> }
> </script>
> 
> and then use those functions output to refresh the page using either a meta 
> refresh or setTimeout method. Something to the effect of either of the 
> following methods which I have no idea if they are correct:
> 
> <script type="text/javascript">
> setTimeout("location.replace('page?minutes=minutesValue()&seconds=secondsValue()')",refreshRate());
> </script>

In order for this to work at all, two things would need to be changed:

1. Call syntax is not evaluated inside string literals.

    window.setTimeout(
      "window.location.replace('page?minutes="
        + minutesValue()
        + "&seconds="
        + secondsValue()
        + "')",
      refreshRate());

2. You would need to parse the values from the query part of the URI.

   That can be a good exercise to understand string parsing in JS/ES,
   but from what you have posted, I would recommend you learn the
   basics first.

> or
> 
> <meta http-equiv="REFRESH" content="<script>refreshRate()</script>; 
> URL=page.php?minutes=<script>minutesValue()</script>&seconds=<script>secondsValue()</script>" 
> />

Pure phantasy syntax, can't work.  (Nothing wrong with phantasies in
general, really, but please don't have them in [software] engineering.
Stick to the reference material instead.)

http://jibbering.com/faq/

> Appreciate any help in getting this to work using either of the above 
> methods or any alternatives.

Take a look at my example, hopefully it provides some insight.  If you have
any questions regarding it, please do not hesitate to ask:

http://pointedears.de/scripts/test/dom/countdown


PointedEars
0
Reply Thomas 10/14/2007 8:07:04 PM


Thanks for the info, it helped me get it working.  
0
Reply Jay 10/15/2007 12:26:46 AM

2 Replies
349 Views

(page loaded in 4.926 seconds)

Similiar Articles:













7/26/2012 2:17:03 PM


Reply: