Friday, October 24, 2014

jQuery Modify dropdown menu and default value

1) Call latest jQuery library
2) JS Associate Array, data may come from ajax post
     '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>
<script src=""></script>
table {
    border-collapse: collapse;
table, th, td {
    border: 1px solid black;
function myFunction() {
   var taList = {};
     '1': {id:1, name:'Andy'},
     '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');

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

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:
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
3) Example 3, deny all access
<Files "*">
    Order deny,allow
    Deny from all
You will get
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
AuthType Basic
AuthName "Restricted access"
AuthUserFile  "/var/www/html/htaccess_test/.htpasswd"
Require valid-user
<Files ~ "^\.ht">
  Order allow,deny
  Deny from all

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
    echo '<h1> This is  a test for cron job</h1>';

 2) Create a shell script
#! /bin/sh
#wget -q --no-check-certificate -O- ""
php test.php
3) run following in shell to make executable
chmod +x
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
<Files "*">
    Order deny, allow
    Deny from all

5) crontab -e
30 23 * * * /home/jiansenl/crontest/
each night 23:30pm to  run
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
I download and use winrar to unzip and put it in

2)Configuration php.ini to enable openssl
In C:\xampp\php\php.ini
 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.
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->SMTPDebug  = 2;
$mail->From = "";
$mail->FromName = "Jiansen";
$mail->Host = ""; // 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 = ""; // SMTP username
$mail->Password = "mypassword"; // SMTP password

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

$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
We got
SMTP -> FROM SERVER:220 ESMTP uf6sm10363353pac.16 - gsmtp
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:
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

    $message = "The mail message was sent with the following mail";
    $headers = "From:";
    mail("", "Testing", $message, $headers);
    echo "Test message is sent to<BR/>";
Change 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. 

For Linux, refer to:

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:

1) For Windows, run

default location

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:
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
$  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
$ 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