[英]How to access data submitted by a form in Angularjs?
Here is an example from Ng-Book - the Complete Book on Angularjs Book by Ari Lerner
http://jsbin.com/voxirajoru/edit?html,output where ng-form
has been used to create nested form elements and perform validation. 这是
Ng-Book - the Complete Book on Angularjs Book by Ari Lerner
一个示例Ng-Book - the Complete Book on Angularjs Book by Ari Lerner
http://jsbin.com/voxirajoru/edit?html ,其中ng-form
已用于创建嵌套的表单元素并执行验证。 As the each input field has same value for name
attribute so how can on the server side I can access value of all three variables by using some server side language? 由于每个输入字段的
name
属性值都相同,因此如何在服务器端使用某种服务器端语言访问所有三个变量的值? Let's say it's PHP
and method
is post
. 假设它是
PHP
, method
是post
。 Usually in PHP
I would do this: 通常在
PHP
我会这样做:
`$_POST['dynamic_input']`
but how is it possible to access three field values coming from input field using $_POST
array? 但是如何使用
$_POST
数组访问来自输入字段的三个字段值?
Using the example from the link you provided, you can access form data by updating the code there with the following. 使用您提供的链接中的示例,可以通过使用以下内容更新代码来访问表单数据。
// Add the fields object as a parameter to submitForm() //将fields对象作为参数添加到submitForm()
<form name="signup_form" ng-controller="FormController" ng-submit="submitForm(fields)" novalidate>
// In the $scope.submitForm() function... //在$ scope.submitForm()函数中...
$scope.submitForm = function(data) {
alert(data[0].name); // Alerts name
alert(data[1].name); // Alerts password
alert(data[2].name); // Alerts email
};
If you log out the data received by submitForm(), you get the following: 如果注销由SubmitForm()接收的数据,则会得到以下信息:
[{"placeholder":"Username","isRequired":true,"$$hashKey":"004","name":"random name"},{"placeholder":"Password","isRequired":true,"$$hashKey":"005","name":"password"},{"placeholder":"Email (optional)","isRequired":false,"$$hashKey":"006","name":"email@host.com"}]
For passing to your server, package all this up as is or format it to your preference and send it to your server via the built in $http.post()
or $resource()
inside of the $scope.submitForm function. 要传递到服务器,请按原样打包所有这些文件,或将其格式化为您的首选项,然后通过$ scope.submitForm函数内部的内置
$http.post()
或$resource()
将其发送到服务器。
An example of the formatted data could be: 格式化数据的示例可以是:
$scope.submitForm = function(data) {
var postData = {};
postData.name = data[0].name;
postData.password = data[1].name;
postData.email = data[2].name;
... send postData to server via AJAX ...
// Creates the object: {"name":"random name","password":"password","email":"email@host.com"}
//alert(JSON.stringify(postData));
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.