Saturday, 29 March 2014

Validation in PHP form Without Using JavaScripts



//define variables and initialize with empty values
$nameErr = $addrErr = $emailErr = $howManyErr = $favFruitErr = "";
$name = $address = $email = $howMany = "";
$favFruit = array();
 
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        $nameErr = "Missing";
    }
    else {
        $name = $_POST["name"];
    }
 
    if (empty($_POST["address"])) {
        $addrErr = "Missing";
    }
    else {
        $address = $_POST["address"];
    }
 
    if (empty($_POST["email"]))  {
        $emailErr = "Missing";
    }
    else {
        $email = $_POST["email"];
    }
 
    if (!isset($_POST["howMany"])) {
        $howManyErr = "You must select 1 option";
    }
    else {
        $howMany = $_POST["howMany"];
    }
 
    if (empty($_POST["favFruit"])) {
        $favFruitErr = "You must select 1 or more";
    }
    else {
        $favFruit = $_POST["favFruit"];
    }
}
// the HTML code starts here

Tuesday, 18 March 2014

Keyword Relevancy Algorithms

A small and simple algorithm in order to find relevant keywords in a text works as follows:
  1. Initialising array of stop words and keywords,
  2. Text preprocessing, such as lowercasing and removing peculiars,
  3. Determining general text statistics, such as number of characters,
  4. Removing stop words,
  5. Exploding text to keyword array,
  6. Iterating keyword array in order to count earch word,
  7. Sorting and delivering result.

Algorithm : Javascript

/////////////////////////////////////////////////////////////////////////////
//
// The following code snippet works on the text from document.f.text.value
// Within this JavaScript solution, there is no sorting!
//
/////////////////////////////////////////////////////////////////////////////

// init
var aStopWords = new Array ("about", "all", "alone", "also", "am", "and", "as", "at", "because", "before", "beside", "besides", "between", "but", "by", "etc", "for", "i", "of", "on", "other", "others", "so", "than", "that", "though", "to", "too", "trough", "until");


var aWords = new Array ();
var aKeywords = new Array ();

// the text
var sText = document.f.text.value;

// total character count
var iCharCount = sText.length;

// remove line breaks
sText = sText.replace(/\s/g, ' ');

// convert to lowercase
sText = sText.toLowerCase();

// remove peculiars
sText = sText.replace(/[^a-zA-Z0-9äöüß]/g, ' ');

// total word count
aWords = sText.split(" ");
iWordCount = aWords.length;
var iCharCountWithout = 0;

// count words
for (var x = 0; x < aWords.length; x++) {
    iCharCountWithout += aWords[x].length;
}

aWords = new Array();

// remove stop words
for (var m = 0; m < aStopWords.length; m++) {
    sText = sText.replace(' ' + aStopWords[m] + ' ', ' ');
}

// explode to array of words
aWords = sText.split(" ");

// every word
for (var x = 0; x < aWords.length; x++) {
    // trim the word
    var s = aWords[x].replace (/^\s+/, '').replace (/\s+$/, '');
   
    // if already in array
    if (aKeywords[s] != undefined) {
        // then increase count of this word
        aKeywords[s]++;
    }

    // if not counted yet
    else {
        if (s != '') {
            aKeywords[s] = 1;
        }
    }
}

// result
sAlert = "Found keywords:";

n = 1;
for (var sKey in aKeywords) {
    iNumber = aKeywords[sKey];
    fQuotient = Math.round(100 * (iNumber / iWordCount), 2);
    sAlert = sAlert + "\n" + iNumber;
    sAlert = sAlert + " times (" + fQuotient + " %): ";
    sAlert = sAlert + sKey;
    n++;
   
    // break the loop if more then 10 results
    if (n > 10)
        break;
}

// alerting result
alert(sAlert);

// end
*************************************************************************************



Algorithm : php


/////////////////////////////////////////////////////////////////////////////
//
// The following code snippet works on a text delivered via $_POST["text"]
//
/////////////////////////////////////////////////////////////////////////////

// init
$aStopWords = array ('about', 'all', 'alone', 'also', 'am', 'and', 'as', 'at', 'because', 'before', 'beside', 'besides', 'between', 'but', 'by', 'etc', 'for', 'i', 'of', 'on', 'other', 'others', 'so', 'than', 'that', 'though', 'to', 'too', 'trough', 'until');
$aWords = array ();
$aKeywords = array ();

// total character count
$iCharCount = strlen($_POST["text"]);

// remove line breaks
$sText = ereg_replace("[\r\t\n]", ' ', $_POST["text"]);

// decode UTF-8
$sText = utf8_decode($sText);

// convert to lowercase
$sText = strtolower($sText);

// remove peculiars
$sText = preg_replace('/[^a-z0-9äöüß&;#]/', ' ', $sText);

// total word count
$aWords = explode(" ", $sText);
$iWordCount = count($aWords);
$iCharCountWithout = 0;

// count words
for ($x = 0; $x < count($aWords); $x++) {
    $iCharCountWithout += strlen($aWords[$x]);
}

unset ($aWords);

// remove stop words
for ($m = 0; $m < count($aStopWords); $m++) {
    $sText = str_replace(' ' . $aStopWords[$m] . ' ', ' ', $sText);
}

// reduce spaces
$sText = preg_replace('/^\s*$/', ' ', $sText);

// explode to array of words
$aWords = explode(" ", $sText);

// every word
for ($x = 0; $x < count($aWords); $x++) {
    // if already in array
    if (isset ($aKeywords[$aWords[$x]])) {
        // then increase count of this word
        $aKeywords[$aWords[$x]]++;
    }

    // if not counted yet
    else {
        if (trim($aWords[$x]) != '') {
            $aKeywords[$aWords[$x]] = 1;
        }
    }
}

// sort
arsort($aKeywords);

// result
echo '<table>';
echo '<tr><th>Count</th>;
echo '<th>Percentage</th>;
echo '<th>Found keyword</th></tr>';

$x = 0;
while ($iNumber = current($aKeywords)) {
    $iNumber = intval($iNumber);
    $sKey = key($aKeywords);
    $fQuotient = number_format(round(100 * ($iNumber / $iWordCount), 2), 2);
    echo '<tr><td>' . $iNumber . ' </td>';
    echo '<td>' . $fQuotient . ' %</td>';
    echo '<td>' . $sKey . '</td></tr>';
    $x++;
    next($aKeywords);
}
echo '</table>';

// end

Saturday, 15 March 2014

Delete Data and Grid View using PHP

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Crime Reporting</title>
</head>

<body>
<?php
include_once("../Connection.php");
$c = new Connectionclass();
$c->connect();
if(isset($_REQUEST["area_id"]) && isset($_REQUEST["Status"]))
{
    if($_REQUEST["Status"] == "true")
    {
        $query="delete from area_master where area_id=".$_REQUEST["area_id"];
        $c->in_up_de($query);
        echo"<script>alert('Data Deleted');document.location='areadetail.php';</script>";
    }
    else
    {
        echo"<script>document.location='areadetail.php';</script>";
    }
}
else if(isset($_REQUEST["area_id"]))
{   
    echo"<script>
    var status = confirm('Are You Sure,You want to delete?');
    document.location='areagrid.php?area_id=".$_REQUEST["area_id"]."&Status='+status;
    </script>";
}

$query="select a.area_id,a.area_name,c.city_name from area_master as a inner join city_master as c on a.city_id=c.city_id";
$c->calldata($query);

echo"<table border='1'>";
echo"<tr>";
echo"<th>Area Id</th>";
echo"<th>Area Name</th>";
echo"<th>City Name</th>";
echo"<th>Delete</th>";
echo"<th>Update</th>";
echo"</tr>";

while($row=mysql_fetch_array($c->res))
{
    echo"<tr>";
    echo"<td>".$row[0]."</td>";
    echo"<td>".$row[1]."</td>";
    echo"<td>".$row[2]."</td>";
    echo"<td><a href='areagrid.php?area_id=".$row[0]."'><img src='images/delete_icon.png' width='50px'></a></td>";
    echo"<td><a href='areaupdate.php?area_id=".$row[0]."'><img src='images/update.png' width='70px'></a></td>";
    echo"</tr>";
}
echo"</table>";
?>

</body>
</html>

Update the Data Into Mysql Database using PHP

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Crime Reporting</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>

<body>

                <div id="content">
<br />
               
               
                <?php
include_once("../Connection.php");
$c = new ConnectionclasS();
$c->connect();
if(isset($_REQUEST["submit"]))
{
    $query="Update area_master set area_name='".$_REQUEST["txtareaname"]."' where area_id=".$_REQUEST["txtareaid"];
    $c->calldata($query);
    echo"<script>alert('Data Updated');document.location='areadetail.php';</script>";
}


$query = "select * from area_master where area_id=".$_REQUEST["area_id"];
$c->calldata($query);
$row=mysql_fetch_array($c->res);
?>
<form name="AreUpdate" method="post" action="#"><fieldset>
<legend><b>Area Update Details</b></legend><table><br/>
<tr>
<td><b>Area Id:</b></td>
<td><input type="text" name="txtareaid" value="<?php echo $row[0];?>" /></td>
</tr>
<tr>
<td><b>Area Name:</b></td>
<td><input type="text" name="txtareaname" value="<?php echo $row[1];?>" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit"></td>

</tr>
</table>
</fieldset>
</form>               
                </div>

                    <br />
                   
     
  </div>

</body>
</html>

Inserting Data into MySql Database Using PHP

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Crime Reporting</title>
</head>

<body>
<?php
include_once("../Connection.php");
$c = new Connectionclass();
$c->connect();
$query="select * from area_master where area_name='".$_REQUEST["txtareaname"]."' and city_id=".$_REQUEST["drpcity"];
$c->calldata($query);
$no=mysql_num_rows($c->res);

if($no>0)
{
    echo"<script>alert('Duplicate');document.location='areadetail.php';</script>";
}
else
{
  

$query="insert into area_master(area_name,city_id) values('".$_REQUEST["txtareaname"]."',".$_REQUEST["drpcity"].")";
$c->in_up_de($query);
echo"<script>alert('Data Saved');document.location='areadetail.php';</script>";
}
?>
</body>
</html>

How to Create Login Page Using PHP

Login.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Crime Reporting</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>

<body>


<div id="wrapper">
    <div id="header">
        <img src="images/cits.png"  width="959" height="100%" id="header img"/>
    </div>
   
        <div id="nav">
            <a href="index.php"><b>Home</b></a>
        <a href="About.php"><b>About</b></a>
        <a href="Contact.php"><b>Contact Us</b></a>
        <a href="#"><b>Offices</b></a>
        <a href="#"><b>Staffs</b></a>
        <a href="#"><b>Criminals</b></a>
        <a href="#"><b>Message</b></a>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
<input type="text" name="search" />
        <input type="button" name="Search" value="Search" />
       
       
        </div>


                <div id="content">
<br />
                <form name="Login Page" method="post" action="LoginConfirm.php">
                <fieldset>
                <table>
                    <h3 align="center"><img src="images/login_banner.jpg" height="120" width="650" /></h3>
                </table></fieldset><br />
               
                <fieldset><legend><img src="images/loginics.gif" height="40" width="50"/> &nbsp;<b>Login Information</b></legend>    <br />        <table>
                    <tr>
                        <td>User Name:</td>
                        <td><input type="text" name="txtuname" /></td>
                    </tr>
                    <tr>
                        <td>Password:</td>
                        <td><input type="password" name="txtpass" /></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td><input type="submit" name="submit" value="Login"  class="button"/></td>
                        <td><input type="reset" name="reset" value="Reset"  class="button"/></td>
                    </tr>
                    </table></fieldset><br />
                    <tr>
                        <td><a href="Reg.php"><b>New User !</b></a></td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <td><a href="#"></a><b>Forgot Password</b></td>
                    </tr>
               
                       
                </form>
               
                </div>

                    <br />
                   
                        <div id="sidebar">
                        <br />
                        <fieldset>
                        <legend><b>Navigation</b></legend><br />

                        <li><a href="index.php">Home</a></li></br>
                        <li><a href="About.php">About Us</a></li></br>
                        <li><a href="Contact.php">Contact Us</a></li></br>
                        <li><a href="Login.php">Login</a></li></br>
                        <li><a href="Reg.php">Registration</a></li>
                        </fieldset>
                       
                        </div>
   
                                    <div id="footer">
                                    <p class="color">Copyright @ 2014 &nbsp;&nbsp;&nbsp;<a href="#" class="color 1">Cyber Crime</a></p>
                                    </div>
</div>





</body>
</html>


LoginConfirm.php

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
include_once("Connection.php");
$c = new Connectionclass();
$c->connect();

$query = "select * from login_master where uname='".$_REQUEST["txtuname"]."' and password='".$_REQUEST["txtpass"]."'";
$c->calldata($query);

$no=mysql_num_rows($c->res);

if($no > 0)
{
    $row = mysql_fetch_array($c->res);
    $_SESSION["uname"] = $row[1];
    $_SESSION["userid"] = $row[6];
    $_SESSION["type"] = $row[3];

    if($row[3] == "citizen")
    {
        echo "<script>document.location='Citizen/';</script>";
    }
    else if($row[3] == "admin")
    {
        echo"<script>document.location='Admin/';</script>";
    }
    else if($row[3] == "OfficeAdmin")
    {
        echo"<script>document.location='Staff/';</script>";
    }
}
else
{
    echo"<script>alert('Invalid Data');document.location='Login.php';</script>";
}


?>

</body>
</html>

Followers