Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm using angular to add comments to a list. This is working fine. But I'm stuck with adding a php variable to save this to my list.

HTML:

<div class="col-lg-6 col-md-12" ng-controller="customerCommentController" ng-init="userInit('<?php echo $_GET['id'] ?>')" >
    <div class="box">
        <div class="box-header">
            <h3 class="box-title"><?php echo $lang['LBL_CUSTOMER_COMMENTS']; ?></h3>
        </div><!-- /.box-header -->
        <div class="box-body">
            <div ng-include src="'php/layout_customer_comments.php'"</div>
        </div><!-- /.box-body -->
    </div><!-- /.box -->
</div><!-- /.col -->

JS

var app = angular.module('customerCommentApp', []);

app.controller('customerCommentController', function($scope, $http) {

    $scope.userInit = function(uid) {
        $scope.user = uid;
    };

    getItem(); 

    function getItem() {
        $http.post("php/getCustomerComment.php").success(function(data){
            $scope.items = data;
        });
    };

    $scope.addItem = function (item) {
        $http.post("php/addCustomerComment.php?item="+item+"&customerId="+$scope.user).success(function(data){
            getItem();
            $scope.itemInput = "";
        });
    };

});

I want the "ID" to be saved in my mysql as well. But I dont get it to work.

EDIT: I've got it to work. Code was ok, but my addCustomerComment.php was not. But another problem occurs. When opening the page the ID is not passed onto the angular. Only when I click the add button. So a new comment is added to the correct ID, but older comments are only visible after adding a new comment. How can I get the ID in angular when the page loads?

JS:

$scope.userInit = function(uid) {
    $scope.user = uid;
    //$scope.user = '99999';
    };

    // Load all available items
    getItem();  
    function getItem(){  
    $http.post("php/getCustomerComment.php?customerId="+$scope.user).success(function(data){
        $scope.items = data;
       });
    };
share|improve this question
2  
This is not the way to proceed. Data should come from the server in an API, even "currentLoggedUser". In a normal app flow, you are not allowed to view anything unless you login - and then you'd already have the loggedUser and that won't be a problem. I would suggest simulating a live app and just do a 10-liner login. – Dragos Rusu Oct 6 '15 at 12:30
    
@DragosRusu The ID is not the loggedUser, it is the selected user in a list. Is it not possible to make it work this way? (Or just not the recommended way to do this) – data2info Oct 6 '15 at 13:07
    
The selected element on page load? – Dragos Rusu Oct 6 '15 at 14:01
    
@DragosRusu the page url will be www.mydom.com/customer.php?id=10072 – data2info Oct 8 '15 at 6:47
    
So just use the $routeParams['id'] if you are using routes, or $location.search().id and make that available on the $scope for the template – Dragos Rusu Oct 8 '15 at 21:03

Try to post your data like this

 $scope.createBook = function(book_form,user){

    if(book_form.$invalid) {
        $scope.invalidSubmitAttempt = true;
        return;
    }else{

         $http.post(appurl+'user/adds/', user).success(function() {

});

             $scope.book_form.$setPristine();

         });

 }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.