gpt4 book ai didi

javascript - 如何在 Controller 中动态创建http post语句

转载 作者:行者123 更新时间:2023-11-30 20:02:58 24 4
gpt4 key购买 nike

我在使用 http 发送 POST 时遇到问题,因为我在数据属性中的值来自一个数组,该数组每次可能具有不同的值。数组项从表单中的字段中获取其值。它会因每种形式而变化。

这是我的 Controller 代码,

var app = angular.module("myApp", []);

app.controller('myCtrl',function ($scope, $http , $sce, $rootScope,$parse)
{

var items=<?php echo json_encode($columnsForArray) ?>;

//This is what items looks like

console.log(items);/*=> ["ID", "stock", "locstock", "location",
"con_date", "status", "showafterreturn", "vin", "auto_type", "import",
"race","street_rod", "special_interest", "other", "car_year", "make",
"model", "submodel", "special_edition", "body", "doors", "pc_date",
"return_date"]*/

$scope.runPHP = function () {

$http({
method: 'POST',
url: 'Connect-carinfo.php',
data: {
source: 'from_inside',
angular.forEach(items, function (value, key) {
--------
|
| });/* Here i need this as a result of the above foreach to
| be like this-
-------> selected: $scope.selected,
ID: $scope.ID,
stock: $scope.stock,
locstock: $scope.locstock,
location: $scope.location,
con_date: $scope.con_date,
status: $scope.status,
showafterreturn: $scope.showafterreturn,
vin: $scope.vin,*/
}

}).then(function (response) {
$scope.templateURL= $sce.trustAsHtml(response.data);
})
};
});

这是我的 View ,其中包含附加到范围的输入字段。我将我所有的表单字段都放在一个名为 testArr 的数组中,下面的代码可以使用它

    public function getDisp($i, $j, $testArr)
{
for ($k = 0; $k < sizeof($testArr); $k++)
{
if ($testArr[$k][1] == $i && $testArr[$k][2] == $j)
{
$this->element = $testArr[$k][0];
$type = $testArr[$k][4];
$first = $testArr[0][0];

echo <<<HTML

<div>
<label for="$this->element" class="control-label">
$this->element:
</label><br>
HTML;


if ($testArr[$k][3] == 'yes')
{

if (substr($testArr[$k][4], 0, 4) == 'enum')
{
$this->createDrop($testArr[$k][4], $testArr[$k][3]);

$this->validation();

}

else
{
echo <<<HTML

<input type="$type" style="box-sizing: border-box;" class="form-control" name="$this->element" id="$this->element" data-ng-model="$this->element" required/>

HTML;

$this->validation();
}
}

else
{
if (substr($testArr[$k][4], 0, 4) == 'enum')
{
$this->createDrop($testArr[$k][4], $testArr[$k][3]);
}

else
echo <<<HTML

<input type="$type" style="box-sizing: border-box;" class="form-
control" name="$this->element" id="$this->element" data-ng-model="$this-
>element" />
HTML;
}

echo <<<HTML
</div>
<br>
HTML;

}

else
continue;
}
}

public function createDrop($item, $validation)
{
$edit = substr($item, 6, -2);
$results = explode("','", $edit);
if($validation === 'yes')
$drop = "<select style=\"box-sizing: border-box;\" class=\"form-
control\" name=\"$this->element\" id=\"$this->element\" data-ng-
model=\"$this->element\" required/>";
else
$drop = "<select style=\"box-sizing: border-box;\" class=\"form-
control\" name=\"$this->element\" id=\"$this->element\" data-ng-
model=\"$this->element\"/>";
$drop .= "<option value=\"\" disabled selected value>Select an
option</option>";
foreach ($results as $result) {

$drop .= "<option>$result</option>";
}
$drop .= "</select>";
$drop = stripslashes($drop);
echo <<<HTML
$drop
HTML;

}

最佳答案

but I still dint find the answer about how to pass all the form data.

AngularJS 的规则是在您的 ng-models 中始终有一个点。 1

<input ng-model="items.ID" />
<input ng-model="items.stock" />
<input ng-model="items.vin" />

然后在 Controller 中,简单地 POST items 对象:

$scope.items = {};

$scope.onSubmit = function(items) {
$http.post(url, items)
}

无论表单中有多少不同的项目,POST 的代码都保持不变。

关于javascript - 如何在 Controller 中动态创建http post语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53179173/

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