Hello,
I have a form, simple, with 3 fields, name, email and password in HTML and along with an AngularJS code. However, by submitting the form, I get the message that the data was sent to the bank, but they are not there at the same.
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>CRUD AngularJS</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js></script></head><bodyng-controller="MainCtrl as ctrl">
<form ng-submit="ctrl.submit()">
Nome<br>
<input type="text" ng-model="ctrl.user.nome"><br>
Email<br>
<input type="text" ng-model="ctrl.user.email"><br>
Senha<br>
<input type="password" ng-model="ctrl.user.senha"><br>
<input type="submit" id="submit" value="Submit" />
</form>
<script type="text/javascript">
angular.module('myApp', [])
.controller('MainCtrl', ['$scope', '$http', function($scope, $http){
$scope.list = [];
var self = this;
self.submit = function() {
console.log('User clicked submit with ', self.user);
$http.post('php/salvar.php', {'nome': $scope.nome, 'email': $scope.email, 'senha': $scope.senha})
.then(function(response) {
console.log(response.status);
console.log(response.data.msg);
}, function(response) {
console.log(response.status);
console.log(response.msg);
});
}
}]);
</script>
</body>
</html>
PHP:
?php
$user = 'root';
$password = 'root';
$db = 'angularDB';
$host = 'localhost';
$con = mysqli_connect("localhost", $user, $password, $db);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$nome = $_POST['nome'];
$email = $_POST['email'];
$senha = $_POST['senha'];
$ins = mysqli_query($con, "INSERT INTO users VALUES (NULL, '$nome', '$email', '$senha')");
if($ins == 1){
echo json_encode( array('status' => 200, 'msg' => 'Cadastro efetuado com sucesso!'));
}else{
echo json_encode( array('status' => 0, 'msg' => 'Erro ao cadastrar no banco.'));
}
?>