Send data to a php script with AngularJS -


i'm new in angularjs , following tutorials able create simple page , simple form.

the problem every time submit form seems script sending empty data.

this html:

<div class="jumbotron text-center">         <h1>contatti</h1>          <p ng-show="message">{{ message }}</p>     </div>  <div class="row">     <div class="col-lg-8">         <form ng-submit="processform()">             <div class="form-group" ng-class="{ 'has-error' : errorname }">                 <label>nome</label>                 <input type="text" name="name" class="form-control" placeholder="chris" ng-model="formdata.name">                  <span class="help-block" ng-show="errorname">{{ errorname }}</span>              </div>             <button type="submit" class="btn btn-large">                 salva             </button>         </form>     </div> </div>  <pre>     {{ formdata }} </pre> 

this js:

//creo il modulo e includo ngroute come dipendenza var scotchapp = angular.module('scotchapp', ['ngroute']);   //configuro le routes scotchapp.config(function($routeprovider){      $routeprovider          .when('/', {             templateurl: 'pages/home.html',             controller: 'maincontroller'         })          .when('/chi-siamo', {             templateurl: 'pages/chi-siamo.html',             controller: 'aboutcontroller'         })          .when('/contatti', {             templateurl: 'pages/contatti.html',             controller: 'contactcontroller'         });  });  //creo il controller che gestisce la home scotchapp.controller('maincontroller', function($scope){     $scope.message = "sono in home"; });  //controller che gestisce la pagina chi-siamo scotchapp.controller('aboutcontroller', function($scope){     $scope.message = "sono in chi siamo"; });   //controller che gestisce la pagina contatti scotchapp.controller('contactcontroller', function($scope, $http){      $scope.formdata = {};      $scope.processform = function(){           console.log($scope.formdata);          $http.post('process.php', $scope.formdata)              .success(                 function(data){                      console.log(data);                      if( data.success )                     {                         $scope.message = data.message;                     }                     else                     {                         $scope.errorname = data.errors.name;                     }                  }              );     } }); 

and simple php script:

<?php // process.php  $errors = array();  // array hold validation errors $data = array();        // array pass data   // validate variables ======== if (trim($_post['name']) == '')   $errors['name'] = 'name required.';  // return response ==============  // response if there errors if ( count($errors) > 0 ) {    // if there items in our errors array, return errors   $data['success'] = false;   $data['errors']  = $errors; } else {    // if there no errors, return message   $data['success'] = true;   $data['message'] = 'success!'; }  // return our data ajax call echo json_encode($data); 

even if write data in input in console get:

object {success: false, errors: object} errors: object name: "name required." __proto__: object success: false __proto__: object 

am missing something?

you need data in php as,

$data = json_decode(file_get_contents("php://input"));  $data->name  // access name 

or

add jquery , change headers,

$http({     url: "process.php",     method: "post",     headers: {'content-type': 'application/x-www-form-urlencoded'},     data: $.param($scope.formdata) }).success(function(data, status, headers, config) {  }).error(function(data, status, headers, config) {  }); 

in php

$name= $_post['name']; 

if need change headers of ajax requests in config block ex,

app.config(['$httpprovider' , '$routeprovider', function ($httpprovider, $routeprovider) {     $httpprovider.defaults.headers.post['content-type'] = 'application/x-www-form-urlencoded;charset=utf-8'; }]) 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -