Aug 19

Adding HTML fields so that when they get submitted back to the server in an array format is easy, but what about when you want to group multiple fields together to make up a single entry? Most of the time, that’s when things get a bit tricky. In order for the array fields to be related to each other as one record, you often end up having to place an ID or an incremented number in the array somewhere, usually something like this:

<p>
Caption: <input type="text" name="images[0][name]" size="20" /> 
Image: <input type="file" name="images[0][url]" />
</p>
<p>
Caption: <input type="text" name="images[1][name]" size="20" /> 
Image: <input type="file" name="images[1][url]" />
</p>

This is an okay solution for form elements that are not likely to change, but this method quickly gets very complex when adding additional form elements dynamically to the DOM through javascript. You can’t just copy the HTML code with javascript anymore - you have to increment the number in order to keep those new fields together when they are submitted to the server. The end result most of the time looks ugly and hackish. So what if we could simplify everything by submitting the fields together in a straightforward way that will cut all that number incrementing out? Read More


Jun 24

The documentation wiki on TinyMCE provides a great tutorial on how to turn TinyMCE into a standalone AJAX editor.  But what it doesn’t have is a great tutorial on how to update the contents of TinyMCE into the original <textarea> form element for more passive AJAX saving.
Read More


May 14

All too often, I see code like this, attempting to test if a particular string is found in a variable:

$test = 'abc123';
if(strpos($test, 'abc')) {
    // do comething
}

This statement is actually a common mistake, because the strpos() function returns the integer position of the first found letter in the string searched for. So in this example, strpos() would return integer ‘0′, because the string ‘abc’ starts at the very beginning of the string (the ‘0th’ position). And just like C/C++, when you put ‘0′ in a IF statement, it gets evaluated as FALSE. So although the string ‘abc’ has indeed been found in the $test variable, the IF statement will not be evaluated as such, because the match occured at the beginning of the string and ‘0′ was returned. Instead, you must use the strict triple comparison operator to test if the strpos() function has returned FALSE (string not found).

The code should look like this instead:

$test = 'abc123';
if(strpos($test, 'abc') !== false) {
    // do comething
}

So if the strpos() function does NOT return boolean FALSE (string not found), we know the string has indeed been found in the variable we are testing for. The IF statement will execute, even if the string begins at the ‘0th’ position.


Feb 22

SimpleXML does not correctly parse SOAP XML results if the result comes back with colons ‘:’ in a tag, like <soap:Envelope>. Why? Because SimpleXML treats the colon character ‘:’ as an XML namespace, and places the entire contents of the SOAP XML result inside a namespace within the SimpleXML object. There is no real way to correct this using SimpleXML, but we can alter the raw XML result a little before we send it to SimpleXML to parse.
Read More


Feb 4

The Problem:

When you load up the PHP script you just created, you get the following error message:

“Warning: Cannot modify header information - headers already sent by …”

Which is then followed by a file location and line number where the output started, and the file and line number where the error occured (the function call where the header was trying to be sent).

The functions that send HTTP headers that can cause this problem are header() and setcookie().

The Explanation:

This problem occurs when you are trying to send HTTP header information AFTER output to the browser has already been started. The most common occurrence of this error is when you have some code that looks something like the following:
Read More