0

im trying to make a basic http post request that INSERT data to my DataBase. i have the pages:

  • register.php that have the register form.

  • maincons.js that have the controllers of the appliction.

  • sqlregister.php that have the INSERT query.

im using MeekroDB (libary of sql functions).

the appliction insert the data to the database, but the success function dosn't work.

register.php:

<!doctype html>
<html lang="he" dir="rtl">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="../css/style.css">
    <script src="../js/angular.min.js"></script>

    <title>register</title>
  </head>
  <body>
  <div ng-app="appRegister" ng-controller="conRegister" class="grid">
  <form name="register" method="post" class="col-4 regform" ng-submit="valfunc()">
  <input  type="text" name="fname" ng-model="fname"  required>
  <input  type="email" name="email" ng-model="email"  required>
  <input type="submit" value="register">
  </form>  

</div>
<script src="../js/maincons.js"></script>
  </body>
</html>

maincons.js:

var app = angular.module('appRegister', []);
app.controller('conRegister', function($scope,$http) {
    $scope.valfunc = function () {
            $http.post(
                "sqlregister.php", {
                    'fname': $scope.fname,
                    'email': $scope.email
                }
            ).success(function(data){
                alert(data);
              })
              .error(function(data){
                alert(data);
              })

    }
});

sqlregister.php:

<?php
require_once '../system/sqlfunc.php';
$info = json_decode(file_get_contents("php://input"));
if(count($info) > 0) {
    $fname = $info->fname;
    $email = $info->email;
    $query=DB::insert('tbcustomers', [
        'Custname' => $fname,
        'email' => $email
      ]);
      if($query==1)
      echo "true";
      else
      echo "false";
}
?>

the problem is that the success function dont alert the data.

thanks for the helpers.

  • use .then instead of success ...see more here https://stackoverflow.com/questions/16385278/angular-httppromise-difference-between-success-error-methods-and-thens-a – M A Salman Apr 10 '20 at 13:56

2 Answers2

0
$http.post(
            "sqlregister.php", {
                'fname': $scope.fname,
                'email': $scope.email
            }
        )

This code doesn't send JSON as you expect in PHP. It sends the data in www-form-urlencoded format.

You can access this data in PHP directly from $_POST superglobal array.

astax
  • 1,769
  • 1
  • 14
  • 23
0

According to doc this should be the syntax

var req = {
method: 'POST',
 url: 'http://example.com',
headers: {
  'Content-Type': undefined
 },
data: { test: 'test' }
}

$http(req)
.then(function(res){ 
    console.log(res);
 }, function(err){
     console.log(err);
 });
Palak Jadav
  • 1,202
  • 1
  • 11
  • 23