Handling One Radio Button With JavaScript

Today, I discovered an issue that I have probably encountered in the past; but it’s been so long I forgot about it. I had some JavaScript set up to select a radio button based on text input, and then update the text based on the selected radio button (it’s a TinyMCE plugin that inserts a shortcode into the WordPress visual editor). Throughout all of the testing I performed, everything worked just fine. Today, though, someone else was testing the interface and could not get the text or the radio button to update properly.

After a bit of testing and messing around with the interface, I finally discovered that my JavaScript wasn’t recognizing the existence of the radio button on the form. After a bit more testing, I realized that’s because only one radio button existed; and JavaScript doesn’t treat radio buttons as arrays when there’s only one instance. When I had been testing before, I had multiple radio buttons, so everything worked; but now there was only one button, so it fell over.

PHP: Stepping Through Arrays

I’ve posted a few articles about working with arrays in PHP over the past few years. I have posted information about adding and replacing elements in arrays; searching for items in arrays; and even a general post about handy array-related functions in PHP. I’m back again with a few more handy functions.

In PHP, you can obviously loop through arrays pretty easily by using a foreach() loop, but did you know you can actually step through arrays manually? PHP offers a handful of functions to do just that.┬áLet’s take a look at those functions, and how you might be able to use them.

Find an Item in an Array (PHP)

On occasion, I’m sure you’ve found yourself in a position where you need to find an item inside of an array. PHP offers a very simple method to do just that. The array_search function lets you search for the item and then tells you what the index for that item is. This function works with numerically indexed arrays and with associative arrays.

Adding and Removing Items in PHP Arrays

When working with arrays, you may often find yourself in a position where you need to add a few extra items to the array or remove some of the items you already added. PHP has a few functions that are designed to help you with this process, allowing you to add new elements to the beginning or end of the array or to remove elements from the beginning or end of the array.

Some Handy Array Functions in PHP

This post will go over the basics of some very nice array-related functions that are built in to PHP. If you use arrays fairly often in your PHP code, you will no doubt find a use for each of these handy functions over time.