1

This is the form data.

<div>
    <form name="payform" action="pay.html" method="POST">
        <div>
            <input id="customerName" name="firstname" ng-model="customerName" style="display:none"/>
            <input style="display:none" id="txnid" name="txnid" ng-model="transactionid" />
            <input type="submit" name="paybutton" id="payit" />
        </div>
    </form>
</div>

When i submit data the contents are posted to pay.html. How can I read those data using javascript on pay.html page?

Houy Narun
  • 1,557
  • 5
  • 37
  • 86
ajithes1
  • 433
  • 2
  • 11
  • 28

4 Answers4

1

The action attribute specifies where to send the form-data when a form is submitted and usually it's a server side method.

But,

If you don't use server-side programming, such as PHP or C#, or Java, you could use the query string, in order to GET search parameters.

For this, you have to use window.location.search property in order to get the search params.

var url_string = "http://www.example.com/t.html?a=1&b=3&c=4";
var url = new URL(url_string);
var c = url.search;
console.log(c);

get is not secure right? i cannot use method get. only allowed method is post.

POST data is data that is handled server side. And Javascript is client side technology. The idea is that there is no way you can read a post data using JavaScript.

You can use localStorage in order to access data from anywhere.

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
1

Actually, you are sending data of the FORM through POST. If you are sending data through POST method, then you need a 'Server' which processes the data received from the HTML page and then forward the same data as response to the requested HTML Page.

If you change the Form data to GET, then you can read the data by using JavaScript.
If you still want to use POST, you can use Cookies to store and retrieve data on another page. Here are the steps!

Hope this helps.

0

You can send form data in URL, just set the form submission's method as "GET".

Then the url will be "localhost:XXXX/pay.html?firstname='himanshu'&txnid='1'".

Now at pay.html, you can read this URL from javascript methods and parse this url to get all the parameters. Use window.location.search

Mr_Hmp
  • 2,474
  • 2
  • 35
  • 53
0

You can use location.search

var params = {};

if (location.search) {
    var temp = location.search.substring(1).split('&');
    for (var i = 0; i < temp.length; i++) {
        var dt = temp[i].split('=');
        if (!dt[0]) continue;
        params[dt[0]] = dt[1] || true;
    }
}

console.log(params.abc);
console.log(params.xyz);
mrJ0ul3
  • 126
  • 7