1

I am running into an issue I cannot figure out. I create a simple button that sends data with AJAX to a php file. I have the variables defined and are appearing correct in the console when clicking the button, however my PHP file is not reading the variables I am setting. The else statement in my php file is triggering.

Does anyone see why the variables are not being set in the php file?

HTML

<button class="catalogDownload downloadButton" name="Profile Catalog" id="profileCatalogPDFButton" data-catalog-now="Profile Popular Button" data-catalog-view-name="Profile Catalog">Download Now</button>

AJAX

//Download Now AJAX
var catalog_name = '';
var button_triggered = '';
$('.downloadButton').on('click', function (event) {
    catalog_name = $(this).attr('name');
    button_triggered = $(this).data('catalog-now');
    console.log(catalog_name);
    $.ajax({
        url: 'urlhere.php',
        type: 'POST',
        data: {
            'catalog_name': catalog_name,
            'button_triggered': button_triggered
        },
        success: function (data) {
            //console.log(data);
        },
        error: function(xhr, textStatus, errorThrown) {
            alert(textStatus + "|" + errorThrown);
        },
        cache: false
    });
});

PHP File

ini_set('display_errors', 1);
error_reporting(E_ALL);

//$catalog_name = $_POST['catalog_name'];
if(isset($_POST['catalog_name'])){ 
    $catalog_name = $_POST['catalog_name'];
} else {
    echo 'Catalog Name is not reading';
}
Paul
  • 3,348
  • 5
  • 32
  • 76
  • 1
    what's in browser network console? Does the request goes from a browser to the server? – Robert Nov 06 '18 at 21:25
  • @Robert Yes, it does. It reads this `Notice: Undefined variable: catalog_name in` – Paul Nov 06 '18 at 21:26
  • looking at the code you pasted it's impossible. Are you sure this code it's executed? Maybe restart fpm or check if this is the file you execute – Robert Nov 06 '18 at 21:29
  • Can you `var_dump($_POST)` to see if there's anything at all in there? If you look at the request in your browser's network inspector, is it actually sending a POST request and is the data included in the request (as `catalog_name=XXX&button_triggered=YYY`)? – rickdenhaan Nov 06 '18 at 21:36
  • Is that your exact and entire ajax function definition? – Patrick Q Nov 06 '18 at 21:37
  • set a dataType: 'json', and try again – JMoura Nov 06 '18 at 21:37
  • @rickdenhaan `var_dump($_POST)` renders this `array(0) { }` – Paul Nov 06 '18 at 21:39
  • `'catalog_name':` try dropping the quotes – Patrick Q Nov 06 '18 at 21:40
  • @PatrickQ I tried without the quotes - no difference. Taha, I took out the space from the name - no difference. – Paul Nov 06 '18 at 21:42
  • If `$_POST` is an empty array, it looks like your browser is not submitting a POST request. Did you check the request in your browser's network inspector? – rickdenhaan Nov 06 '18 at 21:44
  • @rickdenhaan I believe the POST is working. Network - `Request URL: https://url.php Request Method: POST Status Code: 200 OK` – Paul Nov 06 '18 at 21:45
  • Is the data included in the request body? – rickdenhaan Nov 06 '18 at 21:46
  • @rickdenhaan No, I get this. `Catalog Name is not readingButton Triggered is not readingarray(0) { }
    Notice: Undefined variable: catalog_name in /home.php on line 36

    Notice: Undefined variable: button_triggered in /home/.php on line 36
    Connection failed: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'catalog_name' cannot be null`
    – Paul Nov 06 '18 at 21:48
  • That looks like the **response** body. I'm interested in the **request** body. If jQuery is working correctly, it should contain the data in either regular HTTP format (`catalog_name=XXX&button_triggered=YYY`) or JSON format (`{"catalog_name":"XXX","button_triggered":"YYY"}`). – rickdenhaan Nov 06 '18 at 21:50
  • Something appears to be setting the Content-Type on the request to `text/plain;charset=UTF-8` when it should be something like `application/x-www-form-urlencoded; charset=UTF-8` – Patrick Q Nov 06 '18 at 21:50
  • remove these unneccesary things like cache: false, contentType: false, processData: false, then instead of objects put strings as data values and tell me if it helped – Robert Nov 06 '18 at 21:53
  • @rickdenhaan I'm not sure where to obtain that from - even after searching for it. – Paul Nov 06 '18 at 21:53
  • 1
    @Paul it comes from contentType: false – Robert Nov 06 '18 at 21:55
  • @Robert Yes, you are correct. I just removed it and it works just as it should now. Thank you and thank everyone else for the extended help! – Paul Nov 06 '18 at 21:56
  • Would have been helpful to see your _full_ code when I asked "Is that your exact and entire ajax function definition?" (or, even better, to begin with) – Patrick Q Nov 06 '18 at 21:56
  • 1
    I'll post it as an answer so you can accept and question will be closed ok? – Robert Nov 06 '18 at 21:56
  • 1
    @Paul for future reference: [this question](https://stackoverflow.com/q/15603561/1941241) has answers that detail how to help debug requests in Chrome. Other browsers have similar debugging tools built-in. – rickdenhaan Nov 06 '18 at 21:57
  • 1
    By the way you could read this but with using raw reading from `echo file_get_contents('php://input')` you can try for educational purposes and this is also how you should read json :) – Robert Nov 06 '18 at 22:01

1 Answers1

2

Remove uneccesary things especially contentType: false from your code.

those should be removed

cache: false, contentType: false, processData: false

when you set contentType to false then the header Content-Type won't be set and PHP won't be able to read the variables and put it into $_POST array

From manual:

$_POST - An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data

If you want to read it without setting the Content-Type then you need to read raw request with

$postRequestContent = file_get_contents('php://input');
Robert
  • 19,800
  • 5
  • 55
  • 85