title: AngularJS pass form array to php
body: <p>I am struggling to get this form to work was wondering if anyone can give me any ideas why! So what I have is an index page with an ng-app=especial. In the DIV main_area_holder goes the ng_view. The Ng-view displays fine and form displays on index page (from localtion app/partials/cust_form.php). What I am struggling to get working is the http request to php file so I can import form data into DB. I know the php code works without ajax (straight post request). If you can help out I would be very grateful. </p>
  
  <p><strong>app.js UPDATED</strong></p>
  
  <pre><code>var especial = angular.module('especial', ['ngRoute']);
  
  especial.config(function($routeProvider) {
  $routeProvider.when('/',
      {
          controller: 'custPage',
          templateUrl: 'app/partials/cust_form.tpl.html'
      });
  });
  
  especial.config(function($httpProvider) {
  $httpProvider.defaults.transformRequest = function(request){
      if(typeof(request)!='object'){
          return request;
      }
      var str = [];
      for(var k in request){
          if(k.charAt(0)=='$'){
              delete request[k];
              continue;
          }
          var v='object'==typeof(request[k])?JSON.stringify(request[k]):request[k];
          str.push(encodeURIComponent(k) + "=" + encodeURIComponent(v));
      }
      return str.join("&amp;");
  };
  $httpProvider.defaults.timeout=10000;
  $httpProvider.defaults.headers.post = {
      'Content-Type': 'application/x-www-form-urlencoded',
      'X-Requested-With': 'XMLHttpRequest'
  };
  });
  especial.controller('custPage', function($scope, $http){
      $scope = {};
      $scope.custCreUpd = function(){
          $http({
              method: 'POST',
              url: 'app/php/cust_cre_upd.php',
              data: $scope.cust,
              headers : {'Content-Type': 'application/x-www-form-urlencoded'}
          }).success(function(data){
  
              console.log("OK", data)
  
          }).error(function(err){"ERR", console.log(err)})
      };
  });
  </code></pre>
  
  <p><strong>cust_cre_upd.php</strong></p>
  
  <pre><code>&lt;?php
  
          $post = file_get_contents("php://input");
          $values = json_decode($post, true);
          $table='customers';
          $conn = new PDO('mysql:host=localhost;dbname=displaytrends;charset=utf8', 'displaytrends', 'displaytrends');
          $conn-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
                      //Strip array to fields with values     
          $values=array_filter($values);
                      //Take array keys from array
          $field_keys=array_keys($values);
                      //Implode for insert fields
          $ins_fields=implode(",", $field_keys);
                      //Implode for insert value fields (values will binded later)
          $value_fields=":" . implode(", :", $field_keys);
                      //Create update fields for each array create value = 'value = :value'.
          $update_fields=array_keys($values);
          foreach($update_fields as &amp;$val){
              $val=$val." = :".$val;
          }
          $update_fields=implode(", ", $update_fields);
                      //SQL Query
          $insert = "INSERT INTO $table ($ins_fields) VALUES ($value_fields) ON DUPLICATE KEY UPDATE $update_fields";
          $query = $conn-&gt;prepare($insert);
                      //Bind each value based on value coming in.
          foreach ($values as $key =&gt; &amp;$value) {
              switch(gettype($value)) {
                  case 'integer':
                  case 'double':
                  $query-&gt;bindParam(':' . $key, $value, PDO::PARAM_INT);
                  break;
                  default:
                  $query-&gt;bindParam(':' . $key, $value, PDO::PARAM_STR);
              }
          }
          $query-&gt;execute();
  
      ?&gt;
  </code></pre>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html ng-app="especial"&gt;
  &lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Especial - Database&lt;/title&gt;
  &lt;link rel="stylesheet" href="css/index.css"&gt;
  &lt;script src="scripts/angular.js"&gt;&lt;/script&gt;
  &lt;script src="scripts/angular-route.js"&gt;&lt;/script&gt;
  &lt;script src="scripts/angular-animate.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;div class="header"&gt;
  &lt;img id="logo" src="images/especial-logo.jpg"&gt;
  &lt;a id="logout" href="logout.php"&gt;Logout&lt;/a&gt;
  &lt;div class="menu"&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="sub_menu"&gt;&lt;/div&gt;
  
  &lt;div class="main_area"&gt;
      &lt;div id="main_area_holder" ng-view&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="footer"&gt;&lt;/div&gt;
  &lt;script src="app/app.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>cust_form.php</strong></p>
  
  <pre><code>&lt;div ng-controller="custPage"&gt;
  &lt;div id="form"&gt;
          &lt;form name="cust_form"&gt;   
             &lt;label&gt;Account No:&lt;/label&gt;
             &lt;input type="text" ng-model="cust.int_custID" name="cust[int_custID]" id="int_custID"/&gt;
             &lt;label&gt;Company:&lt;/label&gt;
             &lt;input type="text" ng-model="cust.cust_company" name="cust[cust_company]" id="cust_company"/&gt;
              &lt;label&gt;Address:&lt;/label&gt;
              &lt;textarea type="text" rows=5 ng-model="cust.cust_address" name="cust[cust_address]" id="cust_address"&gt;&lt;/textarea&gt;
              &lt;label&gt;Postcode:&lt;/label&gt;
              &lt;input type="text" ng-model="cust.cust_postcode" name="cust[cust_postcode]" id="cust_postcode"/&gt;
              &lt;label&gt;Contact 1:&lt;/label&gt;
              &lt;input type="text" ng-model="cust.cust_contact_1" name="cust[cust_contact_1]" id="cust_contact_1"/&gt;
              &lt;label&gt;Contact 2:&lt;/label&gt;
              &lt;input type="text" ng-model="cust.cust_contact_2"  name="cust[cust_contact_2]"  id="cust_contact_2"/&gt;
              &lt;label&gt;Telephone:&lt;/label&gt;
              &lt;input type="text" ng-model="cust.cust_tel" name="cust[cust_tel]" id="cust_tel"/&gt;
              &lt;label&gt;Mobile:&lt;/label&gt;
              &lt;input type="text" ng-model="cust.cust_mob" name="cust[cust_mob]" id="cust_mob"/&gt;
              &lt;label&gt;DDI:&lt;/label&gt;
              &lt;input type="text" ng-model="cust.cust_DDI" name="cust[cust_DDI]" id="cust_DDI"/&gt;
              &lt;label&gt;Email:&lt;/label&gt;
              &lt;input type="email" ng-model="cust.cust_email" name="cust[cust_email]" id="cust_email"/&gt;
              &lt;label&gt;Notes:&lt;/label&gt;
              &lt;textarea type="text" rows=5 colums=1 ng-model="cust.cust_notes"  name="cust[cust_notes]" id="cust_notes"&gt;&lt;/textarea&gt;
  
             &lt;button type="submit" ng-click="custCreUpd()"&gt; Submit &lt;/button&gt;
         &lt;/form&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>