0
I’m trying to receive the return via Ajax and display using Angular JS, but without success, follows below the code:
HTML:
<body ng-app="app" ng-controller="controlador">
<form ng-submit="submitForm()">
<label>X: </label><input type="number" ng-model="x"/>
<label>Y: </label><input type="number" ng-model="y"/>
<span ng-show="erroCalcular">{{erroCalcular}}</span>
<span ng-show="sucesso">{{sucesso}}</span>
<input type="submit" value="Enviar"/>
</form>
</body>
Javascript:
app = angular.module("app",[]);
app.controller("controlador", ["$scope", "$http",
function($scope, $http){
$scope.submitForm = function() {
$http({
method : 'POST',
url : 'enviar.php',
data :{ valor_x: $scope.x, valor_y: $scope.y},
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data) {
if (data.errors) {
$scope.erroCalcular = data.errors.calculo;
} else {
$scope.sucesso = data.sucesso;
}
})
};
}]);
send php.
<?php
require_once "calcular.php";
//require_once "desenhar.php";
?>
calculate.php
<?php
$errors = array();
$data = array();
$dados = json_decode(file_get_contents('php://input'), true);
$x = $dados['valor_x']['x'];
$y = $dados['valor_y']['y'];
if ($y == 0)
$errors['calculo'] = 'Erro ao calcular.';
else
$errors['calculo'] = '';
if (!empty($errors)) {
$data['errors'] = $errors;
}
else {
$data['sucesso'] = 'O calculo foi realizado com sucesso.';
}
?>
And what’s happening, what message is being displayed ?
– MagicHat
No message displays, that’s the problem.
– lucasbento