2016-02-05 43 views
1

我有一個多個字段在數組合成像如何在angualr js中提交同名的表單值?

<input type='text' name="frm[]">相同的名稱。我想在AngularJS中提交這個,並在PHP中獲得價值。

我的腳本:

<input type="text" name="textval" id="textval1" ng-model="data.textvalt[1]" > 
<input type="text" name="textval" id="textval2"ng-model="data.textvalt[2]"> 
<input type="text" name="textval" id="textval3" ng-model="data.textvalt[3]"> 
+0

您應阿賈克斯JSON傳遞數據,所以名字沒有在這種情況下 – karaxuna

+0

在正常情況下,無論(我的意思是角時,項目未使用)如果我們有相同名稱的輸入,我們不能提交表單(一直得到最後輸入的名稱),ng模型工作作爲我們項目中的名稱..我認爲我們不能這樣做 – Maher

回答

1

使用角的$ http服務的數據發佈到你的PHP文件。

檢查下面的代碼

<html ng-app="submitExample"> 
 
<script type="text/javascript" src="lib\angular.js"></script> 
 
<script type="text/javascript" src="index.js"></script> 
 
<form ng-submit="submit()" ng-controller="FormController"> 
 
    Enter text and hit enter: 
 
<input type="text" name="textval" id="textval1" ng-model="data.textvalt[1]" > 
 
<input type="text" name="textval" id="textval2"ng-model="data.textvalt[2]"> 
 
<input type="text" name="textval" id="textval3" ng-model="data.textvalt[3]"> 
 
<input type="submit" id="submit" value="Submit" /> 
 
</form> 
 
<html> 
 
    
 

 
    var app = angular.module('submitExample', []) 
 
    app.controller('FormController', ['$scope','$http', function($scope,$http) { 
 
    
 
     $scope.data = {}; 
 
     $scope.data.textvalt = new Array(); 
 
     $scope.data.textvalt[1] = ''; 
 
     $scope.data.textvalt[2] = ''; 
 
     $scope.data.textvalt[3] = ''; 
 

 
     $scope.submit = function() {  
 
       
 

 
       var configObject = { 
 
       method: 'POST', 
 
       url: 'index.php', 
 
       data: { textval1: $scope.data.textvalt[1] , textval2 : $scope.data.textvalt[2], textval3 : $scope.data.textvalt[3] } 
 
       } 
 

 
$http(configObject).then 
 
    (function successCallback(response) { 
 
console.log("posted sucuessfully"); 
 
    }, 
 
    function errorCallback(response) { 
 
    console.log("Failed"); 
 
    }); 
 
     
 
    }; 
 
    }]); 
 

+1

您也可以使用$ resource service $ http。參考文檔 - https://docs.angularjs.org/api/ngResource/service/$resource –

+0

它確定,但在我的情況下,文本框是動態創建的循環值,這意味着一般來說,我們創建一個文本框與循環在提交這個文本框之後,我們在arrray中獲得了文本框的值。這是怎麼回事?我們使用angularjs來管理 –

+0

@ManojShevate謝謝:) –