• Checking the contents of a Form using Javascript
    23 replies, posted
Hi there :) I am trying to learn Javascript by using it. What I want to do is check the contents of a form and, if everything is in order, show the Submit button. I have the current code, in it I am trying to print the value the user entered: [code] <script type="text/javascript"> <!-- function toggle_visibility(id) { var e = document.getElementById(id); if(e.style.display == 'block') e.style.display = 'none'; else e.style.display = 'block'; } //--> </script> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="text" onClick="toggle_visibility('amount_stock');" name="stock_trade_amount" maxlength="60" value='0'> <script type="text/javascript"> //submit_trade_amount.style.display == 'block' var kVar = getElementsByName(stock_trade_amount) document.write(kVar); </script> <input id='amount_stock' type="submit" name="submit_trade_amount" value=" NextX "> </form> [/code] But, apparerently it can't seem to get the value :S Can someone please explain what's wrong?
document.formname.controlname.value
[QUOTE=Sirkorv;21233185]document.formname.controlname.value[/QUOTE] How do I use this? What is the controlname?
var Hello = nameofyourform.username.value; alert(Hello); Controlname is what you named your field, in this case username.
Okay, thanks :) But what about value then?
[QUOTE=Eax;21234254]Okay, thanks :) But what about value then?[/QUOTE] Make sure it's not null. :downs:
[QUOTE=Sirkorv;21233185]document.formname.controlname.value[/QUOTE] IIRC , that's deprecated. [code]document.getElementById('controlId').value[/code]
[QUOTE=turb_;21235079]IIRC , that's deprecated. [code]document.getElementById('controlId').value[/code][/QUOTE] Okay, the id is "id" set in the forms name? What should VALUE be?
No, you set the id of the input to a certain name, then you fetch the Element with JavaScript, and get the value property which contains what's in the input box. [code] <form> <input id="hello" /> </form> <a href="javascript:alert(document.getElementById('hello').value);"> What's in the (input) box? </a> [/code]
[QUOTE=Eax;21246034]Okay, the id is "id" set in the forms name? What should VALUE be?[/QUOTE] the id is the id of your element, defined in the HTML (<input id="username" value="please enter your username here" type="text"/>). The value is the object's value. [code] if (document.getElementById("username").value != "") { [I]code goes here[/I] }[/code]
Okay thanks :) How do I switch visibility on/off on a submit button then? If the form is filled out it is to show the submit button but I cannot make it work :(
[code] // make visible element.style.visibility = "visible"; // make invisible element.style.visibility = "hidden"; [/code]
[QUOTE=turb_;21246250][code] // make visible element.style.visibility = "visible"; // make invisible element.style.visibility = "hidden"; [/code][/QUOTE] Thanks :) Uhm.. Stupid question, but where do I, in the element.style.visibility, write the name/id of the element? [editline]01:52PM[/editline] For some reason, this isn't working :( Can someone see what's wrong? [code] <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="butts"> <input id='some_field' type="text" name="stock_trade_amount" maxlength="60"> <script type="text/javascript"> if (document.getElementById("some_field").value != "") { submit_trade_amount.style.visibility = "visible"; } </script> <input id='amount_stock' style="visibility: hidden;" type="submit" name="submit_trade_amount" value=" NextX "> </form> [/code]
No, by element, I meant that style is a property of the element object. You'll need to use document.getElementById() to get an element object from an ID. To avoid calling it twice, you can store the element object in a variable like so: [code] var el = document.getElementById('yourid'); el.do-stuff(); el.style.visibility = 'visible'; [/code]
Okay, this is what I have now: [code] <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="butts"> <input id='some_field' type="text" name="stock_trade_amount" maxlength="60"> <script type="text/javascript"> var fCheck = document.getElementById("some_field").value; if ( fCheck != "") { var el = document.getElementById('amount_stock'); el.style.visibility = 'visible'; } </script> <input id='amount_stock' style="visibility: hidden;" type="submit" name="submit_trade_amount" value=" NextX "> </form> [/code] I enter the string "123" in "some_field" and it still doesn't show the button :S No error in the debugger either :(
Someone? :S
like this? [url]http://strange-works.org/index.php[/url] [php] <html> <head> <script type="text/javascript"> function check(){ var fCheck = document.getElementById("some_field"); if ( fCheck.value.length != 0) { var el = document.getElementById('amount_stock'); el.style.visibility = 'visible'; } else{ var el = document.getElementById('amount_stock'); el.style.visibility = 'hidden'; } } </script> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="butts"> <input id='some_field' type="text" name="stock_trade_amount" maxlength="60" onkeyup="check()"> <input id='amount_stock' style="visibility: hidden;" type="submit" name="submit_trade_amount" value=" NextX "> </form> </body> [/php] Edit:If its in the body. It only does it once.
Yeah! Exactly like that! Thanks a lot :) What do you mean by "it only does it once"?
It does the scripts from top to bottom and stops. But in the head you can make a function for when the text changes.
Okay, thanks a lot! :)
[QUOTE=Eax;21256720]Okay, thanks a lot! :)[/QUOTE] Your 42x game is broken. It says I cant register because its in valid. But it is. Will reply in morning.
Hmm weird. I didn't have problems registering another account :S What part does it say is invalid?
[email]123-45@hotmail.co.uk[/email] doesnt work. Its the -
Ahh okay, that's weird. Gotta fix that! Thanks! :)
Sorry, you need to Log In to post a reply to this thread.