1

I wrote an angular quiz and I'm trying to send the quiz results to the database for processing. My $http call is as follows:

  function saveQuiz() {
      quizObj.isSaving = true;
      var data = {
          id: quiz_id,
          action: 'quiz_data',
          part: 'save_quiz',
          score: quizObj.score,
          passed: quizObj.passed,
          completed: quizObj.completed,
          percentage: quizObj.perc,
          time_spent: $filter('formatTimer')(quizObj.counter),
          questions: quizObj.quiz.questions
      };
      console.log(data);
      $http({
              url: quizapp.ajax_url,
              method: "POST",
              params: data
          })
          .then(function(response) {
                  console.log(response.data);

                  quizObj.isSaving = false;

              },
              function(response) { // optional
                  // failed
                  console.log(response);
              });
  }

Notice I am passing an array of json questions as quizObj.quiz.questions. The problem on the server side is that $_POST['questions'] evaluates to the last item of the quizObj.quiz.questions json object instead of the full list.

Where have I gone wrong?

Ayman El Temsahi
  • 2,600
  • 2
  • 17
  • 27
Tommy Adeniyi
  • 325
  • 3
  • 16
  • I think it may be because you're using `params` with `method: "POST"`. try saying `data: data` instead of params on your `$http` config object and see if that works for you. – mhodges Apr 12 '17 at 20:05
  • data:data worked out perfectly. Thanks mhodges I was pulling hairs – Tommy Adeniyi Apr 12 '17 at 20:09
  • Oh my gosh, man you have no idea how many times I have done the same thing! Haha glad that worked for you! I will post an answer for future visitors – mhodges Apr 12 '17 at 20:10
  • data:data worked out perfectly in sending the questions as array. I can see it in the console logs, however I am unable to access the variables on serverside using $_POST or $_REQUEST – Tommy Adeniyi Apr 12 '17 at 21:02
  • [See this question & answers](http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined) and let me know if those work for you – mhodges Apr 12 '17 at 21:17

1 Answers1

0

When using the $http service through Angular, data goes with method: "POST" and params goes with method: "GET"

Change the properties on your config object you are passing to the $http service like so:

$http({
    url: quizapp.ajax_url,
    method: "POST",
    data: data // <-- data here, not params since using POST
}).then(function () { /*...*/ }));
mhodges
  • 10,938
  • 2
  • 28
  • 46
  • my $_POST and $_REQUEST variables on serverside are returning null – Tommy Adeniyi Apr 12 '17 at 21:22
  • @TommyAdeniyi [See this question & answers](http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined) and let me know if those work for you – mhodges Apr 12 '17 at 21:22
  • 1
    Adding this snippet to the top of my serverside file did the trick: if ($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST)) $_POST = json_decode(file_get_contents('php://input'), true); – Tommy Adeniyi Apr 12 '17 at 21:30
  • @TommyAdeniyi Sweet, glad you got it up and working! – mhodges Apr 12 '17 at 21:35