Sunday, January 1, 2012

Ajax Example in PHP

Example



<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
var XMLHttpRequestObject = false;
if(window.XMLHttpRequestObject)
{
   XMLHttpRequestObject = new XMLHttpRequest(); 
}
else if(window.ActiveXObject)
{
  XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
function getdata(datasource, divid)
{
  if(XMLHttpRequestObject)
  { 
    var obj = document.getElementById(divid);
XMLHttpRequestObject.Open("GET",datasource+document.getElementById("tb1").value);
XMLHttpRequestObject.onreadystatechange = function()
{
 if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
 {
   obj.innerHTML = XMLHttpRequestObject.responseText;
    


 }
}
XMLHttpRequestObject.send(null);
}
}    




</script>


</head>


<body>
<form>
<input name="tb1" type="text">
<input name="submit" type="button" value="cllick me" onClick="getdata('http://localhost/ajax/ajax2.php?tb1=','targetdiv')">
</form>
<div id = "targetDiv">
<p>fetched msg</p>
</div>
</body>
</html>

Random Function in PHP

Example



<body bgcolor="#CCCCFF">
<font size="+4" ><center>RANDOM FUNCTION DEMO</font></marquee></center><br><br><br>
</head>
<body>
<?php

echo "Generate Random Number Between (50 to 200) [using rand()] ::".rand(50,200);
echo "<br><br>";
//echo "Generated Number [using srand()] ::".srand(20);
//echo "Generated Number [using mt_srand()] ::".mt_srand(20);

echo "Generate Maximum No Of Integer [using getrandmax()]::".getrandmax();
echo "<br><br>";
echo "Generate Number Betwee (0 & 100) [using mt_rand()] ::".mt_rand(0,100);
echo "<br><br>";
echo "Generate Largest Possible Value [using getrandmax()] ::".mt_getrandmax();
echo "<br>";
?>
</body>

Odd/Even/Prime Example in PHP

Example



<body>
<form id="form1" name="form1" method="post" action="">
  <label>ENTER NUMBER::
  <input type="text" name="t1" />
  </label>
  <p>&nbsp;</p>
  <p>
    <label>
    <input type="submit" name="b1" value="Submit" />
    </label>
  </p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>


<?php


if(isset($_POST["b1"]))
{
if(is_numeric($_POST["t1"]))
   {
 if($_POST["t1"]%2==0)
 {
echo $_POST["t1"]." IS EVEN";
 }
 else
 {
echo $_POST["t1"]." IS ODD";
 }
 $i=2;
 
while($i<$_POST["t1"])
       {
if($_POST["t1"]%$i==0)
{
  echo "\n"." & It is not Prime Number";
  break;
}
$i++;
       }
       if($i==$_POST["t1"])
       {
echo "\n"." &  it is Prime Number";
       }
}
 
  else
  {
  echo "INPUT A NUMBER !!";
  }
}

?>
</form>
</body>

Date/Time Function in PHP

Example



<body bgcolor="#CCCCFF">
<font size="+4" ><center>DATE/TIME FUNCTION DEMO</font></marquee></center><br><br><br>
</head>
<?Php


//echo "CHECK IN DATE(DD/MM/YY FORMAT) WHETHER EXIST OR NOT::".checkdate(12,24,2010);
////echo "<br>";
//echo time();
echo "<br>";
//echo date("ddmmyy");
echo "<br>";
//echo localtime();
//echo time();
echo "<br>";
//echo gettimeofday();
//echo idate();
echo "<br>";






echo "(dd mon, year | time) :";
echo date("j M, Y");
echo " | ";
echo date("g:i a");
echo " | ";
echo date("H:i:s");
?>




String Function In PHP

Example



<body bgcolor="#CC99CC">
<font color="#993300" size="+2"><center>STRING FUNCTION DEMO</font><br><br>
</head>

<body>

<form id="form1" name="form1" method="get" action="">
  <label>EnterString::
  <textarea name="textarea"></textarea>
  </label>
  <label>ENTER SECOND STRING
  <input type="text" name="tf" />
  </label>
<label></label>
  <p>&nbsp;</p>
  <p>
    <label>
    <input type="submit" name="b1" value="Submit" />
    </label>
  </p>
</form>

<?php

if(isset($_GET["b1"]))
{

$tf=$_GET["tf"];
$s=$_GET["textarea"];

echo "1.LENGTH OF STRING IS::"." ".strlen($s)."<p>";
echo "2.REVERSE OF STRING IS::"." ".strrev($s)."<p>";
echo "3.UPPER CASE OF STRING::"." ".strtoupper($s)."<p>";
echo "4.LOWER CASE OF STRING::"." ".strtolower($s)."<p>";
echo "5.RETURN 0 IF BOTH STRING ARE SAME::"." ".strcmp($s,$tf)."<p>";
echo "6.REPEATATION OF STRING ::"." ".str_repeat($s,2)."<p>";
echo "7.CASE COMPARE::"." ".strcasecmp($s,$tf)."<p>";
echo "8.SUBSTRING::"." ".substr($s,1,4)."<p>";
$b=array('you','are',$tf,$s);
echo "9. USING JOIN::".join(" ",$b)."<p>";
echo "10. STRING REPLACE::".str_replace("sur",$tf,$s)."<p>";
}
?>
</body>

Array Demo in PHP

Example



<body bgcolor="#CCCCFF">
<font size="+4" ><center>ARRAY DEMO</font></marquee></center><br><br>
</head>
<body>
<?php


$test=array(501=>"ITIM",502=>"UNIX",503=>"AWT",504=>"JAVA",505=>"PHP",506=>"PRACTICAL");
echo "<h4>"."1.NUMBER OF ITEMS STORED IN ARAAY ARE::".count($test)."(using count())"."<br><br>";
echo "2.SCROLLING THE ITEMS:"."<br><br>";
echo "Current::".current($test)."<br>";
echo "Next::".next($test)."<br>";
echo "Prev::".prev($test)."<br>";
echo "Last::".end($test)."<br><br>";
echo "3.STORED ITEM IN ARRAY ARE AS FOLLOWS(using foreach loop)"."<br><br>";
foreach($test as $a)
{
echo $a;
echo "<br>";
}
echo "<br><br>";
/*do
{
echo current($test);
echo "<br>"
}while($a=next($test))*/


echo "4. ARRAY INSPECTION (ISARRAY)::".is_array($test)."<br>";




?>
</body>

Saturday, December 24, 2011

Post Method in PHP

Defination
POST is the preferred method of form submission today,particularly in non-idempotent usage (those that will result in permanent changes),such as adding information to a database. The form data set is included in the body of the form when it is forwarded to the processing agent.No visible change to the URL will result according to the different data submitted.


Example
<html>
<body>
<?php
//isset is a function that test a variable to see whether it has been assigned a value

if (isset($_POST["submit"])
{
  if( $_GET["name"] || $_GET["age"] )
  {
     echo "Welcome ". $_GET['name']. "<br />";
     echo "You Rollno is ". $_GET['rollno']. ";
     exit();
  }
}
?>

  <form action="<?php $_PHP_SELF ?>" method="GET">
  Name: <input type="text" name="name" />
  Age: <input type="text" name="rollno" />
  <input type="submit" />
  </form>
</body>
</html>

QUE: difference between get and post?

GET:

http://www.test.com/index.htm?name1=value1&name2=value2
  • The GET method produces a long string that appears in your server logs, in the browser's Location: box.
  • The GET method is restricted to send upto 1024 characters only.
  • Never use GET method if you have password or other sensitive information to be sent to the server.
  • GET can't be used to send binary data, like images or word documents, to the server.
  • The data sent by GET method can be accessed using QUERY_STRING environment variable.
  • The PHP provides $_GET associative array to access all the sent information using GET method.
POST:
  • The POST method does not have any restriction on data size to be sent.
  • The POST method can be used to send ASCII as well as binary data.
  • The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.
  • The PHP provides $_POST associative array to access all the sent information using GET method.
    For GET methodClick to View