0

i want to post data to my php file with $http in angularjs after posting

<?php 
$query=$_POST["mesaj"];
$flag=$query['flag'];

if($flag=="message")
{
    echo "lorem ipsum sit amet";
}
?>

has worked.

This is my app.js

var app=angular.module("app",[]);
app.controller("control",function($scope,$http){

    $scope.check=function (){
        var mesaj = {
            content: $scope.content,
            flag:"message"
        };
        $http.post('send.php', mesaj).then(function(result){
             $scope.result=result;
        }, function(){
            alert('error');
        });


    }
}); 

I want to work with angular like ajax for example

$.ajax(
{
    type: 'POST',
    url: 'send.php',
    data: {query: mesaj},
    success: function (result)
    {
        alert(result);
        setTimeout(timer,1500);
    }
});

I can't use json because my server doesn't suplly json

  • 2
    Can be duplicate of http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined – Ravi Ubana Apr 19 '17 at 06:41
  • Are you sure it's not because you don't reference to `{{ result.data }}` in the view (Or setting result to `$scope.result = result.data;`)? – Alon Eitan Apr 19 '17 at 06:43
  • 1
    Possible duplicate of [Angular HTTP post to PHP and undefined](http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined) – Ramesh Rajendran Apr 19 '17 at 06:46

1 Answers1

0

Try the below code :

var req = {
        method: 'POST',
        url: 'send.php',
        params:  { content: $scope.content, flag:"message" }
    }

    $http(req).then(function (response) {
        // Success function
        $scope.result = response.data;
    }, function (response) {
        // Failure Function
        alert("Error")
    });
Deepa
  • 184
  • 1
  • 2
  • 16
  • also pass the timeout parameter in request as you specified in above $.ajax example (timeout: 1500) – Deepa Apr 19 '17 at 09:49