1 0 Archive | programming RSS feed for this section
post icon

Unobtrusive javascript textarea counter using Mootools Event Class and domready Event

Here’s a cool script for using with your applications if you would like to limit the number of characters in a textarea as well as show a user how many characters they have left.

I wrote this using Mootools event and domready Event classes. For this to work you’ll need to have Mootools Event and DomReady classes. Then embed that file in the top of your document.

Feel free to use it, but if you think it’s sweet links here wouldn’t hurt… but that’s up to you! Have fun!

<script type="text/javascript">
/*Created by Darren Terhune January 16th 2008 http://headfirstproductions.ca*/
window.addEvent('domready', function() {
 //check length function
 function check_length(form) {
  max_length = 255; //set the max length of the form
  if (form.testimonial.value.length >= max_length) {
   form.testimonial.value = form.testimonial.value.substring(0, max_length);
  } else {
   form.num_left.value = max_length - form.testimonial.value.length;
  }
 }
 var testimonial = $('testimonial');
 testimonial.onkeydown = function(event) {
  check_length(this);
 };
});
</script>

Here’s my html form

<form id="testimonial" method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>Name (Person who gave the testimonial)</p>
<input name="name" type="text" tabindex="1" size="40" value="<?php if (isset($_POST['name'])) { echo $_POST['name']; } ?>">
<textarea tabindex="2" name="testimonial" cols="50" rows="10"><?php if (isset($_POST['testimonial'])) { echo $_POST['testimonial']; } else { echo 'Please enter the testimonial here... (max 255 characters)'; } ?></textarea><br />
<p>Characters Left</p>
<input size="3" value="255" name="num_left" id="num_left">
<input type="submit" tabindex="3" value="Submit">
<input type="hidden" name="submitted" value="TRUE">
</form>
Leave a Comment
post icon

Javascript function to uncheck all checkboxes

Yesterday I searched and searched for a good hour for some unobtrusive javascript to set all check boxes within a div to unchecked when a user would click “None” for a none of the above type form, when I realized that, duh I know how to write javascript. So I wrote this function.

Javascript:

function $(id) {
 return document.getElementById(id);
}
window.onload = initPage;
function initPage() {
 var productDiv = $("products");
 var productList = productDiv.getElementsByTagName("input");
 var productReset = $("product-none");
 productReset.onmouseup = function() {
  for (i = 0; i < productList.length-1; i++) {
   productList[i].checked = false;
  }
 }
}

HTML:

input value="1" name="product[]" id="Carpeting" type="checkbox" Carpeting
input value="2" name="product[]" id="Hardwood Flooring" type="checkbox" Hardwood Flooring
input value="3" name="product[]" id="Ceramic Tile" type="checkbox" Ceramic Tile
input value="4" name="product[]" id="Ceramic Tile" type="checkbox" Ceramic Tile
input value="5" name="product[]" id="Kitchen/Bathroom Cabinets" type="checkbox" Kitchen/Bathroom Cabinets
input value="6" name="product[]" id="Counter Tops" type="checkbox" Counter Tops
input value="7" name="product[]" id="Major Appliances" type="checkbox" Major Appliances
input value="32" name="product[]" id="product-none" type="checkbox" None

I’ve taken out the html tags cause it was causing this page to format wrong so you’ll have to just understand how the tags would look. Someone tell me how to get around this?

So basically this javascript creates an window onload then finds the div with id => “products”. Then finds all the input elements and puts them into an array. Then finds the input with the id => products-none. Then creates a function to run a for loop across the array and sets all check boxes value “checked” to false when a user releases a mouse click on the products-none input.

I’ve only tested this in Firefox 2.0 Mac OS X
Feel free to use it modify or what ever you like!

Leave a Comment
post icon

A Flash Game

I just finished a small project for a friend of mine. I made a sweet flash game, where you try and shoot down helmets while being timed. After you shoot all the helmets you enter your initials and email address for a chance to win a free Darren Berrecloth signed, signature Deviant Helmet from Specialzied! Well you have to have the low score but it’s a fun time anyway!

I used Flash and Php together for one of the first times and had some fun. Actually the game was just a modified version of a project I did in school earlier this year. It didn’t have the php/mysql funtionality to log scores, but meow it does!

Leave a Comment
post icon

Understanding How Joins Work

Mike Nixon sent me this link that does a pretty sweat job of describing how the different types of joins work when playing with a database.

Leave a Comment
post icon

Deleting a file in php using unlink

Tonight I was having troubles deleting a file from a directory in an application I am working on. It kept deleting the entry in the table but not the actual file from the directory. Here is what the code looked like:

< ?php
$sql = "DELETE FROM uploads WHERE upload_id = $id";
$result = mysql_query($sql);
//if it ran ok
if (mysql_affected_rows() == 1) {
 delete the file from the uploads dir
 unlink('uploads/'$row['file_name']);
}
?>

I couldn’t figure out for the life of me then I tried this:

< ?php
//delete the file from the uploads dir
unlink('uploads/' . $row['file_name']);
$sql = "DELETE FROM uploads WHERE upload_id = $id";
$result = mysql_query($sql);
//if it ran ok
if (mysql_affected_rows() == 1) {
 //do something
}
?>

It worked perfect! I just had to seperate the name ($row['file_name']), which could also be a variable with a space: ‘uploads/’ . I also had to make sure the unlink was before the DELETE query because if it was after the query it would be trying to delete something that wasn’t there.

Leave a Comment
31. Mar, 2007