Today is Sunday
September 5, 2010

August 29, 2010

How to Check and Validate a file extension with JavaScript / jQuery

From time to time comes the need in almost every coders life to use an upload form on a page they are working on. Be it a single file upload form or a full form with contact information. In either event 99% of the time the case will be that the coder is going to want to double check the extension type. Grant it your going to want to check more than just that when you offer the ability to upload a file on to any server you are running a site on. To prevent a malicious user from attempting to hijack your site. I do feel however that it is worth mentioning that this only one of at very least several precautions you should take. This function alone will not secure your uploads 100%

The Function:

function checkExtension() {
  var extension = new Array(".png",".gif",".jpg",".jpeg",".tif",".pdf");
  var fieldvalue = $('#thefile').val();
  //var fieldvalue = document.formName.fieldName.value;
  var thisext = fieldvalue.substr(fieldvalue.lastIndexOf('.'));
    for(>var i = 0; i < extension.length; i++) {
      if(thisext == extension[i]) { alert("File type: Allowed"); return >true; } else { alert("File type: Not Allowed"); return >false; }
    }
}

Example of use:

<form action="#none" name="testUpload">
<input type="file" name="myfile2up" value="" /> <input value="Upload" type="button" onClick="checkExtension(); return false;" />
</form>


Demo:

Extra information:
The actual upload portion of this script has been disabled, this form will not upload anything it is for demo purpose only.

Allowed file types in this demo are:
".png",".gif",".jpg",".jpeg",".tif",".pdf"

August 28, 2010

WAMP Server Apache2TRIAD Files

by Chris — Categories: SoftwareNo Comments

A while ago I wrote up an article about Apache2TRIAD and how great of a WAMP set up it is for anyone from novice to guru, and my fear of the site closing down as the developer has stopped development, and recently posted a message on the site about it and everything. In that article I had mentioned I would likely swipe all the files I could find pertaining to the whole project and post them here as the suite still works despite its development stopping sometime ago. You can read my other article here Top Rated WAMP Server Apache2TRIAD

Now you may be asking what do I mean by still works, which to that I reply I mean I am on Windows 7 and with no effort at all I was able to install the WAMP and use it straight out of the box. I say this because from what I think I believe the development for the most part stopped late in the Windows XP era. Now I know not everyone is as lucky to just install it and have it work right out of the box. Everyones environment is different. But there is articles, and forums on the site that Apache2Triad resides on currently to help assist people get apache2triad running on other operating systems if there is a problem.

You might be scratching your head now, more so if you read the earlier article I wrote. It’s true I do fear the site closing. So how would the forums help you if the site closes you say. Well that simple (hopefully at least), if you noticed in another article on this site Coding Help And Support Forums For PHP, MySQL, JavaScript, JQuery And More. Or if you have taken notice to the “Forums” link over there in the navigation menu, then you might assume I have come up with what I hope to be a solution for that in the future should the apache2triad.net site ever shut down.

Now if you want to jump straight to the forums here for help your more than welcome to. I can try to help you any which way I can in getting done what you need or want to get done. My hope is to eventually have a big enough community on the forums to eventually have everyone get help quickly and efficiently. So in the same of shameless plugging. Sign up now, tell your friends or family and anyone else you know who codes and send them over to our forums for help if they need it.

Now.. to those files you might have been looking for. I have them below for you. The idea of the files is I went to the apache2triad site and just downloaded them from there. I will say I do not have permission from the developer to distribute them here but I’m personally thinking it’s not an issue. If I am wrong I will take them down upon the developers request.

Files obtained August 18, 2010. Posted here August 28, 2010 (obtained from apache2triad at sourceforge.net)

Apache2TRAID 1.2.3 – Legacy Version (notes | download)
Apache2TRAID 1.4.4 – Stable Version (notes | download)
Apache2TRAID 1.5.4 – Edge Version (notes | download)

Apache Assistant 0.0.1 (notes | download)
Andy a2t Backup 1.8 (notes | download)
Apache2TRIAD Installer Source (notes | download)
SRS Documentation (download)

August 21, 2010

JavaScript jQuery Jump to Anchor tag automaticly

by Chris — Categories: Coding, Javascript, jQueryNo Comments

Well assuming you know standard HTML and how to use it by now you should likely know how to set an anchor and jump to it with a static link.

For Example:

<a name="news">News Header</a>
<a href="#news">View News</a>

However what you might now know is how to have it jump automatically. This little function will help you do that. I will layout 2 versions of this function both pretty much one in the same thing however one you can define the page along with the anchor name and the other just the anchor for use with the same page its being used on.

function theAnchor(anchorName) {
   var sPath = window.location.pathname;
   var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
   location.href = sPage+"#"+anchorName
}
function theAnchorX(thePage, anchorName) {
   location.href = thePage+"#"+anchorName
}

Now the idea of using either of these functions is dependent upon you. How I would be likely to use them is through a conditional statement like if-else or case. But its not limited to that, you could use it just about anywhere you would place JavaScript.

jQuery is my checkbox checked

by Chris — Categories: AJAX, Coding, Javascript, jQuery1 Comment

Ok well first off let me establish that this is just straight JavaScript and not jQuery. However I post it notably as such due to it being such a big question. How do I use jQuery to see if my checkbox is checked. Because if you search for your answer (until someone else copies this article in there own words) your only going to come up with how to find maybe at best how to count checkboxes that are checked through the .length() function. You may come up with something about using the .is() or you might be told how to retrieve the .val().

But you don’t want to do any of those do you?. You just want to know if a particular checkbox one lonely little checkbox is checked. Then if it is checked run a particular bit of code, if its not checked then you want to do something else. But you really can’t find it in your searches. Well I feel your pain, I have done the same search as you looking for the answer. I gave up on my search though, I took what I knew already combined it with the little bits here and there that I noticed and came up with own rendition of how to do it. May not be the best, but works for what its worth and its rather light weight. So without any further waste of your time, here is your solution its been tested on Chrome, IE, FF, Opera, and Safari

function isitchecked(chk){
 var theCheckBox = document.getElementById(chk);
  if(theCheckBox.checked == 1){
    alert("Checked");
  }else{
    alert("Not Checked");
    theCheckBox.checked = 1; // will check automaticly if not
  }
}

Demo:

Example of use:

<input type="checkbox" name="checkme" id="checkme" /> <input type="button" value="Push me.." onclick="isitchecked('checkme')">

August 18, 2010

Top Rated WAMP Server Apache2TRIAD

by Chris — Categories: Software — Tags: , , , , , , , 1 Comment

This may not be software developed by Monkey Tooth but it is software Monkey Tooth uses to develop a pretty good portion of its work for its clients on and with a recent message displayed on the site of this particular software bundle. I think its worth mentioning. Besides Monkey Tooth it seems has taken itself from just a Web Development Company to a Web Development Resource and Company that provides Custom Web Development. (I know, we really don’t have much as far as help goes yet, but I am only one person I can only do so much in my down time, more will come over time). What type of resource would we be if we didn’t mention more things we do to make things come to life on the web….

Well I have known for sometime that Vlad, the creator of A2T had stopped developing this wonderful software bundle that helps even a novice web developer set up a local development server on their own machine of choice using just about all the Windows Platforms (You could also use Apache2Triad as a live server I know I have and do on occasion, but Microsoft windows I don’t care which version it is unless you have a cluster of machines dedicated to the cause just won’t cut it as a live production server).

I have been a faithful user of Apache2Triad for many years now and sometime back I remember seeing the announcement of the possibility of A2T going out of production, then some time later an announcement that confirmed it. Well today I was going out and grabbing a fresh copy from the site like I have time and time again while at a clients office. And I see this new announcement.

Since 2006 Apache2Triad is no longer being developed, there are many reasons behind this but the breaking changes to windows starting with XP have played a significant part, the forums and everything else will remain online.

Since then i have stopped using windows entirely and switched to OSX, the operating system on Mac’s that comes with apache, php, perl, python etc. already built in and updated automatically from Apple, needless to say i never looked back once.

It was a wild ride, thanks to everyone that helped and supported the project, Vlad.

Well now I don’t know about anyone else but I personally take that as a goodbye message. As in maybe when the domain finally expires for apache2triad.net so will the site, the information, possibly even the forums that have helped so many over the years, maybe even the files to obtain the bundle. Well as I see it until the day apache2triad stops working on a Windows system all together and until a day comes that the community that is for A2T stops coming up with solutions for making it work when it doesn’t. Until that day, I don’t want to see apache2triad fade away. It is by far a great bundle of software combined together to work in harmony.

I have seen other WAMP setups out there. I have seen there claims to be the best of the best and easiest to use. Hell I have even tried a couple of them time and time again. But none of them compare to that of A2T.

So what can I do about it? Well to be honest, nothing really I am no guru to apache and its inner workings. I can set it up stand alone, along with all the other applications that come with this bundle. But to make them work as seamlessly as Vlad has, to have them all installed within 15 mins up and ready to be a production server if I really chose to. So with that said I want to keep it alive some how.

My plan is to host the files here, not only will they be easy access for myself when I need them but hopefully from the effort I make to put them on this site will keep the memory alive. I am also dedicating a thread or two on the Monkey Tooth Forums to apache2triad as well, so should the site and forums ever go down people will still have a place to come for help. I personally don’t hope for it to come to that, I hope Vlad keeps the domain and forums up and running but as mentioned earlier I think that the message he has posted up on the site says it clearly, its a goodbye from him to the users.

I will be uploading the files and setting up the extra threads on the forum sometime in the near future. I will post an update or make a new post all together when I do.

August 4, 2010

Clear Form input or text when user clicks on it JavaScript

by Chris — Categories: Coding, Javascript2 Comments

Ever want to clear a text file of its default value when the user clicks on it? Best example of this would be on search boxes or login forms. Where a web developer wants to have the default values of the form fields given be examples of what they are looking for in the form element. Well I have a simple version of that you can implement by adding only a couple lines of code to your existing page.

The Code:

<script type="text/javascript"> 
function clearIt(el) {if (el.defaultValue==el.value) el.value = ""}
</script>

How to use it:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="example.php" method="post" id="example">
<input tabindex="1" type="text" name="elName" id="elName" value="user@exmple.com" onfocus="clearIt(this)">
</form>
<script type="text/javascript">
function clearIt(el) {if (el.defaultValue==el.value) el.value = ""}
</script>
</body>
</html>

Demo:

Coding Help and Support Forums for PHP, mySQL, JavaScript, jQuery and more

by Chris — Categories: Miscellaneous1 Comment

Well today I decided to launch a Coding Help and Support Forums for all mediums of web development help for all you guys who are starting to frequent the site more and more. I know there are dozens if not more forums similar to the idea of this one out there. But this one is different, this one is mine. I just like yourselves know its a valuable asset to anyone coders arsenal to have a feature rich support structure of sorts. Anything from a stock pile of canned code to a community of like minded individualizes who can help one another out. Well between the blog I have here, and the forums, and the various other things I want to do in time I hope to give all of you that and then some for years to come.

That said hopefully this forum will go some where. But I need you guys. Don’t be shy, come post your questions I will try to answer as many as I can regularly and hopefully as the community on the forum grows more people will be able to do the same for each other. Could eventually use some moderators on the board so for those of you who are interested applications so to speak are open for the time being.

Also to follow up on that since this forum at its core is all about giving you guys a safe place to discuss everything and try to get any coding or development issues solved that have you stumped I am all for suggestions just like I am with the blog end of the site. If you guys have ideas to make it better by all means let me know.

Well heres the link to the Coding Help and Support Forums for PHP, mySQL, JavaScript, jQuery and more

August 3, 2010

Finding and Verifying U.S. Dollar amounts with PHP

by Chris — Categories: Coding, PHP — Tags: , 1 Comment

Ok so the idea is you have a user input form. In that form you have your users providing a dollar amount of some sort. But how do you verify this dollar amount is correct. Meaning is it a numeric value, is it in a dollar format, etc.. Well with this little function I hope to help you achieve finding out a little easier that the posted variable is indeed a valid dollar amount or not.

function verifyMoney($what){
   if (preg_match('#^[0-9]+(\.[0-9]{0,2})?$#', $what)){
     $monkey = number_format($what, 2);
     return $monkey;
   }else{ return FALSE; }
}

What this will do is if the dollar amount is not both numeric and in a dollar format (ie: 0.00) it will return false, otherwise it will return what its given.

Demo:
Good: 1,502.38
Bad: 10O.00 came back false.

Example of usage:

$goodmoney = "1502.38";
$badmoney = "10O.00";

if(verifyMoney($goodmoney) !== FALSE) {echo "Good: ".verifyMoney($goodmoney);}else{echo "The Dollar amount you have entered is not valid";}
echo "<br />";

if(verifyMoney($badmoney) == FALSE) {echo "Bad: ".$badmoney." came back false.";}else{echo "The Dollar amount you have entered is valid";}

How to use PHP mail() with HTML and CSS

by Chris — Categories: Coding, PHP — Tags: 1 Comment

The  php mail() function. Such a simple function with so much versatility. Now I know, I know.. trust me I know! There are dozens of tutorials out there on the subject of PHP mail() and its functionality. But how many of them are based off the core concept, and how to use it statically at best with hard coded variables. Example…

$email = "myemail@exmple.com";
$subject4email = "This is a Subject";
$message4email = "Thanks for triggering this email to be sent";
mail($email, $subject4email, $message4email);

How many “php mail” tutorials have you seen like that today while searching for your solution? Chances are they are long winded but up the ally of just giving you the above mentioned code.

Well with this short post I intend on showing you a way many people spend hours searching for to end up sitting on forums eventually waiting for an answer that may never come. PHP’s mail() function is great. But as mentioned above almost all people who give tutorials on the subject show the most core basic concept. Which is fine if you plan to send your self emails in plain text. What I want to show you here is based on the ever growing demand for wanting to send something a bit fancier to your users or even yourself (if your like me, even personal statistics and logs have to look ascetically pleasing even if its only me who will ever see it). So in this post I will give you the core basics of how to code a HTML based template using CSS and standard HTML for use with Email and being sent through PHP. Mind you basic. Im not going to provide you with a fully custom tailored solution. I want to leave it open a bit so you can build on it with ease. This example could be used for example if you have an automated mail to go to new members on your site but its far from limited to the idea of just that.

Full Code:

$mydomain = "somedomain.net";
$username = $selectusers;
$email_who = $selectusersemails;
$titlesubject = $mymessagetitle;
$messagebody = $mymessage;
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
  <head>
<title>' . $titlesubject . '</title>
</head>
<style type="text/css">
<!--
body {background: #edebea;}
#wrapper {background: #fff;border: 4px solid #ddd;}
-->
</style>
</head>
<body><table width="650" id="wrapper">
<tr>
<td>Your Sites Image Logo or slogan or text or link</td>
<td>Dear '.$username.'</td>
<td>'.$messagebody.'</td>
<td>Footer information, copyrights, other..</td>
</tr>
</table>
</body>
</html>
';
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=us-ascii' . "\r\n";
$headers .= 'From: noreply@'. $mydomain . "\r\n";
$headers .= 'Reply-To: noreply@'. $mydomain . "\r\n";
$headers .= '1\r\nX-MSMail-Priority: High' . "\r\n";
$headers .= 'X-Mailer: Monkey Tooth Productions Mailer v1.0.0' . "\r\n";
mail($email_who, $titlesubject, $message, $headers);

Now with the above You can see sending an HTML based email through PHP’s mail() function is not as hard as one would have thought, yet its a pain in the arse to find information on (or was when I last looked some time ago, but then again I still see it asked in forums a lot so either way I suppose).

Now as you can see as I mentioned before this is thee absolute core concept of sending an HTML based email with CSS with PHP

$username = $selectusers;
$email_who = $selectusersemails;
$titlesubject = $mymessagetitle;
$messagebody = $mymessage;

The above four variables can be set to anything and set through any means you choose whether is something through a database query or hard coded in to a combination of the two. However only two of the four above are required with the above mentioned script. Those being $titlesubject and $email_who but you can populate them however you wish. The other two $messagebody and $username are solely for the sake of example within the $message variable.

Now its worth mentioning as you can see I added a variable for the domain $mydomain All though you could use anything its suggested that you keep it specific to your domain the main reason I added this one variable is if you look at the script you will notice that its called a few times within the headers for example. I figured one place to edit it is easier than several. Main reason you want to keep it specific to your domain is well despite what you change it to the email ISP provider will be able to track it back to your domain. So making it something different will only trigger the email provider to throw it in the spam folder. All in all since I mention the spam concept It is possible to have all your emails land directly in the spam box of any give email you send But that will almost always be due to your choice of $messagebody variable in this example. I say this because I have been using this concept for years now with very little feedback of my messages landing in someone spam bin. Mind you I specify mine as I am careful with my messages. I keep them low key with little to no references that can trigger a spam filter into throwing the email into a spam folder.

Prevention Tips try not to use to many links in your emails sent if you have to try to use full urls as you would for the link itself to go to as the links text as well if they match its good use a spell check on your message prior to sending to many misspelled words can be bad to.

Convert a Unix Timestamp to MySQL DATETIME Timestamp with PHP using date()

Well if your like me you like to diversify your usage of timestamps or just like me as well when I first got into coding with PHP I didn’t know much about how to properly mesh PHP with a MySQL Database, and to be quite frank the tutorials out there at the time and even currently (Well the ones with any search engine rankings at least) Still show how us to use a time() based timestamp on many of our elements. However a couple key issues with that now a days is if your going to make any site about anything with any form of user end input, feedback, or otherwise your going to eventually need to use a database for it. Or a part there of at the very least, that said if your using older timestamps like the time() function generates then you already know the problem. Actually you must know if your skimming through this post. Now where the time() function seems like the best bet as its the easiest to do math with for things like session time outs, its not typically prime for so many of mySQL’s features for date ranges and other various time/date functions within mySQL that make handling sessions and so much more easier now a days. So here it is, my simple little solution

function unixToMySQL($timestamp){
return date('Y-m-d H:i:s', $timestamp);
}

Unix Timestamp Example: 1283666682
Above Timestamp converted to MySQL DATETIME: 2010-09-05 06:04:42

I use it as a function just out of my own practices to call things from a global file that contain functions used widely through out sites I develop. This function can also be developed upon heavily to do various other conversions but I figured I would give you what I found most people look for specifically. Myself included at one time. The live example above is equivilant to a MySQL DATETIME date using NOW()

Example of usage:

$blah = time();
echo unixToMySQL($blah);

© 2010 Monkey Tooth Productions All rights reserved - Wallow theme by TwoBeers Crew - Powered by WordPress - Have fun!