将表单数据提交到服务器端

submit form data to serverside

本文关键字:服务器端 提交 数据 表单      更新时间:2023-09-26
<form ng-submit="doRegister()">
    <div class="control-group">
        <label for="email">E-Mail Address</label>
        <input type="email" id="email" name="email" ng-model="user.email" autofocus>
    </div>
    <div class="control-group">
        <label for="password">Password</label>
        <input type="password" id="password" name="user.password" ng-model="password">
    </div>
    <div class="control-group">
        <label for="password_confirmation">Confirm Password</label>
        <input type="password" id="password_confirmation" name="password_confirmation" ng-model="user.password_confirmation">
    </div>
    <input type="submit">
</form>

如何通过POST将表格提交给register?从技术上讲,我不知道data可以放在哪里。

function registerCtrl ($scope, $http) {
    document.title = 'Register';
    $scope.doRegister = function(){
        $http.post('/register', data).
            success(function(data, status, headers, config) {
                console.log(data);
            }).
            error(function(data, status, headers, config) {
                angular.element('.errors').html(data.errors.join('<br>')).slideDown();
            });
    };
}

编辑: 进度:

function registerCtrl ($scope, $http) {
    document.title = 'Register';
    $scope.user = {};
    $scope.doRegister = function(){
        $http.post('/register', $scope.user);
    };
}

但是发送到服务器的请求在 PHP 中是一个空数组:

Array()

窗体中已绑定了属性。您可以使用它们定义数据。

例如,对于电子邮件字段:

var data = {};
data.email = $scope.email;

或者您甚至可以在控制器中定义$scope.data = {},并发布它。


编辑 9/14

这似乎是人们看到的发送$ http.post((的问题之一表单数据。 解决方案在下面的问答中给出。

AngularJS - 有什么方法可以花 http.post 美元来发送请求参数而不是 JSON?

您需要

声明变量并更改headers如下所示:

function registerCtrl ($scope, $http) {
    document.title = 'Register';
    $scope.user = {
        email: '',
        password: ''
    };
    $scope.doRegister = function(){
        $http({
            url: '/register', 
            data: $scope.user,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            transformRequest: function(obj) {
                var str = [];
                for(var p in obj)
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                return str.join("&");
            },
        });
    };
}

你需要

AngularJS的post方法将数据作为请求有效负载发送。

只需使用:

$json = json_decode(file_get_contents("php://input"));

您可以使用 php 访问值

$email = $json->email;
$password = $json->password;
$password_confirmation = $json->password_confirmation;
<form id="Register" action="/register">
 <div class="control-group">
        <label for="email">E-Mail Address</label>
        <input type="email" id="email" name="email" ng-model="email" autofocus>
    </div>
    <div class="control-group">
        <label for="password">Password</label>
        <input type="password" id="password" name="password" ng-model="password">
    </div>
    <div class="control-group">
        <label for="password_confirmation">Confirm Password</label>
        <input type="password" id="password_confirmation" name="password_confirmation" ng-model="password_confirmation">
    </div>
    <input type="submit">
</form>

调用 AJAX 函数。

$.ajax({
    url: form.prop('action'),
    type: 'POST',
    data: form.serialize(),
    dataType: 'json',
    async: false,
    success: function (result) {
    }
}); // ajax post end

var form = $("#Register");数据传递form.serialize()非常容易从服务器端传递数据。