Solved

Can I send data using Javascript HTTP POST method?

Posted on 2007-12-05
11
24,597 Views
Last Modified: 2008-03-01
I have the following Javascript form.

I want to send data / display using javascript throught HTTP POST method? Can I do that?

How can I do that? Once the user clicks on SUBMIT button the data should be displayed on the second page?

Here is my form:-
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
<!-- Start of FORM -->
<form method="POST" action="http://www.SFO.com">
Account Name: <input type="text" name="AC_Name" value="">
<br>
<br>
Account ID:   <input type="text" name="AC_Name" value="">
<br>
<br>
Opportunity ID: <input type="text" name="AC_Name" value="">
<br>
<br>

<input type="submit">


</form>
<!-- End of FORM -->

////////////////////////////////////////////////////////////////////////
0
Comment
Question by:sumprit
  • 6
  • 3
  • 2
11 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20414283
sumprit,

Do you mean you want Javascript to get and process the form?  It can't.  You should use server script to get the form and process it.

Let me know if I misunderstood the question.  If you have a question about server languages/scripts then let me know.  Let me know if you have any questions or need more information.

b0lsc0tt
0
 

Author Comment

by:sumprit
ID: 20414963
Basically I need to understand how to send data using the HTTP POST method.
This is for my personal understanding. So I thought of this form designed in javascript and then I can use HTTP POST method, to send data across the server.

Since, I am doing this on my local machine, XP Professional installed, I would like the data to be displayed on the web page that I designed hosted on IIS - local host.

Can you help me understand how HTTP POST works using Javascript?

Is there a better option? I am comfortable with Javascript and ASP.Net?
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20415310
Hi sumprit:

You can submit a form (POST) using javascript, but to get the POST or GET values you will need a serverside language such as ASP or PHP, etc...

Hope this helps :)
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20415320
Actually my above is not entirely true, you can (to a certain extent) get the GET values using javascript, but it is not very elegant, and i wouldn't recommend it..But don't think it is possible for POST values.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20415412
The way you are doing it, with a form, is the most common way it is done and a good thing to learn.  Depending on your browser there are addons/extensions that will show you the headers.  That is what you would need to look at to see the "next part" of an HTTP POST.  This is the step where the headers, post, etc are sent to the server.

The best way to view this would be with server script.  Javascript isn't really useful in this case, the page is going away from the browser.  You don't have to use .NET but with IIS then ASP will be useful.  Use that for the server code.  You can look at the Request object or the Request.Form collection.

This is the way POST is used and handled.  If you have a more specific question about part of it then please elaborate.  I will be happy to provide more details or help as I can.  However there is a lot to POST so you won't learn it all here. :)

bol
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:sumprit
ID: 20415733
what about using Querystrings in javascript?

Can I not use Querystrings to send data from one page to another?

How will that look like in the code pasted above?
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20415884
Querystrings would not use the POST method.  You would just need to change POST to GET though to have the querystring used.

There are some disadvantages and limits to using the query string though.  You could ask another question for details but 2 big ones are having the info in the URL and the limited length.

bol
0
 

Author Comment

by:sumprit
ID: 20416305
Right.!

Here is First page
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
  <title></title>
</head>

<head>

</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<h1>Sending Data across the pages </h1>

<!-- Start of FORM -->
<form action="End.html" method="GET">
Account Name: <input type="text" name="first">
<br>
<br>
Account ID: <input type="text" name="second">
<br>
<br>
<input type="submit">
</form>
<!-- End of FORM -->


</body>
</html>

////////////////////////////////////////////////////////////////////////////////////////////////////////

Here is the second page which is retrieving the query strings:-

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<head>
  <title></title>
 
<script LANGUAGE = "Javascript" type"test/javascript">
var qsParm = new Array();
function qs() {
var query = window.location.search.substring(1);
var parms = query.split('&');
for (var i=0; i<parms.length; i++) {
var pos = parms[i].indexOf('=');
if (pos > 0) {
var key = parms[i].substring(0,pos);
var val = parms[i].substring(pos+1);
qsParm[key] = val;
}
}
}
qsParm['first'] = null;
qsParm['second'] = null;
qs();

if (qsParm['first'] &&  qsParm['second'])
document.write('You entered' + qsParm['first']
continued from previous line + ' : ' + qsParm['second']);
</script>
 
</head>


<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">



<h1> This is the data </h1>
</body>
</html>

///////////////////////////////////////////////////////////////////////////////////


Notice how the query strings are used to display data from one page to another
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20416438
Right that is about all Javascript can do as far as "form processing."

Let me know what other questions you have on the original issue.  Being able to do that in Javascript when GET is used does not mean the same can be done with POST.  If that was the main question then, as I mentioned before, Javascript won't work.  The way POST forms are processed is with server script.  Let me know if you have a question about part of this still or need something clarified.

bol
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 500 total points
ID: 20416468
You don't have to answer but why would you ever want Javascript to process the form?  The point of using forms is for the user to give you info.  If Javascript is used you don't really have the info, everything is still done by the browser on the clientside.  There are rare case where this may be OK but this question should help you understand why server scripts are an important part of this process.  That is how you get the info, either in your database, in an email, or by some other means but the server script does it while processing the form.

The question above and this comment is just rhetorical.  You mentioned you wanted to learn about this and this point is an important one that seems to be unclear.  There is no way to learn about or use POST if you leave the server out.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20416824
I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to count occurrences of each item in an array.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now