gpt4 book ai didi

mysql - 如何将 ng-value 发布到 mysql 数据库

转载 作者:可可西里 更新时间:2023-11-01 07:38:55 29 4
gpt4 key购买 nike

enter image description here我正在做一个表单,其中我的表单中显示了 ng 值,我必须将该数据传递给 mysql。我使用 ng 值,因为我从我的页面 url 获取数据,我已经解码它,然后将它绑定(bind)到具有 ng 值的形式。有什么建议么。这是我的示例代码。

div class = "bodycontainer col-lg-9" ng-repeat = "x in fType">
<form name = "logForm" class = "col-lg-12" novalidate ng-submit = "stdCtrl.StudentCtrl()">

<input type = "hidden"
id = "studentid"
name = "studentid"
ng-value = "x.studentid"
ng-model = "stdCtrl.stdId"
class = "form-control" />

<div class = "form-group col-lg-6">
<label>Firstname</label>
<input type = "text"
id = "first"
name = "fname"
ng-required = "true"
ng-value = "x.firstname"
ng-model = "stdCtrl.fName"
class = "form-control"
autofocus
placeholder = "FirstName" ></input>
</div>
<div class = "form-group col-lg-6">
<label>Lastname</label>
<input type = "text"
name = "lname"
ng-required = "true"
ng-value = "x.lastname"
ng-model = "stdCtrl.lName"
class = "form-control"
placeholder = "LastName"></input>
</div>
<div class = "form-group col-lg-12">
<label>Gender:&nbsp&nbsp&nbsp</label>
<input type="radio" name="gender" ngValue = "x.gender" ng-model="stdCtrl.gender" required>Male</input>
<input type="radio" name="gender" ngValue = "x.gender" ng-model="stdCtrl.gender" required>Female</input>
</div>
<div class = "form-group col-lg-6">
<label>Email</label>
<input type = "email"
name = "email"
ng-required = "true"
ng-value = "x.email"
ng-model = "stdCtrl.Email"
class = "form-control"
placeholder = "Your Email" ></input>
</div>
<div class = "form-group col-lg-6">
<label>Fathername</label>
<input type = "text"
name = "fathername"
ng-required = "true"
ng-value = "x.fathername"
ng-model = "stdCtrl.Fname"
class = "form-control"
placeholder = "Father's Name"></input>
</div>
<div class = "form-group col-lg-6">
<label>Mothername</label>
<input type = "text"
name = "mothername"
ng-required = "true"
ng-value = "x.mothername"
ng-model = "stdCtrl.Mname"
class = "form-control"
placeholder = "Mother's Name"></input>
</div>
<div class = "form-group col-lg-6">
<label>DOB</label>
<input type = "text"
id = "birthdayPicker"
name = "birthdy"
ng-required = "true"
ng-value = "x.birthday"
ng-model = "stdCtrl.brthdy"
class = "form-control"
placeholder = "DOB-Date Of Birth"></input>
</div>
<div class = "form-group col-lg-12">
<label>Home-Address</label>
<textarea name = "address"
ng-required = "true"
ng-bind = "x.address"
ng-model = "stdCtrl.address"
class = "form-control"
placeholder = "PresentAddress"
row = "10" cols = "50" ></textarea>
</div>
<div class = "form-group col-lg-6">
<label>10<sup>th</sup>-Percentage</label>
<input type = "number"
name = "ten"
ng-required = "true"
ng-model = "stdCtrl.tenth"
ng-value = "x.tenth"
class = "form-control"
placeholder = "10th percentage"></input>
</div>
<div class = "form-group col-lg-6">
<label>12<sup>th</sup>-Percentage</label>
<input type = "number"
name = "twelve"
ng-required = "true"
ng-value = "x.twelfth"
ng-model = "stdCtrl.twelfth"
class = "form-control"
placeholder = "12th percentage"></input>
</div>

这是我的 app.js

var app = angular.module('MyForm',[])
app.controller('StudentControl',['$scope','$http',function($scope,$http){
$scope.StudentCtrl = function(){
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
function(m,key,value) {
vars[key] = value;
});
return vars;
};
var fType = JSON.parse(decodeURIComponent($scope.StudentCtrl()['data']));
console.log(fType);
$scope.fType = fType;
}]);

所以我的目标是如何将 ng 值数据发送回 mysql 数据库,以便我可以保存它并可以将其带回我的 View 表。

最佳答案

您需要将数据发布到您的后端应用程序,这个应用程序将管理 mysql 操作。

您不需要 ng-value , 你必须使用 ng-model值,所以你可以这样做:

app.controller('StudentControl', ['$scope', '$http',
function($scope, $http) {

$scope.userData = {};

$scope.submitForm = function () {
var req = {
method: 'POST',
url: 'http://example.com/your/backend/api',
data: $scope.userData
}

$http(req).then(function(resp) {
// Post OK do something
}, function(err) {
// Post KO do something
});
}
}
]);

我还可以建议更改 ng-modeluserData.<fieldname>并使用 $scope.userData

 <input type = "text"
id = "first"
name = "fname"
ng-required = "true"
ng-model = "userData.fName"
class = "form-control"
autofocus
placeholder = "FirstName" ></input>

您可能还需要创建一个外部 div 以将所有内容包含在那里的某个位置 ng-controller="StudentControl"然后更改 ng-submit也是:

<div ng-controller="StudentControl">
<div class = "bodycontainer col-lg-9" ng-repeat = "x in fType">
<form name = "logForm" class = "col-lg-12" novalidate ng-submit = "submitForm()">
[...]

关于mysql - 如何将 ng-value 发布到 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36326669/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com