I ran across an interesting situation where some users of mine were complaining that a form I created “didn’t work”. Kind of strange to get this complaint after almost a full year of operation. I remember testing the xhtml and css design in both firefox and IE browsers and it all checked out. What I didn’t think of checking was the behavior of browsers when one presses the return key. With the help of Fiddler (thanks to Casey Williams for the Fiddler suggestion) this is what I discovered:
|
IE |
Firefox |
| Submit Button Clicked |
All inputs $_POSTed |
All inputs $_POSTed |
| Return Key Pressed |
All inputs $_POSTed except form buttons |
All inputs $_POSTed |
(For a larger, more detailed image click here)
As you can see, when my IE users were pressing the return key, the button itself was not being $_POSTed. So what right? Well what if you were checking if the submit button was pressed before doing any processing? Like so…
< ?php
if(isset($_POST['button_name'])){
// validate the user entries...
}
?>
This would fail if the user hit the return key in IE7. I haven’t tested earlier versions.
View the example. Test it in both browsers.
Right now the only thing that was suggested to me was to add a hidden field to mimic the submit button in your form like so:
<form ...>
<input type="hidden" name="button_name" value="whatever"... />
</form>
That works just fine.
From perusing irc and several forums, the only response as to WHY this happens are comments like “It’s IE, what do you expect?”. I’m already annoyed that IE doesn’t think it has to adapt to web standards, but this ont really caught me off guard.
I’m wondering if any readers might have some input on my inputs ;P
Posted by ^Lestat on Friday, September 26th, 2008
I came across a situation where I needed to pull records from 1 db to another on a recurring basis. I read many different ways to do it via searching around the net. Some of the suggestions included creating a temporary table and copying it over. What was lacking was that sometimes the new table also needed to be UPDATED, as the information changed from the original table.
I’ve come up with a dirty little example to show this can be done. There are more keywords that can enhance the ON DUPLICATE KEY UPDATE function even more. The Example is assuming an “employees” table that looks like so:
| employees |
| id (PK) |
| first_name |
For brevity, I’ve left out connection data etc;
$myArray = array(0 => array('id' => '1', 'fname' => 'Steve'),
array('id' => '2', 'fname' => 'sara'),
array('id' => '3', 'fname' => 'Matt')
);
// Don't forget to validate & clean your data
// Connect to db here
foreach($myArray as $key => $value){
$query = "INSERT INTO employees
(id, first_name) VALUES ('$value[id]', '$value[fname]')
ON DUPLICATE KEY UPDATE first_name = '$value[fname]'";
$result = mysql_query($query);
if(!$result){
print("Problem: " . mysql_error() . "");
} else {
print("Success !");
}
}
Read the manual on this for more information. This snippet will INSERT if a UNIQUE (in this case ‘id’) does not exist. It will UPDATE any existing unique. In this case any existing ‘id’s, the ‘first_name’ column will get updated.
One could also use the REPLACE function. As I understand the difference, REPLACE will DELETE any matching uniques, and INSERT a new record in it’s place.
Comments? Better ways? I’m always up for learning something new so please chime in 
Posted by ^Lestat on Thursday, December 20th, 2007
This may seem simple enough to many programmers out there but this threw me for a few days. This is how you install imagick on a windows server. If your site is on a shared host, you need to ask your host to install the extension for you.
For this example I am running php 5.2.4 on Abyss/Apache web server
Download the matching pecl5 Binary from php.net. In this case its pecl5.2-win32-200710121230.zip (matching my current php version).
Unpack the file.
Copy the php_imagick.dll into php/ext/ directory

Open your php.ini file and look for the “Dynamic Extensions” area. In there will be a list of items (some commented out). Add the following line to the bottom of the list: extension=php_imagick.dll

Save the changes you made.
Restart your web server.
If these steps were successful imagick will show up on any php page using the phpinfo() function.

Technorati Tags: imagick, pecl-extension, php-extension
Posted by ^Lestat on Friday, October 12th, 2007
Filed under php, Programming
I was writing up a few scripts and as I was adding my commenting I noticed I tend to comment differently depending on the difficulty of the script and how many different parts or procedures there are to it. I don’t have a set convention, but I suppose as with all things it would make things easier on myself and others if I had some kind of standardization…
For larger ‘areas’ that contain functions or a series of procedures that extract or parse sections of data I’m now starting with this:
/***
*
* This is a comment.
* Here I leave notes about the section and what it does.
* I may also include notes to myself here.
* Note: This could probably be done more eficiently
*
*/
As there are more breakdowns within these sections I’m using this:
1
| /* [ Parse out the text file ] */ |
And further drilled down I use the more inline commenting
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
/* [ Print data] */
$parts = explode("|", $the_array); // get the parts
$color = false; // set color var for shading every other row
print("<table>");
// loop through data & print
foreach($parts as $key => $row){
// CSS Note: shading <tr>'s with css mostly works with nice browsers
// For IE 6+ one needs to shade the <td>'s
if($color){
print("<tr class=\"rowColor1\"> "); // start row with color 1
$color = !$color; // unset the color so it alternates next loop
} else {
print("</tr><tr class=\"rowColor2\"> "); // start row with color 2
$color = true; // set the color so it alternates next loop
}
print("<td>$row[first_name]</td></tr>"); // print cell data and close the row
}
print("</td></tr></table>"); |
The inline comments still seem a bit ‘messy’ to me. But for now it will work.
One of the reasons I decided to standardize is because even with commenting and extra line spaces in the scripts it was still difficult to sort out what exactly was going on. Another thing that tripped my thought was having a glance at phpDocumentor. It seems to function on how you comment your code. I did try to install it but for some reason it was way over my head. It looks like a neat tool but I rarely seem to write more than 4 different files and those are often less than 300-400 lines.
I’ve yet to test it in comments but my plugin to allow code viewing is much like bbc code. [code lang="php"] // comment [/code]
So whats your style? Is there a standard? What have you seen that looks the cleanest? Leave a comment… with some comments
Update 12/3/07 Elizabeth Naramore has written a nice article on the effectiveness of comments in your code over at the Chris Shiflett blog. It’s a great read on WHY commenting is good in your code.
Technorati Tags: php, comment, code, script, php+script, php+comment
Posted by ^Lestat on Tuesday, August 14th, 2007
The folks over at Picktogame.com have a way for you to make games and have fun. Its easy to use and intuitive. Of course it’s still in Beta. The downside is that I noticed the signup isn’t secure (https).
What are you waiting for? Go ahead and get busy!!
Posted by ^Lestat on Thursday, May 31st, 2007