如何使用javascript读取表单数据?

时间:2021-10-11 13:55:45

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?

当我提交数据时,内容被发布到。html。如何付费使用javascript读取这些数据。html页面吗?

4 个解决方案

#1


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.

实际上,您正在通过POST发送表单的数据。如果您正在通过POST方法发送数据,那么您需要一个“服务器”来处理从HTML页面接收到的数据,然后将相同的数据转发给所请求的HTML页面。

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!

如果更改要获取的表单数据,则可以使用JavaScript读取数据。如果您还想使用POST,可以使用cookie在另一个页面上存储和检索数据。下面是步骤!

Hope this helps.

希望这个有帮助。

#2


1  

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

action属性指定表单提交时发送表单数据的位置,通常是服务器端方法。

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.

如果不使用服务器端编程(如PHP或c#或Java),可以使用查询字符串来获取搜索参数。

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

为此,必须使用windows .location。搜索属性,以获取搜索参数。

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.

get是不安全的,对吧?我不能使用方法get。唯一允许的方法是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.

POST数据是服务器端处理的数据。Javascript是客户端技术。其思想是,您不可能使用JavaScript读取post数据。

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

您可以使用localStorage从任何地方访问数据。

#3


0  

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

您可以在URL中发送表单数据,只需将表单提交的方法设置为“GET”。

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

然后url将是“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

现在支付。html,您可以从javascript方法中读取此URL并解析此URL以获取所有参数。使用window.location.search

#4


0  

You can use location.search

您可以使用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);

#1


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.

实际上,您正在通过POST发送表单的数据。如果您正在通过POST方法发送数据,那么您需要一个“服务器”来处理从HTML页面接收到的数据,然后将相同的数据转发给所请求的HTML页面。

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!

如果更改要获取的表单数据,则可以使用JavaScript读取数据。如果您还想使用POST,可以使用cookie在另一个页面上存储和检索数据。下面是步骤!

Hope this helps.

希望这个有帮助。

#2


1  

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

action属性指定表单提交时发送表单数据的位置,通常是服务器端方法。

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.

如果不使用服务器端编程(如PHP或c#或Java),可以使用查询字符串来获取搜索参数。

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

为此,必须使用windows .location。搜索属性,以获取搜索参数。

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.

get是不安全的,对吧?我不能使用方法get。唯一允许的方法是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.

POST数据是服务器端处理的数据。Javascript是客户端技术。其思想是,您不可能使用JavaScript读取post数据。

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

您可以使用localStorage从任何地方访问数据。

#3


0  

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

您可以在URL中发送表单数据,只需将表单提交的方法设置为“GET”。

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

然后url将是“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

现在支付。html,您可以从javascript方法中读取此URL并解析此URL以获取所有参数。使用window.location.search

#4


0  

You can use location.search

您可以使用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);