Archive for the 'SQL' Category

Fixing An “Unterminated String Literal” Error In An AJAX JavaScript HttpRequest

So you’ve written your first AJAX program, tested it, and it seems to work. Except sometimes, when your “helper script” is supposed to return some data from your database, you get this error:

Unterminated string literal

What gives? Basically, you’ve run into a quote mark in your data — and because it doesn’t know any better, JavaScript thinks you mean for it to end a string.

Of course, that’s not what you meant. As a result, you have more text appearing after what JavaScript considers the end of a string.

And when JavaScript sees that extra text, it wants it to start and end with quotes. Since, as far as JavaScript is concerned, that extra text isn’t fully enclosed in quotes, it’s unterminated — thus, an “unterminated string literal.”

Let’s look at this more closely, and see how simple it is to clear up.

Read the rest of this entry »

A Charity Donation Recognition System Using PHP, MySQL, JavaScript And DOM

Recently asked on Yahoo! Answers:

Divide photo in 5,000 pieces with rollover text?
we are raising money and selling support tickets. To show our progress we are going to start with a black and white image split in 5,000 pieces then as each piece is sold we want to turn it into color.

When you roll over each piece the person who purchased that support ticket will have their name show up as well as a personal message.

any opinions on how this should be built? The admin section will all be managed manually, so we’ll just need a backend for the admin person to go in and make the changes manually.

This seemed an interesting challenge to me; not difficult, but with multiple steps that no beginner could hope to properly tackle on his own without some help.

This project calls on almost all the basic skills a competent Web developer should possess: Image editing; database design and simple queries; DOM manipulation; security and back-end systems, and therefore, it gives me my first real opportunity in quite some time to address a project in full scope.

As always, I’ll have a link to a working demo and code you can download at the end of this entry.

Read the rest of this entry »

Multilingual Web Pages Via PHP, Arrays And MySQL

Asked recently on Yahoo! Answers:

What is the best method to create a multi-lingual website?
Do we use xml, php, or what??? and how, help appreciated.

There are many different ways to go about having a Web page appear in multiple languages, and I may address others in upcoming articles. The approach I am going to focus on in this post assumes you already have your translations in hand, and simply want to give users a choice of languages in which your page can appear.

In the future, I may address ways of determining the default language to present to a user, based on his browser’s user-agent string; translating text on-the-fly via AJAX and one of the free translation Web sites (even ones that don’t have an API), and using alternative data sources, depending on interest in this article.

For now, two demos using PHP: The first will employ an array, the second will use a database as its back end. As always, I will have a link to a working demo and code you can download at the end of the discussion.

Read the rest of this entry »

Variable PHP Recordset / Results Set Pagination

Recently asked on Yahoo! Answers:

Paginate in php: 3 items on front page-then 1 item on next pages?

I know how to paginate mysql results, but have only been able to have the same number of results appear on page 1 and the remainder of the clicked pages. However, I want to have a different set of results on the first page, and then on pages 2 and up, I want a different number of results. For example, on page 1, I want 3 results, then on pages 2 and up, just one result. Thanks.

I’ve covered this several times before, but never as a seperate topic, and it’s often asked: How do you paginate a PHP recordset?

It’s fairly simple, using mysql_data_seek() and a few checks to ensure our variable stays in range. We begin by assuming you will use one page to display your recordset, and will keep passing a page number parameter via querystring to that page, like this:

http://www.mysite.com/records.php?page=1

If you want to move to the second page of records, the url would be:

http://www.mysite.com/records.php?page=2

And so on. Let’s begin with the basics of PHP results set pagination; after that, the specific request.

Read the rest of this entry »

Using AJAX To Update A Non-Map DIV Via Google Maps API’s GDownload() And GMarker OnClick Event

Recently asked on Yahoo! Answers:

Is it possible to have a google map invoke an Ajax request?

I basically need a google map to invoke a different Ajax request to load a part of a page outside the map when an item on the map is clicked…. is this possible? I know that the map itself uses Ajax when you scroll too far, to call in new imagery, but can I also modify the map to load/swap a different part of the page? I’m using ASP.NET pages too, btw.

The answer is “Yes,” thanks in large part to the Google Maps API’s built-in GDownload() class and the GMarker’s onclick event.

All we need to do is add an event listener that waits for our marker to be clicked, then call a function that will accept some unique ID for the marker, and cull the relevant information from some helper script. For this example, I’ll use PHP to pull a record from a MySQL database that gives details about the thing the marker is pointing out.

This discussion assumes you have basic familiarity with the Google Maps API, or that you can at least follow the documentation overview.

As always, you can download the source code for this example at the end of the discussion.

Read the rest of this entry »