# HTML DOM, Javascript Question



## ct5098 (Dec 24, 2010)

I'm creating a web-based inventory management system for a computer repair store. I want to have the user click a button and create a form underneath the button. If I create the form via Javascript with createElement, appendChild, etc. will I be able to access the ids within the newly created form and provide client-side validation with Javascript? Right now I'm using AJAX to get the form from a PHP script where I just echo the HTML code; then I just use innerHTML to insert the code onto the page. I can't use Javascript to validate the form the way I do it now.


----------



## temp02 (Dec 24, 2010)

How about setting the default size of the Form to 0 (CSS height) and when the button is pressed the form is shown (height property removed or returned to default size, you could even animate the sucker if you want)?
As for the validation it has to occur in the server when the POST of the form is submited, you could validate it with javascript but some _smartguy_ could still submit "wrong" values.

Please note that it has been a while since I've messed with any web stuff .


----------



## ct5098 (Dec 24, 2010)

I was thinking of doing something with CSS, except with the visibility property. Wil this work if I just change the visibility of the form to hidden and then visible? I was only just starting to learn AJAX and I just wanted to use it to see how it would work.


----------



## temp02 (Dec 24, 2010)

AFAIK yes you can change the css "visibility" property to "hidden" or "visible" and the form should act accordingly.
It's easier this way, since it's just one or two lines of Javascript to show and hide the form.


----------



## ct5098 (Dec 24, 2010)

Ok, thanks for your help. I always try to do things the hard way


----------



## W1zzard (Dec 24, 2010)

if the form is static you should hide it via css visibility

if you need to generate its contents on user action, use ajax to generate the html code and insert it in appropriate position, this should update the DOM tree.

dont forget about server side validation. never ever trust the client with anything, it's incredibly easy to modify/remove/generate html and js code


personally i avoid js whereever possible or at least make sure there's a decent fallback for client without js enabled that makes sure everything stays usable


----------



## ct5098 (Dec 24, 2010)

Yes, the form is static. I am going to have to validate with PHP as well trio check the MySQL database to ensure no duplicate entries, etc. Thanks for the responses.


----------

