1

So user uses a datepicker input which the value of is what is sent via ajax to the PHP which will return an array of objects containing $anoArray below.

First Problem: ajax will only succeed using jquery; using angular $http returns "DATA WASNT AN INT" as seen below ( this is even when setting the headers as many have posted before)... ultimately I would like to use the native angular $http service instead. WHAT I HAVE BELOW DOES RETURN DATA but leaves me with the Second Problem.

Second Problem: I wish to do various things such as sort objects and set certain key:value values as variables once the data is returned but I cannot seem to get the data returned in a format capable of this. Note* I am fairly new to programming (six months self taught) and any help is appreciated.

What should be returned:

[
{"nameF":"harry","nameL":"chicken","start":"1456790450","end":"1456790490","location":"Station 1","comments":"these are comments"},
{"nameF":"johnny","nameL":"appleseed","start":"1456790450","end":"1456790490","location":"Station 1","comments":"these are comments"}
]

My angular Controller:

app.controller('scheduleController', ['$scope', '$window', function($scope, $window) {
    $scope.shifts = [];
    $scope.shiftAjax = function(){
        var x = $('#scheduleDate').val();
        $.ajax({
            method: 'post',
            url: 'getSchedule.php',
            data: {scheduleDate: x},    
        })
        .done(function(response){
$scope.shifts = response;
console.log(response);

        });
    }
}]);

The console log shows the data as I expect it but I cannot use it as a javascript object for iteration and if change the .done to: $scope.shifts = $scope.shifts = JSON.parse(response); then I just get a console of [object Object] and I still cannot iterate or get key:values out of it.

PHP post url (getSchedule.php):

<?php
  $startTime = strtotime($_POST['scheduleDate']);
  include_once('../../classes/scheduler.php');
  if(is_int($startTime)){

    $var = new scheduler; //SENT TO classes/scheduler.php
    $jsonArray = $var->getSchedule($startTime); 

  }else{
    echo "DATA WASNT AN INT";
  };?>

1 Answer 1

0

HOURS ONLINE led me to this solution ... finally:

Controller:

app.controller('scheduleController', ['$scope', '$window', '$http', function($scope, $window, $http) {
    $scope.shifts = [];
    $scope.shiftAjax = function(){
        $http({
            url: 'getSchedule.php',
            method: 'POST',
            data: $.param({'scheduleDate': $scope.input}),
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        })
        .then( function(response) {
            $scope.result = response.data;
        }, function () {
            console.log('ERROR: ANGULAR AJAX FAILED');
        });
    }
}]);

The solution on this part was to set the $.param correctly along with headers.

PHP Post url (getSchedule.php):

<?php
  $data = $_POST['scheduleDate'];
  $startTime = strtotime($data);
  include_once('scheduler.php');
  if(is_int($startTime)){
    $var = new scheduler;
    $jsonArray = $var->getSchedule($startTime);
  }else{
    echo " DATA WASNT AN INT";
  };
?>

Not sure why this is, but placing the $_POST in a separate variable before calling strtotime() on it was the kicker that solved the issue.

Now what is returned is:

[
0:{"nameF":"harry","nameL":"chicken","start":"1456790450","end":"1456790490","location":"Station 1","comments":"these are comments"},
    1:{"nameF":"johnny","nameL":"appleseed","start":"1456790450","end":"1456790490","location":"Station 1","comments":"these are comments"}
]

Now I can access the objects returned by using dot notation in the .then for example:

 $scope.result[0].start //which returns 1456790450
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.