Saturday, April 2, 2011

Mail Attachment with PHP

Mail attachments are tricky with php are tricky. After hours of research, and allot of trial and error I finally learned how to do mail attachments.

Example:



$to =     'ilive72@hotmail.com';
$subject =     'PHP Mail Attachment Test';
$bound_text =     "jimmyP123";
$bound =     "--".$bound_text."\r\n";
$bound_last =     "--".$bound_text."--\r\n";
     
$headers =     "From: admin@server.com\r\n";
$headers .=     "MIME-Version: 1.0\r\n"
      ."Content-Type: multipart/mixed; boundary=\"$bound_text\"";
     
$message .=     "If you can see this MIME than your client doesn't accept MIME types!\r\n"
      .$bound;
     
$message .=     "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
      ."Content-Transfer-Encoding: 7bit\r\n\r\n"
      ."hey my good friend here is a picture of regal beagle\r\n"
      .$bound;
     
$fileatt = "./008.jpg";
$file = fopen($fileatt,'rb');
$data = fread($file, filesize($fileatt));
   
//$file =     file_get_contents("http://internetinfoanddesign.com/webdevelopment/brockconnect/admin/008.jpg");

//When adding the attachment, you must include a slash n escape
     
$message .=     "Content-Type: image/jpg; name=\"008.jpg\"\n"
      ."Content-Transfer-Encoding: base64\n"
      ."Content-disposition: attachment\n; file=\"008.jpg\"\n"
      ."\n\n"
      .chunk_split(base64_encode($data))
    //.chunk_split(base64_encode($file))
      .$bound_last;

if(mail($to, $subject, $message, $headers))
{
     echo 'MAIL SENT';
} else {
     echo 'MAIL FAILED';
}

?>

Thursday, March 31, 2011

The Onload Event Handler


Take a look at the on load event on the body tag of your document. It will run everything on the page, after the page has finished loading, including table elements, dividers, images, scripts, style sheets, multimedia files, etcetera.

Often it is necessary to perform some action on the page, for example, filling a form with information from a database table, displaying a list of information, or getting a value from a table for future reference.

Example:
In the on load event you can write; on load=javascript:getvalue();

You can program multiple events simply by adding semicolons.

Example:

If you need to program multiple events you can specify the functions in your onload event as follows;
onload=javascript:getvalue();javascript:displaylist()

The on load event handler becomes very useful when displaying dynamic information to different parts of your page. It is good to become familiar with this event, and use it to your advantage when designing your content.

Checking for the Existence of an Object

When using hidden fields, or generic form fields, as part of best practice it is advisable to check for the existence of the object on the page. 

Example

var Element = document.getElementById("page");

if (Element != null){ // Check object exists
                //Do something
}

Checking for the object on the page enables it to be reusable. It does not need to be on the given page your working with, to be part of your code.

Sunday, March 27, 2011

Get Parameter

This is the most important function you will every need in your online endeavors. It is called getparm. You use it by calling it into a variable.

Example:

var recordnumber=getparm('parm'); [be sure your parameter match's what was passed]


Here is the function to get the parameter;

function getparm(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
 

I found this function posted on the internet while working on a project. Similar solutions are posted all over the internet. Over the years, I have learned that if you need a solution, let your fingers do the walking. If you can't find what your looking for WALK AWAY AND THEN COME BACK, works every time.


Additional Resources:


http://www.netlobo.com/url_query_string_javascript.html







Programming for the Web