Showing posts with label tutorials. Show all posts
Showing posts with label tutorials. Show all posts

Monday, July 15, 2013

how to select all options in multi select dropdown using jquery


When we use multi select drop down, we may consider the feature for selecting all and deselect all option for the respective Multi Select box.

Using jQuery, we can easily do the select all and deselect all option for the dropdown.

Below is the code to select all using jQuery

HTML
<select name="education" id="education" multiple="multiple">
       <option value="1">Under Graduate</option>
       <option value="2">Graduate</option>
       <option value="3">Post Graduate</option>
       <option value="4">Pursuing Graduation</option>  
</select>

$('#education').find('option').attr('selected','selected');

Where 'education' is the id for the multi select & we are adding the attribute selected to get selected.

To deselect all using jQuery.

$('#education').find('option').removeAttr('selected','selected');

Saturday, October 16, 2010

Sending Mails with Attachment in PHP

Here is the code for sending for mails with Attachments in PHP.

$email_from = "Anil Kumar"; // Who the email is from
$email_subject = "Email with Attachment"; // The Subject of the email
$email_message = "Is the File Attached."; // Message that the email has in it

$email_to = "anilbuddha@gmail.com"; // Who the email is too

$headers = "From: ".$email_from;


$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";

/********************************************** First File ********************************************/


$fileatt = "../reports/users.pdf"; // Path to the file
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = "users"; // Filename that will be used for the file as the attachment

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);


$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}\n";
unset($data);
unset($file);
unset($fileatt);
unset($fileatt_type);
unset($fileatt_name);
$ok = mail($email_to, $email_subject, $email_message, $headers);

if($ok) {
echo "Email With Attachment has been Sent Successfully";
} else {
echo "Sending Email with Attachment has Failed. Please check the Code Added";
}














Friday, August 20, 2010

Function for taking MySQL Backup in PHP

Here is the function for taking backup of a MySQL database without any external tool for MySQL.

$host ---> Hostname
$user ----> Username
$pass ----> Password
$name ----> Database name
If we need the total database, just put the value of $tables as * or else pass only the tables you want with comma separated.

function backup_tables($host,$user,$pass,$name,$tables = '*')
{

 $link = mysql_connect($host,$user,$pass);
 mysql_select_db($name,$link);

 //get all of the tables
 if($tables == '*')
 {
 $tables = array();
 $result = mysql_query('SHOW TABLES');
 while($row = mysql_fetch_row($result))
 {
 $tables[] = $row[0];
 }
 }
 else
 {
 $tables = is_array($tables) ? $tables : explode(',',$tables);
 }

 //cycle through
 foreach($tables as $table)
 {
 $result = mysql_query('SELECT * FROM '.$table);
 $num_fields = mysql_num_fields($result);

 //$return.= 'DROP TABLE '.$table.';';
 $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
 $return.= "\n\n".$row2[1].";\n\n";

 for ($i = 0; $i < $num_fields; $i++)
 {
 while($row = mysql_fetch_row($result))
 {
 $return.= 'INSERT INTO '.$table.' VALUES(';
 for($j=0; $j<$num_fields; $j++)
 {
 $row[$j] = addslashes($row[$j]);
 $row[$j] = ereg_replace("\n","\\n",$row[$j]);
 if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
 if ($j<($num_fields-1)) { $return.= ','; }
 }
 $return.= ");\n";
 }
 }
 $return.="\n\n\n";
 }
$s_file = 'db-backup-'.time().'-'.'.sql';
 //save file
 $handle = fopen($s_file,'w+');
 fwrite($handle,$return);
 fclose($handle);
 header("Location:".$s_file);
 exit;
}

Monday, August 16, 2010

Download PHP Manual

PHP is the powerful server side scripting language, popularly known as Hypertext Preprocessor.

PHP is also called as Personal Home Page.

Download the PHP Manual here. For Free Download, Click Here

Friday, August 13, 2010

Download MySQL Tutorial

Here is PDF document for MySQL Tutorial...

MySQL is the world's most popular open source database because of its fast performance, high reliability, ease of use, and dramatic cost savings. Want to learn MySQL, download the pdf document & enhance your database skills..

Free Download, Click Here

Monday, August 9, 2010

Post your Status in Twitter using PHP

t$twitter_api_url = "http://twitter.com/statuses/update.xml";
$twitter_data = "status=Visit http://anilkajadoo.blogspot.com/ for PHP tips and tutorials!";
$twitter_user = "your_user_name";
$twitter_password = "your_password";

The $twitter_data variable contains the message to be posted to Twitter, which you must ensure is 140 characters or less before posting. You could use strlen() function to do so when you ready the data to be sent, and truncate it if it’s too long by using substr() function. The status= before the message is the POST variable, the parameter for the API.


To send the request, we will use cURL, which is included on most servers. First we initiate cURL and pass it the URL we will be sending the request to:

$ch = curl_init($twitter_api_url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $twitter_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "{$twitter_user}:{$twitter_password}");//Twitter Username & Password are used to post the status


$twitter_data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

All that’s left really is to see whether it worked or not. Since we have our handy $httpcode variable, we can just run a conditional statement to see if the response code is equal to 200 (which means everything went okay).

if ($httpcode != 200) {
     echo "The tweet wasn't posted correctly. ";
}

         

Monday, August 2, 2010

Get the Page title of a webpage using PHP

Below is the function for getting the Title of the webpage using PHP

function getMetaTitle($content){
$pattern = "|<[\s]*title[\s]*>([^<]+)<[\s]*/[\s]*title[\s]*>|Ui";
if(preg_match($pattern, $content, $match))
return $match[1];
else
return false;
}

$url = $siteurl; //Enter the Site URL which we want to get the Page Title
$content = file_get_contents($url);
$title = getMetaTitle($content);
$tags = get_meta_tags($siteurl); // To get the meta content, description, keywords of the given URL....


$tags is a array of all meta data
To get the Meta Keywords, Description we will use the get_meta_tags which is a predefined function in PHP. To retrieve the values we need to use $tags['keywords'] for Keywords & for Description $tag['description']