Blog

Safe advice for failure

Lesson:

Every single piece of advice I give is absolutely correct because I only give advice based on my actual experiences.

Yet any piece of advice I give may also not be right for you because your situation is different.

I don’t presume to think my experience fits your situation. People only truly learn from their own experience anyway.

I just hope my advice can be a guide and some small help to choose and navigate your way safely through your experiences.

Failure is good. Safe failure is what I want for you.

Interim (the correct spelling)

“Interim” is one of those words that I keep forgetting how to spell.
(https://www.dictionary.com/browse/interim)

Here are the ways you don’t spell it (I’m including this list so I can Google search my name and incorrect spelling to find this post):

intirum
interum
intrum
intrem
intram
intarum
interum

Once against, the correct spelling is

Interim

 

A Memory Mnemonic

Here’s a way I thought of to remember how to spell it:

Think “In term” but with an “i” in term.

The Cloud (is not the rose that it seems)

The Cloud doesn’t remove the need for infrastructure and operations people. Nor does it particularly make infrastructure easier.

It simply shifts where the hardware runs and who owns it.

And for small software teams it still means the developers and project manager are “the” operations and infrastructure people.

The big difference now is The Cloud makes it much easier to accidentally spend vast amounts of money on infrastructure you had no idea you deployed (or who deployed it), where you struggle to understand what it does or why you need it, and you can never seem to figure out how it is charged on your bill.

I know this because I live it.

 

(Also published on LinkedIn)

Get started with MongoDB and PHP with XAMPP on Windows

Getting started with MongoDB on Windows and developing it with PHP turned out to be quite easy.

You can create a MongoDB database “in the cloud” for free with MongoDB’s Atlas platform – https://cloud.mongodb.com. No credit card required and you’re up and running in under 10 minutes (most of that time is waiting) after filling in a simple form fill and a few button clicks.

Next you need to setup XAMPP if haven’t already. Follow my post Setting up XAMPP for WordPress Development on Windows for details on how to do that. That gives you Apache and PHP running capability.

Finally you need to install MongoDB drivers for PHP under XAMPP. I use https://www.configserverfirewall.com/mongodb/install-mongodb-php-driver-ubuntu-windows/#install-php-mongodb-driver-on-windows to figure that out. The steps are:

  1. Download the drivers for Windows from https://pecl.php.net/package/mongodb and extract the archive (select the “DLL” link for the version you want, scroll to the bottom of the next page, then download from the “DLL List” section for your version of PHP and OS architecture.
  2. Copy the extracted php_mongodb.dll file into <drive>:\xampp\php\ext folder.
  3. Open XAMPP php.ini and add the line: extension=php_mongodb.dll.
  4. Restart the Apache server in XAMPP. You’re good to go.

 

MongoDB Atlas connection string in PHP

If you run MongoDB locally you’ll have PHP code something link this to establish a connection:

<?php
$connection = new MongoDB\Driver\Manager("mongodb://localhost:27017");
var_dump($connection);
?>

If you connect Atlas use a connection string something like:

new MongoDB\Driver\Manager("mongodb+srv://<db-username>:<db-password>@azure-westus-1-fzl9p.azure.mongodb.net/test?retryWrites=true&w=majority");

Where <db-username> and <db-password> are the username and password you created in Atlas.
“azure-westus-1-fzl9p.azure.mongodb.net” is for my the specific Mongo instance – it will be different for you.

The connection string between the double-quotes is the same as was generated when you go into your collection, select the “Connect” button and follow the prompts.

JavaScript (.js) file interpreted as MIME type text/html

I had situation with a JavaScript (.js) file seeming to load OK but in reality it was a misdirect.

Using Chrome, I inspected the Network tab in Developer Tools and couldn’t see an error with the file load. But on closer inspection, I noticed the Status was a “302” and the Type was “text/html” as follows (I was expecting a “200” Status and “script” mime Type).

It was luck that I found the answer:

I was loading the JavaScript file using the path “/inc/assets/js/vueks/vue-2.6.10.js”.

But I had a spelling mistake in the path. It should have been: “/inc/assets/js/vuejs/vue-2.6.10.js”.

I changed “vueks” to “vuejs” and the script loaded successfully.

 

The strange thing is the incorrect path didn’t give me a “404” (Not Found) response. Instead I received a “302 Found”, where clearly the file was not found.

My guess is I was loading it in a WordPress site, and while the file was not found I do have code internally to redirect to a “/not-found” page that may have returned some form of response (even though it contained no content). That or some headers were returned which confused Chrome.

(I haven’t tried to replicate this other browsers yet.)

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

Sample Vue.js app to demonstrate the simple concepts (app, conditionals, loops, binding, components, and routes)

Update: 11 November 2019

I’ll be honest – The cost is great example. I created this for some student teams at RMIT to give them something to get started. But it works and it is simple, so I hope you still get the underlying value from it.


 

I’ve created a very simple Vue.js sample app you can run immediately to see different Vue concepts in action.

Get the code at: https://github.com/jsnelders/vuejs-sample

Simply download the 2 files and run vueapp.html in a browser (it should even run from your hard drive). No compiling or tools required.

See in action at https://jsnelders.com/vuejs_sample/.

 

The app is a simple way to see the most common elements of Vue in action:

  • Setting up a simple in-page “app”.
  • Setting the “data” model.
  • Creating methods (for example for handling click events.
  • Using if statements.
  • Using for loops.
  • Creating “hash” routes which map to different “views” in the app that you can switch between (think of them as pages within a page).
  • Creating components
    • With templates defined in the JavaScript.
    • With templates in the HTML page.
    • Attaching them to routes.
    • Stand-alone components you can use in your app, and allow you to pass data in from the app, and get data back from the component via events.

These simple concepts should cover 95% of your Vue.js needs.

Git: Reset a local branch to last remote commit, remove unstages files, and fix file permissions

I use the Codeanywhere online IDE for my WordPress development. Sometimes I screw up something I’m testing (usually when I try to manipulate the filesystem with PHP) and need to reset my local branch to the last clean commit I sent to remote.

(Note that I said “remote”. I don’t trust local commits. I continually push to remote to have a safe restore point, especially if I need to do a full delete of my local branch. Yes, it has happened more than once.)

Occasionally I find folder permissions screw up as well and I need to reset via the bash command line.

The following commands are what I always come back to.

Note: The “$” in the command below represent the command-line prompt. The actual commands follow it.

 

Reset a local branch to the the last remote commit of the branch

$ git reset --hard HEAD

 

Clean up any uncommitted folders/files that the reset didn’t remove

$ git clean -f -d

 

Set full permission recursively on a folder and all files/sub-folders

Change directory (cd) to the parent folder of the folder you want to set permission on.

$ sudo chmod -R 777 vylesk-module-z1

 

Recursively delete a folder and all files/sub-folders

Change directory (cd) to the parent folder of the folder you want to delete.

$ rm -rf vylesk-module-z1

Note: The “f” means you are not prompted to delete each file/folder.

The difference between old media and social media

There are two clear differences between almost all published media that has come before and the social media we know now.

1. Available audience
2. Cost

Not cost to the consumer.

I’m about talking cost to the publisher – the person who wants to distribute their content.

In the past there has always been a greater cost to produce and distribute content, whether it in time, effort, materials or straight up money to get others to do work.
There were higher barrier to publishing and distribution: paper, printing, physical distribution of real media, a limited audience who would see the material. It was not readily scalable, nor was it cheap to scale.

That cost generally translated to a need in higher quality content, because the producer had to maximise on their return.
By that I mean: they couldn’t afford to produce shit no once would waste their time on, whether they were paying or not.
They needed maximum return on their money spent, for what was generally an expensive outlay to spread their content.

In addition, and partly due to the cost of media a type of distribution, the audience was limited.
Distribution was limited, harder, and bound my the physical medium.
Again, the producer needed to ensure maximum adoption of their content.

But with the Internet and social media that has all changed.

Social platforms allow “free” and easy distribution of content.
They also allow a “free” and easy inclusion of a massive audience.
That’s an incredibly low barrier of entry on both sides of the equations.
Combine that with increasingly easier ways to create content – written, audio and video – through readily available consume tools, and an abundance of cheap add-on services offered via outsourcing, and we have now have social platforms open to publish almost limitless amounts of content, of every quality, at almost zero cost to both the publisher and consumer.

But there is a cost.

Some may say it’s distraction. Some say privacy. And others see pathways for disinformation.
All are true to varying degrees.

What is see as the ultimate cost to the average person is:

Time and attention.

Because every piece of content generated and distributed on the social platforms is calling for our attention. And it take away our precious time, to both consider and digest, regardless of value.
People are hungry for it (for whatever their individual reason).
And worst of all, in the world of social media:

There are no editors.
No one to check for quality or facts.
No one to curate the chaff from the wheat.

Our time – our precious, limited time alive – is second-by-second being consume by other people’s shit.
Shit that is now without the barrier of economics that anyone can produce and distribute things to suck our time.
Yes, we get a momentary hit of joy or a hint of interest, but how often do we get value that actually enriches us or is usable?

I wonder: what if the social networks flipped the model and started charging for content to be published? How will the world look in 10 years time?