How to enter variable in php of ionic 2 by means of form?

I need to send the variables lat and lng through the form and send them to php


.controller(‘HomeCtrl’, function($scope, $http){

var defaultHTTPheaders={
‘Content-Type’: ‘application/json’,
‘Accept’: ‘application/json’


$ = defaultHTTPheaders;

$scope.enviar = function () {

            method: 'post',
            url: '',
            headers:  {'Content-Type': 'application/json'},
                lat: $,
                lng: $scope.lng
        }).success(function (data) {
        }).then(function (response) {$scope.categoria =;});




Latitud Longitud
   <ion-item class="item-divider" id="inicio-list-item-divider2"> Distancia proveedor</ion-item>
  <ion-item class="range range-positive" id="inicio-range2">Distancia<input type="range" name="volume" value="50" min="0" max="100" step="2" color="secondary" ng-model="rangeValue" ng-change="drag(rangeValue)" >
  <div> {{rangeValue}}km</div>

<button id="inicio-button1" type="submit" value="Enviar" ng-click="enviar()" class="button button-assertive  button-block">Buscar</button>


<?php header("Access-Control-Allow-Origin: *"); header("Content-Type: application/json; charset=UTF-8"); $postdata = file_get_contents("php://input"); $request = json_decode($postdata); $lat=$_GET["lat"]; $lng=$_GET["lng"]; $distance =5; ?>

This code works for me:


 * Collect all Details from Angular HTTP Request.

header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');

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

echo '{"lat":"' . $lat . '"}';

Please edit your post and use the </> button above the post input field to format your code or error message or wrap it in ``` (“code fences”) manually. This will make sure your text is readable and if it recognizes the programming language it also automatically adds code syntax highlighting. Thanks.

Besides, what is your actual problem? You told us what you goal is, and the code you want to use, but not if you are getting an error, if something is not working etc.

Everything you need to know is in the error message: Your server is responding with an error 500 to the OPTIONS request. It should be a 200.

1 Like