SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    You guys are going to laugh!

    Alright, I'm going to dig through the books, but I thought I'd post it anyway.

    Being the JavaScript ignorant that I am, I can't figure out how to place a JavaScript variable's value in the value attribute of and <input> tag.

    any help would be appreciated. The following is some sample code.

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[url=http://www.w3.org/TR/html4/loose.dtd]http://www.w3.org/TR/html4/loose.dtd[/url]">
    <
    html>
      <
    head>
        <
    title>Java Time offset retriever</title>
      </
    head>
      <
    body>
        <
    script language="javascript">
          var 
    localDate = new Date();
          var 
    timeOffset localDate.getTimezoneOffset();
          
    document.write("here = > " + (timeOffset 60 * -1));
        
    </script>
        <form action="timeOffset.php" method="post">
          <input type="text" name="timeOffset" value="document.write(timeOffset);" />
        </form>
      </body>
    </html> 
    John

  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, I found that this works, but I've got to ask if there is a simpler/more efficient way?

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[url=http://www.w3.org/TR/html4/loose.dtd]http://www.w3.org/TR/html4/loose.dtd[/url]">
    <
    html>
      <
    head>
        <
    title>Java Time offset retriever</title>
      </
    head>
      <
    body>
        <
    script language="javascript">
          var 
    localDate = new Date();
          var 
    timeOffset = (localDate.getTimezoneOffset() / 60 * -1);
        
    </script>
        <form name="form" action="timeOffset.php" method="post">
          <input type="text" name="timeOffset" value="" />
        </form>
        <script language="javascript">
          form.timeOffset.value = timeOffset;
        </script>
      </body>
    </html> 
    John

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes
    Code:
    <html>
    <head>
    	<title>Java Time offset retriever</title>
    	<script language="javascript">
    		function init()
    		{
    			setDateField( document.forms['form'].elements['timeOffset'] );
    		}
    		function setDateField( field )
    		{
    			field.value = new Date().getTimezoneOFfset() / 60 * -1;
    		}
    	</script>
    </head>
    <body onload="init();">
    	<form name="form" action="timeOffset.php" method="post">
    	<input type="text" name="timeOffset" value="" />
    	</form>	
    </body>
    </html>
    I like to load all onload-triggered scripts from a common init() function - just makes life easier.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #4
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh!

    The crazy part of it is, I understand VERY little of it (but, I'm glad it works! )

    Would you be willing to explain how this script manages to work on an obscure (to me) reference of "field"? I don't see the connection. Would you have to do something different with a form with multiple inputs of different names?
    John

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure, I'll "edumacate" you.

    First - init() runs when the page loads - we know that. So, what does init() do?

    setDateField( document.forms['form'].elements['timeOffset'] );

    Okay, what's happening here? We call setDateField() and send it one parameter. What are we sending? Why, a reference to the form element we want to receive the date! This answers your question about multiple inputs.

    Now, what does setDateField() do? It receives one parameter, named field.

    Now, it just uses that reference to give it a date value and format that you specified (which I refactored into 1 step, instead of two).

    Got all that?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, that makes things clearer! It was the order of the functions that confused me. but, it is late!
    John

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ya - functions only run when called. Their source order has no relevance, barring you don't try to call it before it's declared.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could also use Javascript to actually write the hidden input.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  9. #9
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You know what I'm doing this for!
    Could you provide an example?
    John

  10. #10
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML Code:
    ....
    <body>
    <form action="somefile.php"><div>
    <script type="text/javascript">
    <!--
    localDate = new Date();
    timezoneOffset = -Math.round(localDate.getTimezoneOffset() / 60);
    document.write('<input type="hidden" name="timezoneOffset" value="' + timezoneOffset + '" />');
    //-->
    </script>
    </div></form>
    </body>
    ....
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  11. #11
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah! That makes sense. Thanks!
    John


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •