Friday, March 27, 2015

Clearing textbox after selecting item form JQuery autocomplete

If you need to clear textbox after selecting an item from JQuery AutoComplete list, all you have to do is clear the text box and return false from the select event.


$("#product").autocomplete({
source: "url",
minLength: 2,
select: function( event, ui ) {
$(this).val("");
return false;
});

Friday, February 11, 2011

Jquery Uploadify: Rename Uploaded File

I have been searching for two days to find a solution which will let me upload employee image and rename it, I couldn't find any. Turns out it's pretty simple, I have to just pass the filename as query string and do a little modification in uploadify.php file.
  1. Passing the filename as part of the query string - followed by '?' mark include the filename with the paramater 'script'
    'script' : 'uploadify/uploadify.php?name=imagename'
    //don't worry about the extension
  2. Modify the following line
    $targetFile =  str_replace('//','/',$targetPath) . 
    $_FILES['Filedata']['name'];

    with this
    if(isset($_GET['name'])){
    $targetFile =  str_replace('//','/',$targetPath) . $_GET['name']. 
            substr($_FILES['Filedata']['name'],-4);
     } else{
        $targetFile =  str_replace('//','/',$targetPath) .
          $_FILES['Filedata']['name'];
     }
I hope this is helpful

Tuesday, November 2, 2010

PHP Function: Include file if exists


function includeifexists($filename, $dir = ".", $level = 0, $found = false){
if (!$found){
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (filetype($dir."/".$file)=="dir") {
if($file!="." && $file!=".."){
if($file=="." || $file==".."){
} else{
$found = includeifexists($filename, $dir."/".$file, $level+1, $found);
}
}
}
else {
if($file==$filename){
include($dir."/".$file);
$found = true;
return $found;
}
}
}
closedir($dh);
}
}
}
return $found;
}

Thursday, April 9, 2009

Connect to MS SQL database from C#

Step 1: Set connection string
string connectionString = "user id=" + your_user_id + ";" +
"password=" + your_password + ";" +
"server=" + server + ";" +
"Trusted_Connection=" + "yes" + ";" +
"database=" + database_name + "; " +
"connection timeout=" + "30" ;

'''the server name is your comuputer name\\SQL Server name
'''e.g. abccomputer\\SQLEXPRESS


Step 2: Connect using the connection string and open the connection
SqlConnection connection;
connection = new SqlConnection(connectionString);
connection.Open();


Step 3: Execute SqlCommand and Read output
string query = "SELECT * FROM Table1;";
SqlCommand sqlCommand1 = new SqlCommand(query, connection);
SqlDataReader dataReader = mSqlCommand.ExecuteReader();

Step 4: Read the output
string result = "";while (dataReader.Read())
{
result += mSqlDataReader["column1"];
result += mSqlDataReader["column2"];
result += mSqlDataReader["column3"];
result += Environment.NewLine;
}
MessageBox.Show(result);


Step 5: Close the connection

connection.Close();

Don't forget to add this line "using System.Data.SqlClient;" at the top of your program

Sunday, February 8, 2009

Connecting to MySQL Database in 5 Simple steps using php


\<\?php
//Step 1: connecting to database server
$connection = mysql_connect("localhost","root","");
//("server","user","password")


//Step 2: selecting a database
$sel_database = mysql_select_db("database_name"); //("name of the database that you want to connect to")


//Setp 3: doing the query
$query = "SELECT * FROM table1";
$result = mysql_query($query,$connection);


//Step 4: presenting retrieved data
while ($row = mysql_fetch_array($result)){
foreach($row as $value){
echo $value."\t";
}
echo "
";
}


//Step 5: closing the connection
mysql_close($connection);
?>

Thursday, February 5, 2009

Some useful PHP function (RegEx):

  1. Email address validation:

    function isEmail($email){
    $regex = "/\w+@\w+.\w+/";
    return preg_match($regex, $email);
    }


  2. URL validation:

    function isURL($url){
    $regex = "/(https?ftpfile):\/\/.\w+.\w\w+/";
    return preg_match($regex, $url);
    }


  3. Validate IP:

    function isIp($ip){
    $block = "(25[012345]2[01234]\d[01]?\d\d?)";
    $regex = "/^($block\.$block\.$block\.$block)$/";
    return preg_match($regex, $ip);
    }


  4. Is MAC address:

    function isMac($mac, $separator){
    $mac = strtoupper($mac);
    $block = "([A-F][A-F][A-F][0-9][0-9][A-F][0-9][0-9])";
    $regex = "/^($block$separator$block$separator$block$separator$block$separator$block$separator$block)$/";
    return preg_match($regex, $mac);
    }


  5. Is this a private IP:

    function isPrivateIp($ip){
    // 10.0.0.0 – 10.255.255.255
    // 172.16.0.0 – 172.31.255.255
    // 192.168.0.0 – 192.168.255.255
    $block = "(25[012345]2[01234]\d[01]?\d\d?)";
    $rangeA = "/^(10)\.($block)\.($block)\.($block)$/";
    $rangeB = "/^(172)\.(3[01]2[0-9]1[6-9])\.($block)\.($block)$/";
    $rangeC = "/^(192)\.(168)\.($block)\.($block)$/";
    $regex = "/^($block\.$block\.$block\.$block)$/";
    if (preg_match($rangeA, $ip))
    return true;
    else if (preg_match($rangeB, $ip))
    return true;
    else if (preg_match($rangeC, $ip))
    return true;
    else if (preg_match($regex, $ip))
    return false;
    else
    return false;
    }


  6. Is it a class A private ip or B or C:

    function getPrivateIpClass($ip){
    // 10.0.0.0 – 10.255.255.255
    // 172.16.0.0 – 172.31.255.255
    // 192.168.0.0 – 192.168.255.255
    $block = "(25[012345]2[01234]\d[01]?\d\d?)";
    $rangeA = "/^(10)\.($block)\.($block)\.($block)$/";
    $rangeB = "/^(172)\.(3[01]2[0-9]1[6-9])\.($block)\.($block)$/";
    $rangeC = "/^(192)\.(168)\.($block)\.($block)$/";
    $regex = "/^($block\.$block\.$block\.$block)$/";
    if (preg_match($rangeA, $ip))
    return "A";
    else if (preg_match($rangeB, $ip))
    return "B";
    else if (preg_match($rangeC, $ip))
    return "C";
    else if (preg_match($regex, $ip))
    return $ip." is not a private IP.";
    else
    return $ip." is not a valid IP.";
    }


Monday, February 2, 2009

C#: Reading output from PHP

Steps (MS Visual C#):



  1. Create a new Console Application

  2. Declare four variables req, response, output and reader as WebRequest, WebResponse, Stream and StreamReader

  3. Create an web request using WebRequset.Create(string url)
    (e.g. req = WebRequest.Create(http://localhost/index.php);)

  4. Set WebRequest method as POST
    (req.Method = "POST");

  5. Set :
    response = req.GetResponse();
    output = response.GetResponseStream();
    reader = new StreamReader(output);

  6. Now reader has the output from the PHP file

  7. To read from the reader, reader.ReadLine() method can be used

Example:


(C# : phpEchoReader.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;


namespace phpEchoReader {
class Program {
static void Main(string[] args) {
WebRequest req;
WebResponse response;
Stream output;
StreamReader reader;
req =
WebRequest.Create("http://localhost/index.php");
req.Method = "POST";
response = req.GetResponse();
output = response.GetResponseStream();
reader = new StreamReader(output);
while (!reader.EndOfStream)
{
Console.WriteLine(reader.ReadLine());
}
Console.WriteLine("Press any key too continue...");
Console.ReadLine();
}
}
}


(php code: index.php)

for ($i = 0; $i <>

echo $i."\n";

?>


Output: