Posts tagged ‘JavaScript’

Cleaning Up Some PHP And Incorporating A JavaScript-Based Image Preview

Recently asked on Yahoo! Answers:

Change image when new option selected in PHP?
This probably will also require Javascript. I am using this code:
What it does is draws from a MySQL database where an “avatars” table is set. The “avatars” table includes fields “id” (INT), “title” (VARCHAR) and “url” (VARCHAR).
Basically, the script draws the rows from the table. It puts the titles in an HTML select form as options.

{code block snipped}

When an option is selected, I would like the image that is selected to be shown, without a new page having to be loaded.

This is very easy to do with JavaScript and DOM manipulation, but based on the user’s code, it requires a bit more work that previous blog entries I’ve made on images and JavaScript.

Let’s start with looking at the original code block.

Continue reading ‘Cleaning Up Some PHP And Incorporating A JavaScript-Based Image Preview’ »

More Sociable Hacks: Adding MySpace And Yahoo! Buzz, Changing The Look

I’ve done some more hacking of the Sociable social bookmarking plug-in for WordPress and I’d thought I’d share them with others.

I’ve added buttons for MySpace and Yahoo! Buzz. I’ve also changed the layout to put all the icons inside a fieldset tag and eliminate the rollover effects.

I’ve also cleaned up some more code, most notably making a change to the way the script creates excerpts when the user has not done so.

Let’s look at the changes.

Continue reading ‘More Sociable Hacks: Adding MySpace And Yahoo! Buzz, Changing The Look’ »

A New Low, Even For Yahoo! Answers

I really shouldn’t snicker. But the link below is to what has to be the stupidest question I’ve ever seen posted on Yahoo! Answers. And considering I’ve answered over 6,500 questions, that’s saying something.

Prepare yourself; think “dead puppies” or “naked grandmother” before clicking this link, or you may well burst an aneurysm laughing:

http://answers.yahoo.com/question/index?qid=20080731201724AAhwdIy

Continue reading ‘A New Low, Even For Yahoo! Answers’ »

Some Insight Into The Document Object Model: How Forms Are Stored By JavaScript

Recently asked on Yahoo! Answers:

JavaScript Submit form?

Ok, I know of the function…

function submitform() {document.myform.submit();}

however, for this to work, I have to name my forms, also, if I want to use it multiple times on the same page, each funtion name must be unique.

Is there a way to replace a submit button with javascript without having to uniquely name each function, or name my forms? I’m just looking for a link replacement for the submit button. So, a submit button just sits at the end of the form and submits it’s data, can I just replace it with a link, no extra stuff except one function at the top of the screen?

Also, the form’s method is POST, not GET. Also, the action is $PHP_SELF; though, if you give me an ideal answer, the JavaScript won’t even worry about the action.

This is something I don’t see as often as I’d like: A question about how something works, and a question with a practical application.

In other words, the user wants to solve a particular problem, but the question is posed in a way that its answer can reveal a great deal about how the Document Object Model (DOM) works. And understanding how things work makes programming far easier, believe me.

I began answering the direct question of how to submit an unnamed form with JavaScript, but then decided that, because opportunities like this are so rare, I’d seize upon it and launch a full dissertation here.

Before we begin, I’m going to be tossing around some terms about how classes / objects work. If you’re new to programming, or at least object-oriented programming, and aren’t familiar with terms such as “property,” “method,” “event” or “namespace,” you’ll want to check out my earlier entry, titled “Objects (Classes) Explained In Very Simple Terms.”

Continue reading ‘Some Insight Into The Document Object Model: How Forms Are Stored By JavaScript’ »

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.

Continue reading ‘Fixing An “Unterminated String Literal” Error In An AJAX JavaScript HttpRequest’ »