dpub33855 2015-08-18 09:23
浏览 50

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

can anybody suggest me how to convert angular js dynamic array into php array and then submit value of php array in any new file. in my program i am working in arraytest.php and want to send php array in saveform.php in php format. please anybody help me to do this. thanks

<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>

here is another file saveform.php

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

plnkr demo: http://plnkr.co/edit/20PLT52Ia86UyGClQh7h?p=preview

  • 写回答

1条回答 默认 最新

  • douying6206 2015-08-18 09:47
    关注

    First at all:

    You are mixing AngularJS and php but you cant use AngularJS expression in php.

    Your code:

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

    will be parsed into this:

    <p>{{ fir_value = dateTimeList.first }}</p>
    <p>{{ sec_value = dateTimeList.second }}</p>
    array("{{fir_value}}","{{sec_value}}")
    

    And then is AngularJS called. If that is what you want, okay.

    by the way, you are closing the bottom scripttag twice

    Submitting Forms:

    AngularJS will submit forms as json via the $http service and not as key/value pairs like normal forms. If you want to receive this data with php you have to read the input stream like this:

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

    $request will now contain the form data.

    评论

报告相同问题?

悬赏问题

  • ¥15 求解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果