Javascript 如何将angular js动态数组转换为php数组,并将数组值提交到新的php文件中

Javascript 如何将angular js动态数组转换为php数组,并将数组值提交到新的php文件中,javascript,php,html,arrays,angularjs,Javascript,Php,Html,Arrays,Angularjs,有谁能建议我如何将angular js动态数组转换为php数组,然后在任何新文件中提交php数组的值。在我的程序中,我在arraytest.php中工作,希望以php格式发送saveform.php中的php数组。请任何人帮我做这件事。谢谢 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.1/a

有谁能建议我如何将angular js动态数组转换为php数组,然后在任何新文件中提交php数组的值。在我的程序中,我在arraytest.php中工作,希望以php格式发送saveform.php中的php数组。请任何人帮我做这件事。谢谢

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form name="myform" action="saveform.php" method="post">
<div ng-app="myApp" ng-controller="MyCtrl" data-ng-init="input.var1=input.var2=''"> 
  <div ng-repeat="input in inputs" >
    <input type="text" value="254" ng-model= "input.var1"/>
        <input type="text" ng-model="input.var2"/>
   <button type='button' ng-click='remove(input)'>Remove</button>
  </div>
  <div>
    <button type='button' ng-click='add()'>Add</button>
  </div>
     <p style="display:none">TotalDays = {{getTotal(value)}}</p><br><br>
     <div ng-repeat="dateTimeList in myarray">
            <!--  first value:  <input type ="text" value="{{ fir_value = dateTimeList.first }}">
              second value:   <input type ="text" value="{{ sec_value = dateTimeList.second }}">-->
              <p>{{ fir_value = dateTimeList.first }}</p>
              <p>{{ sec_value = dateTimeList.second }}</p>
                <?php 
                 $array1 = array("{{fir_value}}","{{sec_value}}"); 
                 print_r($array1); 
                ?>

     </div>

     <input type="submit" name="submit" />
</div>



<script>
angular.module('myApp', [])
.controller('MyCtrl', function($scope) {
  $scope.inputs = [{}]; // default 1 sets

  $scope.add = function()
   {
    $scope.inputs.push({});
   }

  $scope.getTotal = function(a)
   {


        var total = 0,first_value=0,second_value=0, total2 = 0;
        $scope.myarray = [{}];
      angular.forEach($scope.inputs, function(value) 
      { // loop over array to process all items
            first_value = value.var1;
            second_value = value.var2;
            $scope.myarray.push({first:first_value,second:second_value});
      });
return total;

  }

// coding for remove button
  $scope.remove = function(item) {
    angular.forEach($scope.inputs, function(value, key) {
      if (value == item) {
        $scope.inputs.splice(key, 1);
      }
    });
  }
});
</script>
</script>
</body>
</html>

无标题文件
去除
添加

TotalDays={{getTotal(value)}



{{fir_value=dateTimeList.first}

{{sec_value=dateTimeList.second}

angular.module('myApp',[]) .controller('MyCtrl',函数($scope){ $scope.inputs=[{}];//默认设置为1组 $scope.add=函数() { $scope.inputs.push({}); } $scope.getTotal=函数(a) { var total=0,第一个值=0,第二个值=0,total2=0; $scope.myarray=[{}]; 角度.forEach($scope.inputs,函数(值) {//在数组上循环以处理所有项 第一个_值=value.var1; 第二个_值=value.var2; $scope.myarray.push({first:first\u value,second:second\u value}); }); 返回总数; } //删除按钮的编码 $scope.remove=功能(项){ angular.forEach($scope.inputs,函数(值,键){ 如果(值==项目){ $范围输入拼接(图例1); } }); } });
下面是另一个文件saveform.php

<?php
echo "output of array is:- ";
extract($_POST);
if(isset($submit))
{
print_r($arr);
}
?>


plnkr演示:

首先:

您正在混合AngularJS和php,但不能在php中使用AngularJS表达式

您的代码:

<p>{{ fir_value = dateTimeList.first }}</p>
<p>{{ sec_value = dateTimeList.second }}</p>
<?php
$array1 = array("{{fir_value}}","{{sec_value}}");
print_r($array1);
?>

$request
现在将包含表单数据。

不要像那样使用extract来模仿PHP过去使用的旧的、不安全的工作方式。直接使用
$\u POST['xxx']
数组,加上$\u POST中的所有数据都需要清理和验证。实际问题是,我无法在php中创建数组,提取数组是第二步。
$\u POST
是一个数组<代码>打印(美元后)$array1=array(“{{fir_值}}}”,“{{sec_值}}”);这条线没有工作$数组1不包含数组值。为什么?我认为$\u POST在我的情况下不是数组。
<p>{{ fir_value = dateTimeList.first }}</p>
<p>{{ sec_value = dateTimeList.second }}</p>
array("{{fir_value}}","{{sec_value}}")
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);