Friday, October 24, 2014

jQuery Modify dropdown menu and default value




1) Call latest jQuery library
http://code.jquery.com/jquery-latest.min.js
2) JS Associate Array, data may come from ajax post
  t={
     '1': {id:1, name:'Andy'},
     '2': {id:2, name:'Jiansen'},
     '3': {id:3, name:'Tommy'}
     };

  3) Put t in dropdown menu and set default value 2
4) Able not to duplicate id    
5) first we have a empty selection dropdown
 <select id="mySelect"><option value=" ">=No TA Assigned=</option></select>
6) Then we have JS function  myFunction() to deal with onclick to add items to dropdown, to append item in dropdown list
row.find('td.Ta select').append('<option value="'+taList[id0].id+'">'+taList[id0].name+'</option>');
7) We need to prevent duplication of items  dropdown, also we can set the default value
   row.find('td.Ta select').val('2');
Complete Code:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<style>
table {
    border-collapse: collapse;
}
table, th, td {
    border: 1px solid black;
}
</style>
<script>
function myFunction() {
   var taList = {};
   taList={
     '1': {id:1, name:'Andy'},
     '2':{id:2,name:'Jiansen'},
     '3': {id:3, name:'Tommy'} };
   var row = $('table.template tr.template');
//add other options
    for (var id0 in taList){
        var x = document.getElementById("mySelect");
        var i, add='y';
        //prevent duplication           
        for (i = 0; i < x.length; i++) {
        
 if(t[j].id ==x.options[i].value) add='y' ;
        }   
           if(add=='y')     row.find('td.Ta select').append('<option value="'+taList[id0].id+'">'+taList[id0].name+'</option>');          
      }
//Set default value, for example Andy
    row.find('td.Ta select').val('2');
}
</script>

<body>
<h1>Add content in dropdown menu and set its default value in jQuery</h1>
 <table class='template' style="border:1" >
<tr>
   <th>Courses</th><th>Pick TA</th>
<tr>
<tr class='template'>
 <td>Physics</td>   <td class='Ta'><select id="mySelect"><option value=" ">=No TA Assigned=</option></select></td>
</tr>
</table>
<button onclick="myFunction()">Click me to add TAs TA dropdown list</button>
<p id="demo"></p>
</body>
</html>

Video: jQuery  Modify dropdown menu and default value
Note: in video, 
       if(taList[id0].id ==i) add='n'
should be changed to
 if(t[j].id ==x.options[i].value) add='y' ;

Sunday, October 12, 2014

Examples of .htaccess - web directory password protected




.htaccess file is used to control the directory access
in Apache web server. Comparing to httpd.conf, 
httpd.conf is global settings for apache.
.htaccess can be applied per folder, overwrite http.conf 
1) Example 1 of .htaccess, prevent directory list
Options -Indexes
You will get message:
"Forbidden
You don't have permission to access /htaccess_test/ on this server."
2) Example 2, will not display .txt file
Options +Indexes
IndexIgnore *.txt

You can not see txt in list, but you still can access, to disable access to txt
<Files "*.txt">
    Order deny,allow
    Deny from all
</Files>
3) Example 3, deny all access
<Files "*">
    Order deny,allow
    Deny from all
</Files>
You will get
"Forbidden
You don't have permission to access /htaccess_test/ on this server."
Remember, no space between deny and allow, or you will get
error message
"Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, root@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log."
4)For password protection
 Create encrypted password  for user name jiansen for
 htpasswd -c /var/www/html/htaccess_test/.htpasswd jiansen
File permission:
chmod 644 /var/www/html/htaccess_test/.htpasswd
Make sure it can be read by apache, but can not read by outside
.htaccess: 
AuthType Basic
AuthName "Restricted access"
AuthUserFile  "/var/www/html/htaccess_test/.htpasswd"
Require valid-user
<Files ~ "^\.ht">
  Order allow,deny
  Deny from all
</Files>

Video: Some examples of .htaccess

Saturday, October 11, 2014

Using Cron Job to process PHP scripts




Using Cron Job to process PHP scripts.
For example, we have test.php and want to run it  every day using cron job.
1) test.php
<?php
    echo '<h1> This is  a test for cron job</h1>';
?>

 2) Create a shell script test.sh
#! /bin/sh
#wget -q --no-check-certificate -O- "http://swf.site40.net/crontest/test.php"
php test.php
3) run following in shell to make test.sh executable
chmod +x test.sh
4)  The reason we want to use php instead of wget is that we do want outside people access test.php
Set up .htaccess to Deny from all in the directory of test.sh
<Files "*">
    Order deny, allow
    Deny from all
</Files>

5) crontab -e
30 23 * * * /home/jiansenl/crontest/test.sh
each night 23:30pm to  run test.sh
6) crontab -l
list cron jobs.
Video: Using Cron Job to process PHP scripts

Sunday, October 5, 2014

Install PHPMailer 5.2.4 and use smtp gmail




1) PHPMailer download
https://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list
I download PHPMailer_5.2.4.zip and use winrar to unzip and put it in
C:\xampp\htdocs\phpmailertest

2)Configuration php.ini to enable openssl
In C:\xampp\php\php.ini
extension=php_openssl.dll
 If not, you may get the following message:
"SMTP -> ERROR: Failed to connect to server:
 Unable to find the socket transport "ssl" -
 did you forget to enable it when you configured PHP? (1909703479) "

3) Restart Apache

4) Create index.php under  C:\xampp\htdocs\phpmailertest. The red code below is import for using gmail as SMTP.  Change jiansentest and mypassword to your gmail account and password.
<?php
error_reporting(E_ALL);
require("PHPMailer_5.2.4/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->SMTPDebug  = 2;
$mail->From = "jiansentest@gmail.com";
$mail->FromName = "Jiansen";
$mail->Host = "smtp.gmail.com"; // specif smtp server
$mail->SMTPSecure= "ssl"; // Used instead of TLS when only POP mail is selected
$mail->Port = 465; // Used instead of 587 when only POP mail is selected
$mail->SMTPAuth = true;
$mail->Username = "jiansentest@gmail.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password

$mail->AddAddress("jiansenreceive@gmail.com", "Jiansen"); //replace myname and mypassword to yours
$mail->AddReplyTo("jiansentest@gmail.com", "Jiansen");
$mail->WordWrap = 50; // set word wrap
//$mail->AddAttachment("c:\\temp\\js-bak.sql"); // add attachments
//$mail->AddAttachment("c:/temp/11-10-00.zip");

$mail->IsHTML(true); // set email format to HTML
$mail->Subject = 'test';
$mail->Body = 'test';

if($mail->Send()) {echo "Send mail successfully";}
else {echo "Send mail fail";}

?>
5) Run
 http://localhost/phpmailertest/
We got
SMTP -> FROM SERVER:220 mx.google.com ESMTP uf6sm10363353pac.16 - gsmtp
SMTP -> FROM SERVER: 250-mx.google.com at your service, [207.81.4.41] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8
SMTP -> FROM SERVER:250 2.1.0 OK uf6sm10363353pac.16 - gsmtp
SMTP -> FROM SERVER:250 2.1.5 OK uf6sm10363353pac.16 - gsmtp
SMTP -> FROM SERVER:354 Go ahead uf6sm10363353pac.16 - gsmtp
SMTP -> FROM SERVER:250 2.0.0 OK 1412491179 uf6sm10363353pac.16 - gsmtp
SMTP -> FROM SERVER:221 2.0.0 closing connection uf6sm10363353pac.16 - gsmtp
Send mail successfully

Video: Install PHPMailer 5.2.4 and use smtp gmail 

Thursday, October 2, 2014

PHP mail function using Gmail account in Windows XAMPP




1) When we test php mail code in  Windows XAMPP, we do  not have smtp server in localhost. Windows XAMPP has sendmail.exe, but do not have PHPmailer. The following setting will make mail function working in PHP in XAMPP.
2) For example, you install XAMPP in C:\xampp
in C:\xampp\sendmail\sendmail.ini:
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=****@gmail.com
auth_password=*******
force_sender=****@gmail.com
in C:\xampp\php\php.ini:
[mail function]
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
 
3) Replace  **** to your gmail account name and password.
Restart Apace in XAMPP.
 
4) testmail.php to test if mail function is working
 <?php

    $message = "The mail message was sent with the following mail";
    $headers = "From: youremail@gmail.com";
    mail("youremail@gmail.com", "Testing", $message, $headers);
    echo "Test message is sent to youremail@gmail.com....<BR/>";
   
?>
Change youremail@gmail.com to your email account. You will receive an email. 
Video: PHP mail function using Gmail account in Windows XAMPP
 

5) PHPmailer is another option to send email in PHP. 
reference:
http://jiansenlu.blogspot.ca/2011/08/php-send-email-using-phpmailer-and.html

For Linux, refer to:
https://www.digitalocean.com/community/tutorials/how-to-use-gmail-or-yahoo-with-php-mail-function

Wednesday, October 1, 2014

Install Tex Live in Windows and Linux, Add Liberation Fonts



TeX Live is a free software distribution for the TeX typesetting system that includes major TeX-related programs, macro packages, and fonts. Textlive can be installed in Windows or Linux. (Around 3GB)
Texlive can be downloaded from:
https://www.tug.org/texlive/acquire-netinstall.html

1) For Windows, run
install-tl-windows.exe

default location
C:\texlive\2014

2)  Add new font, such as Liberation Fonts
If you want to add extra ttf  fonts, such as The Liberation(tm) Fonts, which can be downloaded from:
https://fedorahosted.org/liberation-fonts/
under C:\texlive\2014\texmf-dist\fonts\truetype\public
create directory liberation
copy all ttf liberation font files to liberation  directory.

For Linux, create directory liberation under /usr/share/fonts/
copy all ttf liberation font file to /usr/share/fonts/liberation

3)  Installation for Redhat Linux
 $  wget http://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz
$  tar xf install-tl-unx.tar.gz
$  cd 'directory where file extracted'
$  sudo ./intall-tl
$  O
$  L
  
        Choose the following:
    (1)   New value for binary directory [/usr/local/bin]: /usr/bin
    (Type /usr/bin.)
    (2)   New value for man directory [/usr/man]:
    (Do not type anything.)
    (3)   New value for info directory [/usr/info]:
    (Do not type anything.)
$  R
$  I
You may also need to install PDFtk:
$ wget http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk-2.02-1.el6.x86_64.rpm
$ sudo yum install jre-gcj
$ sudo rpm -Uvh pdftk-2.02-1.el6.x86_64.rpm

Video:  Install Tex Live in Windows and Linux, Add Liberation Fonts