0

I've looked through several question/answer combinations relating to my issue, but the suggestions I've tried haven't worked.

This is my my AJAX request:

$('#submitRequest').on('click', function() {
    // package everything to get it ready for transfer
    var textSamples = JSON.stringify(packTextStyles());
    var swatches = JSON.stringify(packSwatches());
    var clientInfo = JSON.stringify(packClientInfo());

    $.ajax ({
        type: 'post',
        url: 'classes/requestHandler.php',
        //headers: {'Content-Type': 'application/json'},
        data: {
            typeSamples: textSamples,
            swatchSamples: swatches,
            userInfo: clientInfo
        },
        success: function(data) {
            if (data === 'added') {
                alert('ok');
            }
            else {
                alert(data);
                return false;
            }
        }
    });
    return false;
});

The three vars textSamples, swatches, and clientInfo are all arrays. The textSamples array contains eight objects with each having five properties, so it's a good chunk of data being sent over. My problem is that var_dump($_POST) returns an empty array and any attempt at trying json_decode($_POST['typeSamples']) gives me an error of undefined index on PHP side.

Here is the screenshot of the headers on the network tab in Chrome dev tools: Headers from POST

So $_POST is getting something, but I have no idea where it's going. Some questions:

  1. Do I need to put headers: {'Content-Type': 'application/x-www-form-urlencoded'} or headers: {'Content-Type': 'application/json'} in the AJAX, since I used JSON.stringify()?

  2. Because I used AJAX I didn't put method="post" action="something.php" in the form tag, and the input tags don't have the name attributes. Submit button doesn't have type="submit". Does that matter?

EDIT: If it matters, I'm using jQuery 1.11.1 and PHP 5.4.12

EDIT2: half-finished PHP:

<?php


$reqGetter = new RequestReceiver;
$reqGetter->checkInput();


class RequestReceiver{
    // public function __construct() {
    //  $textSamples = json_decode($_POST['typeSamples']);
    // }
    public function checkInput() {

        if (!empty($_POST)) {
            echo('added');
            $typeSamples = json_decode($_POST['typeSamples'], true);
            print_r($typeSamples);
        } else {
            echo('failed');
        }
    }
}
Monica
  • 1,585
  • 3
  • 23
  • 34
  • Are you using a PHP framework, or just your own PHP files? – Mike Trpcic Aug 15 '14 at 00:17
  • My own. Should I not be? Or is it a matter of verifying what PHP is receiving in $_POST? – Monica Aug 15 '14 at 00:19
  • 1
    I only asked because some frameworks might clear out $_POST in favor of accessing that data using their own methods/classes. – Mike Trpcic Aug 15 '14 at 00:20
  • If you want to use `$_POST` in your PHP, stick with the jQuery default content-type (application/x-www-form-urlencoded). Your code looks fine to me – Phil Aug 15 '14 at 00:21
  • 1
    Is jQuery's type parameter case sensitive? e.g. `POST` vs `post`? – scrowler Aug 15 '14 at 00:22
  • @scrowler you wouldn't think so but it's worth a shot – Phil Aug 15 '14 at 00:22
  • @scrowler I tried capping it last night, but I'll try again to make sure – Monica Aug 15 '14 at 00:23
  • 1
    No difference between `type: 'POST'` and `type: 'post'` – Monica Aug 15 '14 at 00:25
  • 2
    Try doing a var_dump on $_GET, $_POST and $_REQUEST. Then you will know exactly everything you are receiving. – Daniel Williams Aug 15 '14 at 00:28
  • I think we're going to need to see the PHP file accepting the POST request as that's obviously where the problem is. Any chance you could add the full set of response headers too? – Phil Aug 15 '14 at 00:53
  • Wait, why is there an `Origin` header present? That should only show up for a CORS request – Phil Aug 15 '14 at 00:58
  • 1
    I just looked at the "response" tab in Chrome > Dev Tools > Network and it now works. I was just going to the page myself, so of course the $_POST array would be empty. I need to sleep. – Monica Aug 15 '14 at 00:58
  • @NojoRu but why does it say `Content-Length: 5` in the response (in your screenshot)? There's obviously way more than 5 bytes of POST data – Phil Aug 15 '14 at 00:59
  • I have no idea, @Phil, but now it says 1841 – Monica Aug 15 '14 at 01:01
  • 1
    Voting to close as **a problem that can no longer be reproduced**. – Phil Aug 15 '14 at 01:05
  • @Phil, seconded. Thank you, everyone, for your help. The response tab definitely helped, so whomever suggested that (I can't find it now), thank you! – Monica Aug 15 '14 at 01:14

0 Answers0