ScottyBoy Posted April 9, 2010 Share Posted April 9, 2010 How can I get the value of a text field from a form on my site? I'm trying to save that value in a JS variable i.e, $text = "textFromTheField";. Thanks, guys. Link to comment Share on other sites More sharing options...
Synook Posted April 9, 2010 Share Posted April 9, 2010 document.getElementById("id_of_field").value document.getElementById("id_of_form").name_of_field.value //older way, if you don't want to ID every field Link to comment Share on other sites More sharing options...
ScottyBoy Posted April 9, 2010 Author Share Posted April 9, 2010 How do I assign the form value to a variable called $txt? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 9, 2010 Share Posted April 9, 2010 You access the value like this:document.getElementById("id_of_field").valueYou can put that on either the right or left side of an equal sign, you can either read that or write to it. Link to comment Share on other sites More sharing options...
ShadowMage Posted April 9, 2010 Share Posted April 9, 2010 How do I assign the form value to a variable called $txt?Can JS variables start with '$'? Or are you trying to get this in PHP? Link to comment Share on other sites More sharing options...
thescientist Posted April 9, 2010 Share Posted April 9, 2010 I think so.$ Link to comment Share on other sites More sharing options...
ShadowMage Posted April 9, 2010 Share Posted April 9, 2010 Interesting... Link to comment Share on other sites More sharing options...
jeffman Posted April 9, 2010 Share Posted April 9, 2010 If they couldn't, jQuery would look a lot different.OTOH, if you mix this stuff with PHP, things could easily get confusing. I personally reserve the $ for private namespaces (i.e., a global object the rounds up a whole bunch of values instead of having a whole lot of independent global variables). This is a common practice. Link to comment Share on other sites More sharing options...
ScottyBoy Posted April 9, 2010 Author Share Posted April 9, 2010 If they couldn't, jQuery would look a lot different.OTOH, if you mix this stuff with PHP, things could easily get confusing. I personally reserve the $ for private namespaces (i.e., a global object the rounds up a whole bunch of values instead of having a whole lot of independent global variables). This is a common practice.I would love to do this with PHP, but unfortunately, the site I'm working on in is one of those free CMS websites that doesn't allow PHP. Link to comment Share on other sites More sharing options...
jeffman Posted April 9, 2010 Share Posted April 9, 2010 This thread wandered a lot. Did we answer the original question? Link to comment Share on other sites More sharing options...
ScottyBoy Posted April 9, 2010 Author Share Posted April 9, 2010 Kinda'.I'm posting my code here so you can help me out with it. I'm not sure what I'm doing wrong. <html><head><script type="text/javascript">var text;function post() {text == document.getElementById('textField').value;}</script></head><body> <div align="center"> <div style="width:500px;height:250px;border:2px solid #000;"> <script type="text/javascript"> document.write(text); </script> </div> <br /><br /> <form onSubmit="post()" name="messageBoard"> <input id="textField" type="text" value="Type Your Message" /> <input type="submit" value="Submit" /> </form> </div> </body> </html> Link to comment Share on other sites More sharing options...
jeffman Posted April 9, 2010 Share Posted April 9, 2010 When your page loads, the global variable text is declared but not initialized. The function post is declared but not executed. The document.write method is executed. It is passed the variable text, which at this point has no value, so either nothing is written, or perhaps the word "undefined" is written.When the form is submitted, the function post is executed. The function assigns a value to the variable text and does nothing else. Because post returns no value, the form will attempt to be submitted. Since there is no action attribute in the form tag, I'm not sure what happens after that. You probably can't tell the difference anyway, unless something flickers.What would you like to happen? Link to comment Share on other sites More sharing options...
ScottyBoy Posted April 9, 2010 Author Share Posted April 9, 2010 All I'm trying to do is make so that when I type something into the text field and hit submit, the text I entered is saved as the value of the variable "text" (this is why I didn't add a preset value to it). And I want that variable to keep it's value, even if the page is reloaded. Until someone else comes along and changes it again. Does that make sense? Link to comment Share on other sites More sharing options...
Synook Posted April 10, 2010 Share Posted April 10, 2010 JavaScript variables are stateless and contained within the script - they won't "survive" the reloading. You'll need to use a server-side storage method, or cookies, to do what you want. Link to comment Share on other sites More sharing options...
ScottyBoy Posted April 10, 2010 Author Share Posted April 10, 2010 Could I use XML for that? Link to comment Share on other sites More sharing options...
Synook Posted April 10, 2010 Share Posted April 10, 2010 Yes, but you'd need to write to the XML file using a server-side language. JS can't do it. Link to comment Share on other sites More sharing options...
jeffman Posted April 10, 2010 Share Posted April 10, 2010 Have you investigated cookies? Link to comment Share on other sites More sharing options...
ScottyBoy Posted April 11, 2010 Author Share Posted April 11, 2010 Have you investigated cookies?A little. It seems complicated, and I'm not sure how to use them correctly though. I already looked at the W3Schools tutorial for using cookies with JavaScript, but I need something a little more explanatory. Link to comment Share on other sites More sharing options...
Synook Posted April 11, 2010 Share Posted April 11, 2010 http://www.quirksmode.org/js/cookies.html <- very good article. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.