Showing posts with label PHP with AJAX Technology. Show all posts
Showing posts with label PHP with AJAX Technology. Show all posts

Wednesday, February 22, 2012

Develop pole(Like facebook vote) application using PHP and AJAX


This program is for develop poll application using AJAX ,JSON and PHP . Copy and paste following file to develop poll application . Use Internet Explorer to view the output .In other browser it may not work.

Step 1) Copy and paste below code in your PHP file.


Output:-



Databse table structure:-








<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language="javascript">
function ajax(val)
{

var result=val;
var xmlhttp=new XMLHttpRequest();

 if(xmlhttp=="")
   {
     alert("Object not created");
   }
  else
  {
                //ajax object creaTED.......
    xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
       {
                                 //var result = xmlhttp.responseText;
                                 //document.getElementById("myp").innerHTML=result;
                                 
                                 var jsonText = xmlhttp.responseText;
                                 var jsonObject= eval('('+jsonText+')');
                                 
                                 var yes=jsonObject.yes;
                                 var no=jsonObject.no;

                                 document.getElementById("mypicyes").width=yes;
                                 document.getElementById("mypicno").width=no;

                                 document.getElementById("yes").innerHTML=yes-1;
                                 document.getElementById("no").innerHTML=no;
                                 
                                                                                 
       }
    }
   xmlhttp.open("GET","pollvote.php?vote=" + result,true);
   xmlhttp.send();

  }
}

</script>
<title>AJAX testing page.</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<center><h1>Poll your vote here.</h1></center>

<table>
 <tr>
   <td>Yes:</td>
   <td><img id="mypicyes" src="bg.jpg" height="20px"></td>
   <td><p id="yes"></p></td>
 </tr>
 <tr>
 <td>No:</td>
 <td><img id="mypicno" src="bg.jpg" height="20"></td>
 <td><p id="no"></td>
 </tr>
</table>             
<form name="myform">
<p id="myp"></p>
Select your vote :-<br>
PHP or ASP.Net
<input type="radio" name="radiovote" value="1" onClick="ajax(this.value)">ASP.Net
<input type="radio" name="radiovote" value="0" onClick="ajax(this.value)">PHP
</form>
</body>
</html>


Step 2) Copy the image into same location of your server directory.

Step 3)  Paste below code and edit database connection and table information part. In my table information having following column.
·         Id
·         Yes
·         No


<?php
$val=$vote;

$conn=mysql_connect("localhost","root","");
if($conn==NULL)
  echo "Database connection failure";
 
$db=mysql_select_db("ajax",$conn);
if($db==NULL)
  echo "Database selection failure";
   
if($val==1)
  {
     $qur="select * from vote";
                 $ans=mysql_query($qur);
                 while($row=mysql_fetch_array($ans))
                 {
                    $val=$row['yes'];
                 }
                 $nval=$val + 1;
                 $qur="UPDATE `ajax`.`vote` SET `yes` = '$nval' WHERE `vote`.`id` =1";
                 $ans=mysql_query($qur);
  }
  else
  {
                 $qur="select * from vote";
                 $ans=mysql_query($qur);
                 while($row=mysql_fetch_array($ans))
                 {
                    $val=$row['no'];
                 }
                 $nval=$val + 1;
                 $qur="UPDATE `ajax`.`vote` SET `no` = '$nval' WHERE `vote`.`id` =1";
                 $ans=mysql_query($qur);

  }
 
$qur="select * from vote where id=1"; 
$ans=mysql_query($qur);

while($row=mysql_fetch_array($ans))
{
   $yes= $row['yes'];
   $no= $row['no'];

}
 
     $qur="select * from vote";
                 $ans=mysql_query($qur);
                 while($row=mysql_fetch_array($ans))
                 {
                    $valyes=$row['yes'];
                                $valno=$row['no'];
                 }
                 $total = $valyes + $valno;    
                 $yespercent=ceil(($valyes / $total)* 100);
                 $nopercent=ceil(($valno / $total)* 100);
//Json data creation goes here.
  $ans=Array("yes" => $yespercent,"no" => $nopercent);
  $json_name=json_encode($ans);
  echo $json_name;    //Data is returning main page here
 ?>

Display MySql data using JSON and AJAX in PHP


This program is for implementing AJAX concept with JSON Technology . It will display roll number from database without reloading page. My database fields  are
·         Name
·         Surname
·         Roll

    Output:-


My Database table:-

Step 1) Copy the following code and paste it in your PHP page

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">
function ajax()
{
 var name=document.myform.name.value;

 var xmlhttp=new XMLHttpRequest();
 if(xmlhttp=="")
   {
     alert("Object not created");
   }
  else
  {
                //ajax object creaTED.......
    xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
       {
                                 var jsonText = xmlhttp.responseText
                                 var jsonObject= eval('('+jsonText+')');
                                 
                                 var sur=jsonObject.surname;
                                 var rol=jsonObject.roll;

                                document.myform.surname.value=sur;
                                document.myform.roll.value=rol;

       }
    }
   xmlhttp.open("GET","ajax.php?name="+name,true);
   xmlhttp.send();

  }
}
</script>
</head>
<body>
<form name="myform" method="post">
<table>
<tr>
  <td>Enter name:-</td>
  <td><input type="text" name="name"></td>
</tr>

<tr>
  <td>Surname:-</td>
  <td><input type="text" name="surname"></td>
</tr>


<tr>
  <td>Roll:-</td>
  <td><input type="text" name="roll"></td>
</tr>
</table>
<input type="button" onClick="ajax()" value="Get roll">
<input type="reset" value="clear">
</form>
</body>
</html>

Step 2)Open a PHP file , name it ajax.php and change the necessary coding according to your database setting.

<?php
$name1=$name;
$conn=mysql_connect("localhost","root","");
$ans=mysql_select_db("ajax",$conn);
if($conn=="" || $ans=="")
{
  echo "Database connection error";
}
else
{
  $qur="select * from stud where name='$name1'";
  $ans=mysql_query($qur);
  while($row=mysql_fetch_array($ans))
  {
    $ur=$row['roll'];
                $us=$row['surname'];
  }
   $ans=Array("surname" => $us,"roll" => $ur);
  $json_name=json_encode($ans);
  echo $json_name;
}
?>

Display txt file using AJAX technique


This program is for implementing AJAX concept using PHP . It will display a content of Text file in your browser window using AJAX technique. Go through the following steps and get result. 



Step 1) Create a test file called hello.txt and save it in same location with program and put “Hello world” text within text file

Step 2) Copy and paste below code into a HTML file and run in your web server.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">
function hello()
{
                var xmlhttp;
                xmlhttp=new XMLHttpRequest();

xmlhttp.onreadystatechange=function()
                {
                                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                                {
                                document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                                }             
                }
xmlhttp.open("GET","hello.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>
<form name="myform">
                <p id="myDiv"></p>
                <input type="button" onClick="hello()" value="Click me">
</form>
</body>
</html>