Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use HTML forms without a server

I am not a web-developer and want to understand the better way to pass variables. In the past I have used various ways to pass things to java script functions. I have never used forms as I always associated them with server and databases. I have a website in which user selections change the contents of the website.

I am wondering if you can use forms without any server just as a way to pass a few things to a javascript function where they are used to change the page content. For basic example, if someone selects male the page background becomes blue, if they choose female the background becomes pink. Would forms be the way to go and just onsubmit call a javascript function? How would I actually pass the form contents to the javascript function?

like image 390
slimbo Avatar asked May 18 '11 21:05

slimbo


People also ask

Are HTML forms still used?

HTML forms were invented and mostly standardized before the advent of asynchronous Javascript and complex web applications. Today, we use form inputs, buttons, and other interaction mechanisms in lots of different ways; but underlying that is a system based on the HTTP request-response paradigm.

How can we store form data in HTML?

HTML web storage provides two objects for storing data on the client: window.localStorage - stores data with no expiration date. window.sessionStorage - stores data for one session (data is lost when the browser tab is closed)

Can you submit a form without PHP?

you don't have to use php for the form submission. You could even use JS for submitting a form. And the mailto haven't anything to do with a form submission. It would be better if you explain what you want to accomplish by doing this.


3 Answers

Yes you absolutely can use forms/inputs/any kind of html element and never talk to a server, just don't expect to store that data! You're right about using events (like the onsubmit one you mentioned) to trigger Javascript functions.

Here is a quick and dirty example (heavy on the dirty) that does sorta kinda what you'd like. Note that instead of waiting for the form to be submitted before the color change, I go ahead and do it immediately after they choose a gender from the dropdown.

http://jsfiddle.net/wG8K4/1/

like image 115
James Avatar answered Sep 29 '22 08:09

James


You wouldn't pass the parameters. You could have "onsubmit" call a javascript function, and then within the function use javascript to access the actual controls that the user has selected. You could use the GetElementById function to retrieve a certain element, and then determine the value of that element.

If all you wanted to do was change the background color, you could use javascript to change the backgroundColor property of the body tag or any tag on the page.

You'd have to remember to return false from your function, though -- otherwise, the form would be submitted.

like image 37
Cynthia Avatar answered Sep 29 '22 09:09

Cynthia


You don't need servers / databases to use forms. Forms are simply a method from passing variables from one file to another, regardless if that is an html file or some php script or what have you. If you stick to using GET forms, your form will naturally pack its data into the URL of your page at which time you can access them. For instance (borrowed from http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/):

<script language="javascript">
function $_GET(q,s) {
    s = s ? s : window.location.search;
    var re = new RegExp('&'+q+'(?:=([^&]*))?(?=&|$)','i');
    return (s=s.replace(/^?/,'&').match(re)) ? (typeof s[1] == 'undefined' ? '' : decodeURIComponent(s[1])) : undefined;
} 

var usersName = $_GET('username');
if(typeof(usersName)!='undefined'){
    document.write('<h1>Hi there, '+usersName+'</h1>');
}

</script>
<form>
     <input type="text" name="username" />
     <input type="submit" value="Say my name" />
</form>
like image 35
Jage Avatar answered Sep 29 '22 09:09

Jage