PHP $_POST not populating from jQuery (JavaScript) $.ajax() POST

Problem

I was trying to submit data from a web page to a back-end API in PHP using jQuery’s AJAX (ie. $.ajax()).

But the $_POST variable in PHP didn’t contain the values I was posting.

 

Solution

The problem is I was encoding the POST body data the wrong way in JavaScript.

I was using JSON.stringify(data), where “data” is a JavaScript object or array.

What I needed to do what manually create a string of key1=value1&key2=value2&keyN=valueN pairs.

Example Code from my submit function:

        //sendData = JSON.stringify(data);  //<-- This doesn't work ...

        // ... but this does.
        sendData = "";
        for (var key in data) 
        {
            if (sendData != "") sendData = sendData + "&";
            sendData = sendData + key + "=" + data[key];
        }

	var ajaxRequest = {
		url: url,
		type: "POST",
		data: sendData,
 		dataType: dataType,

		success: function(data, textStatus, jqXHR)  { successCallback(data, textStatus, jqXHR ); },
		error: function(jqXHR, textStatus, errorThrown) { errorCallback(data, textStatus, errorThrown); }
	};

	$.ajax(ajaxRequest);

 

What I searched for (which didn’t help much):

Search “$_POST empty from jquery”: https://duckduckgo.com/?q=%24_POST+empty+from+jquery&amp;atb=v147-1&amp;ia=web

https://stackoverflow.com/questions/1650516/jquery-ajax-method-post-but-post-empty

https://stackoverflow.com/questions/16360008/jquery-post-post-empty

https://stackoverflow.com/questions/54039285/ajax-form-posting-to-php-script-but-post-is-empty-in-php-script

https://stackoverflow.com/questions/1650516/jquery-ajax-method-post-but-post-empty/49948834#49948834

Excel: Cells have left padding space (but it’s not an indent)

Working in Microsoft Excel (2013 and 2016) I noticed what looked like padding (space between the left side of the cell and the text) in some cells in my sheet.

(See the highlighted cell J39)

I didn’t have text indentation in the cell, and “Format Cells > Alignment > Text alignment > Indent” was set to zero.

The cause was the formatting based on the selected cell data type (in the “Number” section of the ribbon).

I had “Accounting” selected which automatically adds some padding.
Changing it to “General” returned it to normal (removed the space) for a cell with just text.

An easy mistake to make but hard to spot.

The Best Practice Fallacy

I consider “Best Practice” in software development to be a fallacy.

Why?

Yesterday’s best practice is replaced by something new today.
And today’s best practice will be replace by something else tomorrow.

I don’t have a problem with setting good guidelines and habits, but let’s not call it “best” – that implies one right way (and there are enough knuckleheads in our industry who latch onto ideas with zeal that I really don’t want to encourage further).

Instead, let’s think of it as:

A “good” approach for what we are trying to achieve today.

Any way you cut it, any practice is just someone’s opinion of how things should be done, and it’s not necessarily based on varied experience or hard lessons.

In my own business I sometimes dictate how things should be done. A decision needs to be made, a pattern set in place and direction set. But I’m flexible and often review, improve and adjust.
(I also pay the bills so in absence of a better option what I say goes.)
But in no way are the decisions I make “best practice” or based on what others consider to be best.

I regularly make decisions contrary to current belief but are still valid and appropriate for the situation. I do analysis, consider options and put a lot of thought into decisions (other time there’s not much thought but a desire to learn through experimentation).

The reality is, in software there are very few things you need to adhere to. Create code and systems others can understand and maintain. Expect change. Don’t be an asshole.

Apart from that our industry is so young, so fast moving, and has so many possibilities and facets it’s impossible to define “best”.

So let’s just drop the bullshit, call a spade a spade, and admit we’re all learning and making this up as we go.

Google GSuite – Allow email connection from Outlook

While trying to add a Googe GSuite email account in Microsoft Outlook, you may find it impossible to authenticate – Outlook will keep displaying the username/password dialogue during the account setup.

This is likely because by default Outlook is considered a “less secure” app by Google and won’t allow the connection.

Check out the “Let less secure apps use your account” page at https://support.google.com/accounts/answer/6010255?hl=en&authuser=1 for instruction on how to let a GSuite user allow Outlook to access their account.

Note: The GSuite domain owner will need to first update a setting in the domain administration to allow users to access the page that has the switch for their account.
I can’t remember the exact location of the domain admin setting, but log into your GSuite domain account and do a search for “less secure app account access” – you should easily find it then.

Microsoft Word – Float Text Left/Right

If you need to “float” text left and right on the same line in Microsoft Word, checkout these resources.

Right aligned text in Word
Right aligned text in Word

 

Resources: