Pankaj
Pankaj

Reputation: 10105

Read Form Posted Values in another form in Javascript or JQuery

I have following Page with Form Data

<form id="myform" method="post" action="page2.html">
    <input type="hidden" value="1" name="A"
    <input type="submit" value="Submit" />
</form>

When I submit the form, I try to read the form values in Page2.html...I have below code in Java Script.

In the below code, I am trying to read the Posted Value but due to some reasons it always gives null value.

<script>
function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    console.log(url);
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var A = getParameterByName('A');
alert(A);
</script>

Please suggest what is wrong in above code

Upvotes: 0

Views: 48

Answers (2)

Aung Myo Linn
Aung Myo Linn

Reputation: 2890

You cannot read post variables using JavaScript, they are handled at server side.

You have to change your form submit method to "get"

<form id="myform" method="get" action="page2.html">
    <input type="hidden" value="1" name="A" />
    <input type="submit" value="Submit" />
</form>

Upvotes: 1

Shibu Thomas
Shibu Thomas

Reputation: 3196

You forgot to add the Html input closing tag.Please correct It.

<input type="hidden" value="1" name="A"/>

Upvotes: 0

Related Questions