user1994366
user1994366

Reputation: 43

How to post values of HTML form to new HTML page using javascript

I have a HTML form on 'page1.html' with four elements (textbox1, textbox2, listbox1, listbox2). on clicking submit, i want the values of these elements to be posted in table to new page (page2.html)

Table in page 2 is as follows:

Please help

Upvotes: 4

Views: 7757

Answers (2)

Hary
Hary

Reputation: 5818

With plain html and javascript you can do like this

page1.html

<input type="text" id="txt1" />
<input type="button" value="Submit" onclick="postData()" />

javascript

function postData(){
    var val = document.getElementById("txt1").value;

    var tbl = "<table><tr><td>"+val+"</td></tr></table>";

    var w = window.open("page2.html");

    w.document.write(tbl);
}

Upvotes: 3

Wolf
Wolf

Reputation: 2150

Here You can use a form with type = GET and action="page2.html"

Then in page2.html, in pageload use the following function to extract the URL parameters

function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}

Demo:

Page1.html

<form type="GET" action="page2.html">
<input name="text" id="txt" />
</form>

Page2.html

<script>
function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}
alert(getURLParameter("text"));
</script>

Upvotes: 2

Related Questions