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

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

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

};

$http.defaults.headers.post = defaultHTTPheaders;

$scope.enviar = function () {

     $http({
            method: 'post',
            url: 'http://www.efruver.com/ubicarproveedores.php',
            headers:  {'Content-Type': 'application/json'},
            data:JSON.stringify({
                lat: $scope.lat,
                lng: $scope.lng
            })
            
        }).success(function (data) {
            console.log(data);
             
        }).then(function (response) {$scope.categoria = response.data.records;});

}

});

//home.html}

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>
  </ion-item>     


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

//ubicar.php

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

<?php

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