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';
}
?>
In this blog I hope to provide information to aspiring web designers, and web developers looking for answers to technical problems. It also serves as an archive of solutions I have found, for myself.
Saturday, April 2, 2011
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
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
Saturday, March 26, 2011
Multiple Requests with Ajax
The Ajax programing technique can only be used to make one request at a time: however, it is possible to get more data using the java script split command.
Returning only one piece of information at a time is very limiting, and impractical. Many times you need to return more than one item to a web page. While maintaining and adding to a commercial website, I needed to find a solution.
The solution was not easy to find. It took a while to wrap my head around the concept, but I eventually figured it out.
Here it is. In your statechange function under readystate 4, after,
document.getElementById("data").innerHTML=xmlhttp.responseText;,
add the following line;
//The message is split wherever [BRK] is part of the message and stored in an array.
var result=document.getElementById("portdata").innerHTML.split("[BRK]");.
If you are using PHP for your server side script, your last line should include the following (this is only an example, but your code should follow the same format);
$variable="George Thomson";
echo "Customer_Name".[BRK].$variable.[BRK];.
In readystate 4, evaluate the data and assign it.
Example:
if (result[0]=="Customer_Name"){
document.getElementById("msg").innerHTML=result[1];
}
Remember that the index number for an array always starts at zero.
Additional resources;
http://www.w3schools.com/JS/js_obj_array.asp
Returning only one piece of information at a time is very limiting, and impractical. Many times you need to return more than one item to a web page. While maintaining and adding to a commercial website, I needed to find a solution.
The solution was not easy to find. It took a while to wrap my head around the concept, but I eventually figured it out.
Here it is. In your statechange function under readystate 4, after,
document.getElementById("data").innerHTML=xmlhttp.responseText;,
add the following line;
//The message is split wherever [BRK] is part of the message and stored in an array.
var result=document.getElementById("portdata").innerHTML.split("[BRK]");.
If you are using PHP for your server side script, your last line should include the following (this is only an example, but your code should follow the same format);
$variable="George Thomson";
echo "Customer_Name".[BRK].$variable.[BRK];.
In readystate 4, evaluate the data and assign it.
Example:
if (result[0]=="Customer_Name"){
document.getElementById("msg").innerHTML=result[1];
}
Remember that the index number for an array always starts at zero.
Additional resources;
http://www.w3schools.com/JS/js_obj_array.asp
Friday, March 25, 2011
Ajax Tutorial
Ajax stands for Asynchronous JavaScript And XML. Once you learn how to use it properly, you will never go back to posting data to a PHP or ASP script.
Since I don't have alot of time to go into detail, I will provide the basics, you need to get started.
Step1
Create a javascript file and make a link to it in your html page after your title tag. If you have a style sheet, put it after the style sheet reference.
Example:
Step2
Add the following lines to the top of your javascript file;
var xmlhttp
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.5.0");
Step3
Add the following functions. These functions are mandatory. They tell the browser what to do when the data is received.
function stateChanged()
{
if (xmlhttp.readyState==1) //Do while page is loading.
{
document.getElementById("Wait").style.visibility="visible"; Make span id visible.
document.getElementById("Wait").src="ajax-loader.gif";
document.getElementById("lblwait").innerHTML="Processing.....";
}
if (xmlhttp.readyState==4) //Do when complete.
{
document.getElementById("Wait").style.visibility="hidden"; //Set wait status to hidden.
document.getElementById("lblwait").innerHTML=""; //Clear message
document.getElementById("portdata").innerHTML=xmlhttp.responseText; //Get data
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
Step4
Add function to execute server side script, like php or asp.
Example;
function getinfo(parm){
var recordnumber=parm; //get record number passed in
;
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="../inc/getinfo.php";
url = url+"?recordnumber=" + recordnumber;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
Call your function getinfo from an onclick or load event from the page.
Example: onclick=javascript:getinfo();
Due to the nature of Ajax, only one piece of data is returned at a time, which makes it very difficult to do anything useful; however, there is a trick to getting more than one piece of data at a time. Stay tuned for additional blogs.
Here are some additional resources you may find useful;
http://www.tizag.com/ajaxTutorial
http://www.w3schools.com/ajax/default.asp
Since I don't have alot of time to go into detail, I will provide the basics, you need to get started.
Step1
Create a javascript file and make a link to it in your html page after your title tag. If you have a style sheet, put it after the style sheet reference.
Example:
Step2
Add the following lines to the top of your javascript file;
var xmlhttp
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.5.0");
Step3
Add the following functions. These functions are mandatory. They tell the browser what to do when the data is received.
function stateChanged()
{
if (xmlhttp.readyState==1) //Do while page is loading.
{
document.getElementById("Wait").style.visibility="visible"; Make span id visible.
document.getElementById("Wait").src="ajax-loader.gif";
document.getElementById("lblwait").innerHTML="Processing.....";
}
if (xmlhttp.readyState==4) //Do when complete.
{
document.getElementById("Wait").style.visibility="hidden"; //Set wait status to hidden.
document.getElementById("lblwait").innerHTML=""; //Clear message
document.getElementById("portdata").innerHTML=xmlhttp.responseText; //Get data
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
Step4
Add function to execute server side script, like php or asp.
Example;
function getinfo(parm){
var recordnumber=parm; //get record number passed in
;
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="../inc/getinfo.php";
url = url+"?recordnumber=" + recordnumber;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
Call your function getinfo from an onclick or load event from the page.
Example: onclick=javascript:getinfo();
Due to the nature of Ajax, only one piece of data is returned at a time, which makes it very difficult to do anything useful; however, there is a trick to getting more than one piece of data at a time. Stay tuned for additional blogs.
Here are some additional resources you may find useful;
http://www.tizag.com/ajaxTutorial
http://www.w3schools.com/ajax/default.asp
Subscribe to:
Posts (Atom)