In every book I have on HTML, it shows a database or other file associated with a form. I want to use form elements with JavaScript to control the web pages themselves, without the use of an external file. Can this be done?

Recommended Answers

All 6 Replies

Sure, but JavaScript is so limited in its capabilities why would you want to? There's a reason why forms are always handled server-side. You can't send email with JavaScript. And if someone has JavaScript turned off there goes your whole form.

Big reason: I would have to pay more a month for server-side privileges.

The pages I want to implement are:

- A sport-playoff fairness-index calculator.
- An election fairness-index calculator.
- A demonstration of how multiple governments interact to cut each other's revenues.

You can certainly use form controls as javascript controls; there's no point implementing drop-down lists in Javascript manually for example.

Things to consider are just that; you have to put form controls in forms to validate pages correctly; and if you put a submit button on a form, it'll try to send it by whatever 'method' attribute you provide, i think you have to provide one of GET or POST as the method in order to stay compliant, but i think 'action' would be optional because a page can be its own form action. Use <button> instead of <input> for a button... Erm, catch the onClick event on the button using Javascript, and use it for whatever means neccessary.

You can access form field's data using [element].value, and there's a document.forms collection for addressing elements within forms.

So, it's certainly possible, and lots of sites do it for one-page interactivity.

It seems to work OK.

I even got it to validate!

Just put action="none" inside the form tag and it works and validates.

If you fail to put the action attribute in, it does something weird in Firefox. If you leave the mouse cursor over a button on the taskbar, it raises that task on top of the Firefox window when the script ends.

Sure, but JavaScript is so limited in its capabilities why would you want to? There's a reason why forms are always handled server-side. You can't send email with JavaScript. And if someone has JavaScript turned off there goes your whole form.

There is another bigger reason I don't want it server-side. I get only so much download bandwidth an hour. Someone using the calculator form with server-side controls would quickly use up my bandwidth, hiding my pages from everyone for an hour. With JavaScript, the form doesn't reload every time the script runs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.