Get and Post

We deal with forms a lot to capture data from the user and store it in our databases, sometimes we use GET, most of the time we use POST to pass that data to our server in order to handle it.

In this post we are going to show you how to handle data sent to the server using GET and POST.

Say you have this form:

<form method="post" action="accept"> 
 Name: <input type="textbox" name="name"  /> <br/> 
 Email:<input type="textbox" name="email" /> <br/> 
 Phone:<input type="textbox" name="phone" /> <br/> 
 <input type="submit" /> 
</form> 

As soon as you click on the Submit button, the data will be sent to the server and we will be able to get it with code like this:

/* accept.jss */
var view,data={};
data.name  = request.post.name;
data.email = request.post.email;
data.phone = request.post.phone;
view = new Template().process('accept',data);
response.write(view);

As you can see, request.post is all you need to get all your data. Then whether you store it in a database or show it back to the user using templates like this one is up to you:

/* accept.html */
<body>
<div>
 Name  :$(data.name)  <br/>
 Email :$(data.email) <br/>
 Phone :$(data.phone) <br/>
</div>
</body>

It is the same when we use GET without forms, just as a query string in a url like this:

http://people.example.com/show?state=FL&city=miami

Our program show.jss can handle the variables with ease:

/* show.jss */
state = request.get.state;
city  = request.get.city;
// do stuff with state and city

Simpler impossible.

A side note, being request.get and request.post objects, you can access their properties either with dot or bracket notation like request.get.name or request.get['name']