Category: Code Snippets

How to vertically center text in CSS Grid and Flexbox

Take this simple HTML: And, some basic styling: It looks like this: Now, we want to center this text vertically. The first modern approach you can take is with flexbox. Just add these two lines to .parent: And, you get this: Simple. It’s very similar with CSS Grid: And, you can add as much text as you want and it’ll get vertically-centered properly. Like this: So, there you go. That said, if you want to go on learning even more HTML and CSS, check out my Website Template course on Skillshare. You’ll learn CSS Grid, CSS transitions, asynchronous requests in

Read More »

How to Style HTML Input Placeholders In CSS

Let’s start with the HTML: So, now we want to style the placeholders themselves. If you want to keep it simple, most modern browsers support this: You can use the ::placeholder pseudo-class like any other. For older browser support it looks like this: So, let’s look at a real-world example: A simple form. Nothing fancy, but notice the placeholder text for the required email field is a different color. This is one simple example of how you might use something like this. Here’s the full HTML: And, here’s the full CSS: So, there you go. Now, speaking of real-world examples,

Read More »

2 ways to horizontally center a DIV in CSS

Let’s take this simple HTML structure: The first method involves some straight-forward CSS: Doesn’t get much simpler than that: And, if it this is all you need to do, you can probably stop here. But, sometimes what you’re actually trying to do is a little more complicated and you need some more flexibility. Enter CSS Grid: Here, we’re creating a grid with 1 column that fills the entire space of the parent DIV. Then, a child DIV with a 60% width. And, using justify-items to center the child DIV. This might seem like overkill for something this simple, but it

Read More »

Ajax requests with jQuery.post()

This is one of the reasons I still like jQuery. I’m sure all the new frameworks do similar stuff, but things like this are so simple with jQuery. Anyway, jQuery.post() is a shorthand Ajax function. It’s the equivalent of doing this: $.ajax({ type: “POST”, url: url, data: data, success: success, dataType: dataType }); So, with it, you can send an Ajax request this easily: var posting = $.post(url, data); Then, handle the response like this: posting.done(function(data) { // Handle data here }); Or, all together like this: $.post( “process.php”, function( data ) { $( “.result” ).html( data ); }); It

Read More »

The One-Line Difference Between Ugly and Amazing Websites

Forget personal taste. I’m telling you as sure as I’m sitting here… whatever your personal thoughts about the differences between this before and after… one will get you, “Um well… something just feels off.” And, the other: “Wow! Clean, fresh. Amazing!” It’s just how people are. Anyway, here’s the before: And, here’s the after: This is a template I’m working on for a new course I’m doing. And, the difference is a single CSS declaration, but it’s light-years in terms of how it looks and how clients will react to it. The code? body, p, h1, h2, h3, h4, h5,

Read More »

Send PHP Email Using PHPMailer

PHPMailer is a popular open source, email-sending library used by applications like WordPress, Drupal, SugarCRM, Joomla and others. This tutorial will show you how to install PHPMailer, set up SMTP and send email in PHP using PHPMailer.

Read More »

PHP Add To and Remove Elements From an Array

In this tutorial, I’ll show you eight different ways to add to, remove from and insert elements to/from an array in PHP. PHP array functions used include: array(), array_push(), array_unshift(), array_pop(), array_shift(), array_slice(), count(), array_diff() and array_splice().

Read More »

What object-oriented programming is NOT

Object-oriented programming is MORE (much more) than knowing how to write a class. It’s about how you build your applications. I call it an “approach”… but it’s an approach *to application-building*.

Read More »

Output the last row inserted in MySQLi

MySQLi makes grabbing the last inserted row easy. After running your insert query, you can do this: $id = $mysqli->insert_id; Then, you can query for that row like this: $result = $mysqli->query(“SELECT * FROM table_name WHERE ID = {$id}”); And, fetch the result as an object: $user = $result->fetch_object(); From there it’s a simple echo to output it to your page: echo $user->user_name; (Although, you’ll want to use htmlspecialchars or htmlentities to escape the output and prevent XSS attacks. But, we’ll save that for another day.) Simple. Anyway, in the newly released Module 3 of PHP 101, I show you

Read More »

Connecting to MySQL with PDO

With MySQLi, it looks like this: $mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name); But, PDO is a bit different because it can interact 12 different types of databases: Oracle, PostgreSQL, SQLite, MySQL, Cubrid and several others. So, when using it you have to specify which driver you want to use. Like this: $conn = new PDO(“mysql:host={$db_host};dbname={$db_name}”, $db_user, $db_pass); Notice the “mysql:host=” bit. For different drivers, you just change out the “mysql” part. So, PostgreSQL would be: $conn = new PDO(“pgsql:host={$db_host};dbname={$db_name}”, $db_user, $db_pass); For Oracle, it’d be: $conn = new PDO(“oci:host={$db_host};dbname={$db_name}”, $db_user, $db_pass); And, so on. Nice thing is… The code

Read More »

Simple input filtering in PHP

Here’s one I don’t see talked about much: $name = filter_input(INPUT_POST, ‘name’, FILTER_SANITIZE_STRING); What this does is grab the “name” element from your POST array and run it through the the filter: FILTER_SANITIZE_STRING… which removes all HTML tags from the string (since we know a person’s name doesn’t have HTML in it). It’s a really simple way of quickly filtering your data. Here’s another: if ( ! $email = filter_input( INPUT_POST, ’email’, FILTER_VALIDATE_EMAIL ) ) { die(‘Invalid email’); } This one validates the submitted email address. And, returns false if it’s invalid. Again, a very simple way of quickly validating

Read More »

Prevent SQL injection attacks with prepared statements

Bit of a hot topic lately in my inbox. Here’s what an old, vulnerable query might look like: $expected_data = 1; $query = “SELECT * FROM users where id=$expected_data”; $result = $mysqli->query($query); The problem here is we’re injecting user-submitted data directly into our SQL statement without any sort of escaping or validation. So, a hacker could enter something like this in our form: 1; DROP TABLE users; Changing our full query to: SELECT * FROM users where id=1; DROP TABLE users; Which, as you can probably see, will execute the SELECT statement but then drop our users table. No bueno.

Read More »

How to Create Tweets Using the Twitter API

I’m going to quickly show you the exact code to send a tweet using the Twitter API. Then, how to learn to build a full-fledged Twitter app. Let’s dig in. Before you start, you need three things: A developer account on Twitter. (dev.twitter.com) A Twitter app for the API keys and access tokens (apps.twitter.com) The Twitter OAuth PHP class (twitteroauth.com) Download the class to your working folder. Then, create a file called index.php (or whatever you want) and include the Twitter OAuth class like so: require “twitteroauth/autoload.php”; use Abraham\TwitterOAuth\TwitterOAuth; Next, we need to connect to the API: $connection = new

Read More »

PHP cURL Tutorial and Example

A simple PHP cURL tutorial and example. Learn how to use curl_init, curl_setopt, and curl_execute. Also learn how to POST data to a remote URL using cURL. Ready to learn even more PHP? Take my free Beginner’s Guide to PHP video series at http://johnmorrisonline.com/learnphp Get this source code along with 1000s of other lines of code (including for a CMS, Social Network and more) as a supporting listener of the show at http://johnmorrisonline.com/patreon P.S. If you liked the show, give it a like and share with the communities and people you think will benefit. And, you can always find all my

Read More »

The simple secrets to high-paying freelance clients

What makes clients willing to pay $5,000, $10,000 even $20,000 and up for your services? Download and install my mobile app and I’ll show you. It’s free. Just click the button below:

Clients Like:

Inc. Magazine Logo
Lewis Howes Logo
Ray Edwards Logo

WHAT OTHERS ARE SAYING

Aaron Gott

Aaron Gott

John has a particular knack for the development and training of others.

Jim DeJonge

Jim DeJonge

John has a relaxed and engaging manner. His advice is solid and the explanations are well thought out.

Michael Skye

Michael Skye

John is a man of integrity, who gives generously of himself to projects and people he cares about.

Xan Barksdale

Xan Barksdale

Very professional worker who is extremely knowledgable in WordPress and Wishlist Member. I would definitely hire again.

Ray Edwards

I recommend John every chance I get. If every person I worked with were as committed to excellence, punctuality, value, and unquestionable integrity… the world would be a better place. Highest recommendation.

Andrew Malone

Andrew Malone

John Morris is exceptional in his ability to give focused insight into Freelancing and starting one’s business. His direct methods inspire confidence in his honesty.

Lori Grant

John did an outstanding job on my project. I highly recommend him and look forward to working with him on future projects.

Bradley Smith

John and I have worked together on numerous projects. John is very quick and efficient and was a pleasure to work with.

Chris Aitken

He significantly improved my site through his expert knowledge of PHP, CSS and Javascript. Would definitely recommend John to others.

Thabo Motsoahae

John is one of the best instructors I have come across, I learned a lot from his online tutorials.

Steve Dimmick

Steve Dimmick

John has provided expert knowledge and advice on multiple occasions that have helped me better serve my clients. John is a Rockstar!

Oliver Wainwright

Oliver Wainwright

I’m a fan. I have completed several of John’s courses. I find him very knowledgeable and he has a great delivery.