Programming

JavaScript and PHP; Using a JavaScript Include to Run PHP

JavaScript and PHP do not play well together. JavaScript is client side, PHP server side. This makes it difficult to get things to play together. One way to get them to play together, is to have your PHP script build your JavaScript. Now, what if you need to have an external JavaScript file get some stuff from your database and run some things in PHP? Something like this widget:

This is a widget that I wrote. It pulls a list from a database and then displays it. I want people on other websites to be able to display it, so I need a way for them to include it on their site. To do that, I use the <script> tag:


<script type="text/javascript" src="http://contestfarm.com/scripts/cf/rand_contest.php?style=2&uid=1"></script>

I want you to notice a couple of things that are different about this than normal JavaScript includes. First, notice that the file extension is .php, not .js. If I called it .js, then it would not run as a PHP file, and I need it to run as PHP. Second, notice that I included two variables. This allows us to pass variables, just like you would in any PHP script.

OK. Not that we have the hosting site set to call it, how do we make it display something? Well, if I was doing it in HTML/PHP it would have code something like:


echo "<a href='" . $url . "'>" . $title . "</a>";

This is all fine and dandy, if I am calling it as a PHP script, but I'm calling it as a JavaScript. Since it is JavaScript, it does not know the HTML tags as commands, so the script fails. We need to make it JavaScript, so we have to add in a document.write. Now, this line gets longer and more confusing. We have to remember to close the HTML tag, we have to close the JavaScript command, and we have to close the PHP command. This is what we end up with:


echo "document.write(\"<a href='" . $url . "'>" . $title . "</a>)\";";

In PHP, you can stop your PHP script and display some HTML. ie:


?>
<a href="http://shabamdevelopment.com">Shabam Development Rocks
//more php here

We can do something like this, but remember, that we are doing JavaScript, so we can't bail out, use HTML and then go back. We can bail out of the PHP, but not the JavaScript. We still need the document.write. This will work:


?>
document.write('<a href="http://shabamdevelopment.com">Shabam Development Rocks');
//more php here

One last thing. We have to set the header of our file to be JavaScript. At the top of the page include a header, like this:


<? Header('Content-Type: application/x-javascript')?>

So now lets put it all together.

In the hosting page:


<script type="text/javascript" src="http://contestfarm.com/scripts/cf/rand_contest.php?style=2&uid=1"></script>

In the script:

<? Header('Content-Type: application/x-javascript')?>
document.write('<a href="http://shabamdevelopment.com">Shabam Development Rocks');
//more php here

Firefox Extensions: The Must Have's for Developers

When developing web sites, there are some tools that developers find invaluable in their development efforts. When working on web sites, I develop in Firefox, because of the wonderful ability to use ad-ons with it. These tools make it so much easier to find bugs and to make things work the way I want them to. Below are my favorites.

  1. Web Developer: I've been using this tool for years now. It is a collection of a bunch of tools all in one location and allows you to see outlines of various blocks, information about different elements, and so much more. I highly recommend having it available if you do any web development work.
  2. Firebug: This is my new favorite tool. It is a debugging tool that allows you to inspect various aspects of a web page. You can then make changes to the site live, such as changing, adding, or removing CSS or HTML code to see how it will affect the site.

    It also has several tools that can be added on that you might find useful. These are the three I use:

    • YSlow: Yahoo has some specifications they list for making a fast loading web site. This will give you a list of why a site might be running slowly. You can then choose which to change to make it run faster. Yes there are websites that do this, but having it in the console is nice.
    • FireCookie: Allows you to inspect, add, delete and change cookies on a site. If you are working with cookies, this tool makes troubleshooting so much easier.
    • FirePHP: My sites are all PHP based. With this tool, I add a little code to my site and then the PHP log is send to the Firebug Console for me to read. A real time saver.
  3. ColorZilla: A simple color picker tool. If you need to know what the color is, just turn it on, mouse over and look at the status bar. It will display RGB, HTML # notation, and what element is setting the color. Will pick anything on the page, including within images.
  4. HTTPFox: This is a new tool for me, but I like it. I turn it on and it allows me to see the HTTP Headers live. It only collects data when you tell it to, so the data does not get overwhelming. Now there is no guessing at what data is being posted or what headers are being interpreted. It also does cookies, so you might not need the FireCookie extension.
  5. MeasureIt: This cool tool allows you to measure an area. You click to turn it on and then click and drag to measure the region and it returns width and height. Simple, yet useful.
  6. SeoQuake: This is an extra. Not really a developer tool, but many times you also need to think about SEO as you work. This tool allows you to see, all on one bar, the Google PageRank, Google Index, Yahoo Links and Link Domain, MSN Index, Alexa Rank, Site Age (using WayBack Machine) and more. You can add other extensions to it as well.

Open Source: Programming at It's Best

If you have not gathered it from my other posts, I love Open Source. Not just because I'm too frugal (I like that word better than cheap) to pay a bunch of money for programs, but because it is often better, or at least as good as the for pay versions. Even though I love open source programs, I have come to realize that many do not know what it is. In several conversations with techs and programmers, it has become quite clear to me over the years that many people in the high tech industry to not know what this standard is. Today I will break down what open source is, how it is beneficial to users, and why programmers should program open source tools. At the end I will list some of the most popular open source products and their commercial equivalents.

What is Open Source?

The simplest way to define open source is to say that it is a license put on a program where the source is open to everyone. I've heard it said that there are two types of free. There is the free in that you can use it for free. In programming, this is often called "freeware". Then there is free as in freedom. This is open source. You are not just free to use it, you are free to change it. You can take the program, and the source code for it, change it, repackage it and let others use it. Read the complete definition here.

Why Should I Use Open Source?

It's Free. You don't have to pay for it, ever. Nothing, not a dime. No restrictions on when and where you can use it without paying.

There are a World of Programmers. You are not relying on the programmers working for the company who writes the software to come out with bug fixes and updates. Anyone can do it. You can then download them and run the patches to get your version to do the same. Of course, you want to make sure it is coming from a trusted source, but many of these fixes are sent to the package maintainer (a fancy term for the main contact who is in charge of the program) and included in the next version. This means that there might be thousands of people working on these programs to get them ready for you to use, instead of the few, or maybe dozens, that are paid to work on them when you buy commercial versions.

They Work as Well as the Commercial Version, if Not Better. This blanket statement is often true, but not always. Sometimes the commercial version is better, but often the open source one is. They might have different parts of them that work better than the commercial versions, but open source competes with the commercial versions very well. They just don't have a big marketing division to get you to buy them.

You Can Open the Commercial Versions Documents. Just because a document was created in a commercial program does not mean that you need that program to open the documents. Most of the open source versions allow you to open and save in the commercial programs formats. There are exceptions to this rule, so be sure to check out the program to see if it will do what you need.

Will Open Source Work for My Business?

Does your business like to save money? Does your tech support know how to support the tools, or are they willing to learn? Is your staff willing and able to learn the new software? If you can answer yes to these questions, then it will work for you. The tools do not work exactly the same, and some tools are not as good as commercial versions, but if you can find the tools you need in open source, you can save some money. Like with any change, you will need to train your staff. Don't just install it and tell them to use it. Teach them how and you will get a better response and it will work better for them and you. If you don't train them, then they will want to go back to what they already know.

Why Should I Program In Open Source?

Get Others to Code With You. In The Cathedral and the Bazaar Eric Raymond describes open source as a bazaar where programmers with differing agenda are all working on the same thing from different points of view. If you are working on a program and want others to help, open source is a good way to achieve this goal.

Build Loyalty. As a business, you can gain a lot of loyalty quickly by building in open source. Your developers will have a sense of ownership because they are working to develop it in their way, instead of the companies way.

It Is Less Expensive. As a business you only have to pay your core developers. Other developers will come on board with their own agenda and make your product better. You don't have to pay them.

Good Way To Learn. It is a good way for a newbie programmer to learn more about the trade. You can read the code others have written and make minor changes. If you are working on a good project, they will have a list of things that need to be done. You can pick one that is at or just above your skill level and go to work on it. In this way, you are working on a large project that you might not normally get to work on, but you are keeping it at a level that you can do on your own.

Sense of Community. Since you are working with a large group of programmers, there is a sense of community in working with all the programmers and learning from each other.

Can I sell open source? Yes. You can sell and make money from open source programs. But so can anyone else. If you write a program and license it open source, you are allowing anyone to look at your code, change it, and sell it, if they like. It is hard to make a business work with an open source business model, but several have.

Open Source List

Commercial Program - Open Source Competitor
Microsoft Windows - Linux. There are many versions of Linux. Check out Distro Watch as they will give you lots of information about the various versions right as they come out. You can even run Windows programs under the WINE emulator.
Microsoft Office - Open Office. Open Office will open all MS Office documents and save in their formats.
Internet Explorer - FireFox. Many of us use Firefox instead of IE, but not everyone knows that it is open source.
Internet Information Server - Apache Web Server. Apache is a great web server. It has a huge market share and does a great job.
Adobe Photshop - The GIMP. The first non-Microsoft commercial product on the list. The GIMP is great, but there are some things about Photoshop that make it a little better, but unless you are using it nearly every day, it may not be worth the $650 retail price tag. Some places sell it for $500 or less, but it is very expensive anyway.
Quicken - GNUCash. ok. This one is no competition. Quicken is hands down the best here. It is not really expensive and has way better tools. I think that people are afraid of working on financial stuff without the backing that intuit has. There are too many things you could screw up for people. There are no good alternatives in the open source world.
Audio Editing - Audacity. I don't actually know a commercial version that competes with Audacity. Of course, I'm not an audio expert either, but if you do a little audio work, Audacity is the way to go.
Many other products exist, but these are the ones I use regularly. I have two computers, and one has no commercial software on it. The other I'm making dual boot, because there are times when I need to test in Windows. I will then have some commercial software on one boot partition (the software that comes with Windows). For a more complete list of Open Source programs, check out Wikipedia's list

Syndicate content