user3510576
user3510576

Reputation: 15

Error sending data from Angularjs to php

Im creating and mobile app with angularjs and php of recipes. Im trying to send the id from the recipe from the url using the $stateParams of angular to the server with http POST method. The Id is send it or received wrong because when I check the data in my localhost shows me an empty object. Anyone knows what error Im committing?

Angularjs:

-Controller for the view:

.controller('RecipeDetailCtrl', function($scope, $stateParams, Recipes) {

  Recipes.getRecipe($stateParams.recipeId).success(function(data){

        $scope.recipe = data;
        console.log($scope.recipe);
    })                                                
})

-Function in services.js:

   getRecipe : function(Id) {
        return $http({
            url: 'http://localhost/degusto/detail.php',
            method: 'POST',
            data: {'Id': Id},
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
     }) 

  }

PHP:

   require_once("connect.php");

   $user_id = $_POST['Id'];

   echo "user_id:"+$user_id;

   $result = $conn->query("SELECT * FROM Recipes WHERE Id ='".$var."'");

   $outp = "[";
    while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
       if ($outp != "[") {$outp .= ",";}
       $outp .= '{"Id":"'  . $rs["Id"] . '",';
       $outp .= '"Name":"'   . $rs["Name"]        . '",';
       $outp .= '"Date":"'   . $rs["Date"]        . '",';
       $outp .= '"Descrip":"'   . $rs["Descrip"]        . '",';
       $outp .= '"Image":"'. $rs["Image"]     . '"}';

      }
  $outp .="]";

  $conn->close();

  echo($outp);

  ?>

Upvotes: 0

Views: 59

Answers (2)

Dominic Scanlan
Dominic Scanlan

Reputation: 1009

Is Id making across to PHP? I've experienced in the past that the object you want to send must be in a container object.

you could try data: $.param({'data' : {'Id': Id}}),

and then in php $data = $_POST['data'];

Upvotes: 0

Martin
Martin

Reputation: 2427

You set $user_id to the id but you use $var in your select statement

$result = $conn->query("SELECT * FROM Recipes WHERE Id ='".$var."'");

should be

$result = $conn->query("SELECT * FROM Recipes WHERE Id ='".$user_id."'");

Also be aware of SQL Injection see here

Upvotes: 1

Related Questions