4

I'm fairly new to AngularJS, and I'm trying to perform an http service to send data to a php file for input into MySQL. I have been able to get this to work using jQuery and the $.ajax function so I am sure my PHP is alright.

My ultimate question: Is the way that I am implementing this AngularJS service correct? I have binded an ng-click to a function on an a element called routeReloader(data). When I console.log the parameters within the routeReloader function, I get the correct parameter. I am also getting an alert box of "success." However, it isn't updating MySQL.

userApp.controller('photoController',  ['$scope','$location','$log','$route','$http',    function($scope,$location,$log,$route,$http){

    $scope.routeReloader = function(parameters){
        $http.post('centralcommand.php', {id: parameters}).success(function(data,status,headers,config){
            alert('success'); 
        }).error(function(data,status,headers,config){
            alert('failure');
        });
    };

}]);

I get the data in the centralcommand.php file using $_POST['id'].

JQuery Working Equivalent

$('#link-element').click(function(){

        var REL = $(this).attr("rel");
        var URL = 'centralcommand.php';
        var dataString = 'id=' + REL;

        $.ajax({
          type: "POST",
          url: URL,
          data: dataString,
          cache: false,
          success : function() {
            alert('success')
          }
        });
});
9
  • Maybe problem in url? Did you tried full url with http prefix? Commented Aug 3, 2015 at 4:04
  • I have used the entire localhost URL and it works. If I didn't have the correct URL, the alert would state 'failure'. Commented Aug 3, 2015 at 4:10
  • No, when problem in url, alert won't execute, there will be failure inside http service, not in request itself Commented Aug 3, 2015 at 4:22
  • The console doesn't output any errors. When you use a wrong URL, it gives a 404 error. I'm not getting that error. I tested with a bad URL to be sure. Commented Aug 3, 2015 at 4:39
  • But when url is not correct, it will thrown protocol error, for example when you accidentally use double // slash Commented Aug 3, 2015 at 4:41

1 Answer 1

1

I was able to find a solution based on the comments with the fact that the data getting passed is in JSON format. I had to decode the JSON format into something that PHP could use:

PHP CODE

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$photoID = $request->id;

Convert JSON format with this PHP code, and you can use it within your SQL queries. Note that $request->id uses the same id that gets passed in the AngularJS $http post request.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.