Archive for the ‘Lessons learned’ Category

Image as buttons (and IE issues)

Monday, March 7th, 2005

Oh, this one was going to drive me NUTS! I had to use images as buttons in a form. That’s easy enough:

<input type="image" SRC="blahButton.gif" ALT="blah" name="blah" id="blah" value="blah" />

Which works fine when simply submitting the form when there’s ONE button. But on one page I had TWO buttons. The form would submit to itself, and then depending on which button was clicked, I would handle the form as appropriate. Worked fine in firefox - nothing happened in IE. I found this page in a search.

What I had been doing was simply (in php)

if (isset($_POST["blah"])) {
//do stuff
} else if (isset($_POST["blahblah"])) {
//do different stuff
}

What fixed the problem was changing that code to this:

if (isset($_POST["blah_x"])) {
//do stuff
} else if (isset($_POST["blahblah_x"])) {
//do different stuff
}

Now my form works…

ASP: Look up / translation script

Wednesday, November 10th, 2004

I should create a category called “saved my ass”! This script would fall under it.

I was asked to create a “look up” tool that would sort of “translate” one piece of text to another. The user would enter in the first piece of text, and then they’d be given the “value”. Oh, and this list updates every few months or so.

At first I started looking into creating a database, and writing a script that would update that database - problem was that creating and accessing a database for this project was going to be time consuming and difficult. A server-side script would have been the best solution under the circumstances. A few hours this morning searching around turned up this “dictionary” script.

I’m actually being given the the text and values in excel, so I just need to create a sort of “template” in excel that formats everything so it will look like the code - and then I just copy and paste it all into the ASP file.

So here’s kind of what I have in my file:

SET TextToNumber=CreateObject(”Scripting.Dictionary”)
TextToNumber.Add “name”, “292949″
TextToNumber.Add “date”, “123414″
etc. etc…

Then my form looks like this:

<form name=”textlookup” action=”lookup.asp” method=”post”>
Text: <input type=”text” name=”text” value=”" /><br>
<input type=”submit” name=”submit” value=”Look Up Text Number” /></form>
Text: <% Response.Write(Request.Form(”text”)) %><br>
Number: <%
TextValue=Request.Form(”text”)
NumberValue=TextToNumber(TextValue)
Response.Write(NumberValue)
%>

PHP: simple logic statement

Friday, November 5th, 2004

I’m tired of searching for the correct syntax everytime I wan to use this line… if you’re doing a simple one line “if” statement, you can use a “ternary conditional operator” - for example:

echo $variable == ‘value’ ? ‘print if true’ : ‘print if false’;

Continue vs. Break (in while loops)

Monday, October 25th, 2004

(note to self type post)
Using “continue” in the middle of a while loop will skip the current iteration and go back to the beginning of the loop (checking the while value again).

Using “break” in the middle of a while loop will break the loop entirely. (no more looping)

So this code

<?php
$i = 0;
while ($i < 5) {
$i++;
if ($i%2) {
echo “value = ” .$i . ” - inside if<br>”;
continue;
}
echo “value = ” .$i . ” - outside if<br>”;
}
?>

Will produce this:
value = 1 - inside if
value = 2 - outside if
value = 3 - inside if
value = 4 - outside if
value = 5 - inside if

Where as this

<?php
$i = 0;
while ($i < 5) {
$i++;
if ($i%2) {
echo “value = ” .$i . ” - inside if<br>”;
break;
}
echo “value = ” .$i . ” - outside if<br>”;
}
?>

Will produce this:
value = 1 - inside if

See more about while loops here.

Button Element

Monday, October 25th, 2004

Hello, I’d like to take HTML 101.

So I was working on a form. Used this method to create the form. Minimal HTML. Simple CSS. Started playing around with the :focus pseudo-class… and that’s when I noticed my submit button started looking crummy. (From a usability perspective, I think there are some things that SHOULDN’T be over-styled. Scrollbars, Buttons. People are used to seeing these have the default look of their OS or Browser. Mess with them, and while you may think YOUR design of them looks pretty, they’re probably not as usable anymore.)

So I went searching for a way to revert an element back to it’s default, and basically discovered that it’s not possible. But the search led me to this page where the suggestion was to simply use the <button></button> tag instead of <input type=”button” /> W3C says you can even (however, the validate local html through the web developer extension in Firefox is giving me errors on all the attributes, even though they are within spec of the DTD.)

Button element?? Ok, add that to the list of things I probably should have known about but didn’t! :-/

gifs that execute a php script

Tuesday, October 19th, 2004

Problem: (somewhat in line with my last post) I needed to run a script in the “background”, but I only had the ability to present the script as a .gif.

Solution: actually, I came up with two ways of doing this and both use .htaccess to pull it off.

1) I’m a gif but really I’m a php script
This one is fairly straightfoward. I put my script in a seperate directory, (that doesn’t actually include any images!) and renamed it from filename.php to filename.gif (or filename.jpg) (Yes, I know it’s not really an image file. Hang in there)

Then, in the htaccess file for JUST THAT FOLDER! (similar to this trick) I added this:
AddType application/x-httpd-php .php .jpg .gif

FYI - in your php file (that now looks like a .gif or .jpg) make sure you don’t have any headers/text echoed, and at the end of the file add this to your php code:
header('Content-Type: image/gif');
@readfile( '/SERVERPATH/TO/A/REAL/IMAGE/spacer.gif' );

You can then include the “script” as if it were an image file, it will run the script, but only display a gif to the user.

You can even pass it variables like filename.gif?somevariable=somevalue&anotervariable=anothervalue, but there’s another way to do that too without the ugly URL.

2) I’m a gif that’s actually a php script super-powered with rewrite
Ok, starting from the beginning. You have your script (filename.php) in a folder, add the following to the .htaccess file in that folder:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/(.*)/spacer.gif /YOURFOLDER/FILENAME.php?somevar=$1&anothervar=$2 [QSA]

Before we continue, I must tell you that I dont’ completely understand rewrite rules. There’s a LOT of holes in my knowledge. So if you have a better way, PLEASE post it in the comments. I would LOVE to be able to understand what the hell I’m doing. LOL.

OK, moving along. Now you can call your script by actually calling that “phantom” spacer.gif, with the variables in the URL as if they’re directories like this:

http://www.yourdomain.com/YOURFOLDER/somevalue/anothervalue/spacer.gif

Again, make sure you have this at the end of you php code in your filename.php:
header('Content-Type: image/gif');
@readfile( '/SERVERPATH/TO/A/REAL/IMAGE/spacer.gif' );

Border tricks and centering issues

Saturday, October 9th, 2004

I’m working on redesigning a site in CSS that I did many eons ago using tables for Christine. While I’m getting the hang of CSS, I still have a long ways to go.

First of all - Christine wants a border around her photos: first a thick white one, then a one pixel black one. She had previously been doing that with photoshop, but I’ve suggested to her that it would be better to just do it in CSS… On my first try, I had tried wrapping the img tag in a number of divs - which not only didn’t really work quite right, it added extra html that didn’t need to be there. The answer was simple. Here is the CSS I used for the border (a class I add to the img tag) :

padding: 15px;
border: 1px solid #000;
background-color: #fff;

The other thing I was struggling with was another (should have been) simple issue. I simply wanted a block of text, 600px wide - centered on the page, but with the text flush left. The key I was missing was adding margin-right:auto; and margin-left:auto;. So here’s the CSS for the outer div - and the div for the text block:

#content {
text-align: center;
}
#text{
width: 600px;
margin-right:auto;
margin-left:auto;
text-align:left;
}

The only way I was able to figure that out was from looking at the examples on Max Design: CSS Page Layouts

ASP: get the name of the current page

Tuesday, October 5th, 2004

(Side note: I hate ASP. I love PHP. But sometimes mean people make me use ASP) ;o)

I needed to get the current page name and redirect to a different page if it was a certain name. (ie. 1.asp or 2.asp or… up to any two-digit + .asp)

First - to get the page name I used a script I found here:

Dim strURL, aryURL, pagename, objRegExp
strURL = Request.ServerVariables(”SCRIPT_NAME”)
aryURL = Split(strURL, “/”, -1, 1)
pagename = aryURL(ubound(aryURL))

Then I check the page against a reg exp:

set objRegExp = new RegExp
objRegExp.Pattern = “^[0-9]{1,2}\.asp”
if objRegExp.Test(pagename) then
Response.Redirect(”http://www.example.com/pageToRedirectTo.asp”)
end if

(FYI - I’m trying to get a better grasp of regular expressions. I know they’re useful, I just can’t seem to wrap my head around them. We’re still at the “it makes my eyes bleed” stage - which is also pretty much how I feel about ASP)

OOP in JavaScript.

Thursday, September 30th, 2004

The article by Sergey Zavadski introduces the object model of the JavaScript programming language and demonstrates common practices in the OOP (object oriented programming) with the JavaScript.
(more…)

PHP: Hiding Errors

Thursday, September 30th, 2004

I was running into a problem with undefined index errors. You’ll get that if you do seomthing like:

echo $_GET['name'];

when there is no $_GET['name']. One option is to check to make sure $_GET['name'] is set like

if (isset($_GET['name'])) {
echo $_GET['name'];
}

But if you’re feeling lazy, you can leave your code as is and just add this at the top of your file

error_reporting(E_ALL ^ E_NOTICE);

Alternatively, turning off those notices is a setting you can adjust in .ini file, but if you don’t have access to that file, then just putting that error_reporting line in your code will suffice
[found on experts exchange thread]