Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-10-2013, 05:17 AM   PM User | #1
kaanta
New Coder

 
Join Date: Dec 2010
Posts: 26
Thanks: 1
Thanked 0 Times in 0 Posts
kaanta is an unknown quantity at this point
pass data using post between html pages

Hi,
I want to pass values between 2 html pages using POST method. I have lot of data to be passed to 2nd page and cannot use GET. My issue is I dont know how to pass values to second html page and how do i display username on page 2.
pls help.

page1.html
[CODE]
<html>
<head>
<title>Form</title>
</head>
<body>
<h4>Enter your name</h4>
<form name="form1" method="post" action="page2.html">
<input type="text" name="username">
<input type="submit" onclick="submitForm()">
</form>
<script type="text/javascript">
function submitForm()
{
form1.submit();
}
</script>
</body>
</html>

page2.html

<html>
<head>
</head>
<body>
....... dont know how to display username entered on page1.html here...
</body>
</html>

[CODE]
kaanta is offline   Reply With Quote
Old 01-10-2013, 07:09 AM   PM User | #2
sbhmf
New Coder

 
Join Date: Jan 2013
Location: Sunnyvale, CA
Posts: 39
Thanks: 3
Thanked 1 Time in 1 Post
sbhmf is an unknown quantity at this point
Use Request.Form("controlName")...

based on your first page, you may imbed the submitted data into the second page's controls following the syntax above:

<html>
<head><title>page 2</title>
<script type="text/javascript">
function fnLoad(){
/*This will append the submitted name to the content of this page:*/
document.getElementById('me').innerHTML += Request.Form('username') + '.';
}
</script>
</head>
<body onload="fnLoad()">
<p id="me">Yo, I'm </p>
</body>
</html>
sbhmf is offline   Reply With Quote
Old 01-10-2013, 07:32 AM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
How you get the posted values to display in the second page depends on which server side language you are using to construct the page. JavaScript can write POST data but cannot read it.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-10-2013, 01:39 PM   PM User | #4
kaanta
New Coder

 
Join Date: Dec 2010
Posts: 26
Thanks: 1
Thanked 0 Times in 0 Posts
kaanta is an unknown quantity at this point
I don't have any server side script. I don't know if Request object is available in JavaScript. Will it mean that I cannot post data between htmls and js only?
kaanta is offline   Reply With Quote
Old 01-10-2013, 10:16 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Request.form is the ASP way of reading the posted data (one of the many server side languages that can process data that has been POSTed).

JavaScript can only read data from a form on the prior page when the form used GET to pass the information in the querystring on the end of the address.

The alternative would be to have the JavaScript that is called when the form is submitted save the information in a cookie and then call the following page without actually submitting the form. The following page could then read the information from the cookie using JavaScript.

Without a server side language you would still have no way to pass the information from one page to the other for anyone who doesn't have JavaScript enabled in their browser.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-11-2013, 04:51 PM   PM User | #6
kaanta
New Coder

 
Join Date: Dec 2010
Posts: 26
Thanks: 1
Thanked 0 Times in 0 Posts
kaanta is an unknown quantity at this point
Thank u felgall for helping.
One more quesrion..If I introduce aspx page like default.aspx, can u suggest how do I load page2.html after reading post values in aspx code and making these values available on page 2
kaanta is offline   Reply With Quote
Old 01-12-2013, 10:46 AM   PM User | #7
sbhmf
New Coder

 
Join Date: Jan 2013
Location: Sunnyvale, CA
Posts: 39
Thanks: 3
Thanked 1 Time in 1 Post
sbhmf is an unknown quantity at this point
I don't think it's a good idea to start learning server-side coding prior to mastering a good working knowledge of client-side coding. You don't NEED a server to accomplish your objective.

Another way to pass information form one page to another is by appending it to the querystring.

The recipient page simply needs to parse the querystring to get the values. Something like...

var qs = location.href.split('?')[1];
vat tokens = qs.split('&');

now each token in the tokens array contains a property/value pair, formatted as propertyName=Value.
sbhmf is offline   Reply With Quote
Old 01-12-2013, 01:59 PM   PM User | #8
kaanta
New Coder

 
Join Date: Dec 2010
Posts: 26
Thanks: 1
Thanked 0 Times in 0 Posts
kaanta is an unknown quantity at this point
I cannot use query string/get because there are lots of values to be sent to page 2. Even if it is within limit now, if I get to enhance app for next work then there might be more in future.
kaanta is offline   Reply With Quote
Old 01-13-2013, 12:39 AM   PM User | #9
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
POST is something that you do to a server, so it doesn't make sense that a client side page would be able to read it.

if you need more room than GET or cookies (both are about the same limit), use localStorage or window.name to persist data across pages.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:53 AM.


Advertisement
Log in to turn off these ads.