Tuesday, February 9, 2010

PHP Source code for comment box in unicode language(like malayalam, tamil and arabic)

        A comment box is a must have feature in many of the web based aplications. Users should able to save their comments on their own language. Many of the news papers are allowing their visitors to express their views about the articles/news published. Comment enabled web pages will increase user engagement, hence more income from advertisement venders can expect.
Making a comment box is nothing if you are using PHP and MySQL in most scenario.

1. Declare a Rich text box using HTML Tags.
2. On submit, collect the inserted values and save it to mySQL database, thats it.

Though, saving your text in mySQL database will be difficult if you are playing with Unicode charactors.  I know many of my friends are complaining about that. They were tried to save malayalam unicode characters and, while retrieving the saved text they got a bunch of question marks. 
They saved " ചേന്ദമംഗല്ലൂര്‍ നല്ലൊരു നാടാണല്ലൊ ... ഹി ഹി ഹി "
They recieved " ????????????????????????????  ??? "
Its because, unicode charaters are a unique special numercial codes for different languages. MySQL may not decode it to proper language representation unless you are explicity told to him. I have a solution for this problem.
1. Use SET NAMES 'utf8' before database transaction started.
2. Convert your table collation settings to  'utf8_general_ci'

visit my old post for more details about this .

Hope you understand the technique behind the unicode operations.  Now we can move into the coding part of the comment box. I have to PHP funcitons, which you can use for making a comment box in your web based aplication.

   1:  function commentbox($dcName,$docId,$act_page,$SecurePath="../../lib/",$security="secured")

   2:  {


   4:      echo("<form name='"."inputfrm"."' enctype='"."multipart/form-data"."' method='"."post"."' action='".$act_page."?mod=".$security. "' onSubmit='"."return ValidateForm()"."'>");

   5:      echo("<fieldset>");


   7:      echo("<textarea name='"."ta"."' rows='"."10"."' cols='"."50"."'></textarea>");

   8:      echo("<br>");

   9:      echo("Doc ID:"); 

  10:      echo("<input name='"."docId"."' type='"."text"."' value='".$docId."' size='"."5"."'  readonly='"."true"."'>");

  11:      echo("<br><br>");

  12:      echo("Name &nbsp;:"); 

  13:      echo("<input name='"."txtName"."' type='"."text"."' size='"."25"."'>");

  14:      echo("<font face='"."Verdana, Arial, Helvetica, sans-serif"."'>* (Mandatory)</font>");

  15:      echo("<br><br>");

  16:      echo("Email &nbsp;: ");

  17:      echo("<input name='"."txtEmail"."' type='"."text"."' size='"."50"."'>");


  19:      if($security=="secured")

  20:      {


  22:          echo("<table width='"."100%"."' border='"."0"."' cellspacing='"."0"."' cellpadding='"."0"."'><tr>");

  23:          echo("<td width='"."10%"."'>");

  24:          echo("<td width='"."80%"."'>");

  25:          //pass a session id to the query string of the script to prevent ie caching

  26:          echo("<br>");

  27:          echo("<img src='".$SecurePath."securimage_show.php?sid=".md5(uniqid(time()))."'><br/>");


  29:  //        echo("<img src='".$SecurePath."securimage_show.php><br />");

  30:          echo("<font color='"."#ff0000"."'>");

  31:          echo("Enter the string displayed above<br>");

  32:          echo("</font>");

  33:          echo("<input type='"."text"."' name='"."securityCode"."' /><br />");

  34:          echo("</td>");


  36:          echo("<td width='"."10%"."'>");

  37:          echo("</tr></table>");

  38:      }


  40:      echo("</p>");

  41:      echo("<p align='"."right"."'><input type='"."submit"."' value='"."Save Comment"."' /></p>");

  42:      echo("</fieldset>");

  43:      echo("</form>");

  44:  }

function insertComment($docName,$DocId,$uText,$Uname,$TxtEmail,$ipAdr=0)
include 'connect.php';
// echo($pageURL);
// $ipAdr ='';
$time = time(now);
$phptime=date('Y-m-d H:i:s', $time);
@mysql_query("SET NAMES 'utf8'",$id); /// Its very inportant to display or insert unicode characters in db
$unicodeText = preg_replace('#\r?\n#', '<br />', $uText);
$cQryPart="insert into document (docName, DocID, Comments, aproved, u_name, email,CDate,isSuspects,extra,extra2) values ";
$cQry=$cQryPart."('".$docName."',".$DocId.",'".$unicodeText."',".$aproved.",'".$Uname."','".$TxtEmail."','".$phptime."','".$isSuspects."','".$ipAdr."','".$pageURL."')" ;
// echo($cQry);
$cresult = @mysql_query($cQry,$id) or
die("Unable to Execute the query");