Wednesday, 4 March 2015

PHP And Ajax (with MYsql database)



(1) create  databse ajax & into that create table ajax_example:

CREATE TABLE `ajax_example` (
  `name` varchar(50) NOT NULL,
  `age` int(11) NOT NULL,
  `sex` varchar(1) NOT NULL,
  `wpm` int(11) NOT NULL,
  PRIMARY KEY  (`name`)
);

(2)now insert the data into table:


INSERT INTO `ajax_example` VALUES ('Jerry', 120, 'm', 20);
INSERT INTO `ajax_example` VALUES ('Regis', 75, 'm', 44);
INSERT INTO `ajax_example` VALUES ('Frank', 45, 'm', 87);
INSERT INTO `ajax_example` VALUES ('Jill', 22, 'f', 72);
INSERT INTO `ajax_example` VALUES ('Tracy', 27, 'f', 0);
INSERT INTO `ajax_example` VALUES ('Julie', 35, 'f', 90);


(3)write code for ajax.html:

<html>
<body>
<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
 var ajaxRequest;  // The variable that makes Ajax possible!
 try{
   // Opera 8.0+, Firefox, Safari
   ajaxRequest = new XMLHttpRequest();
 }catch (e){
   // Internet Explorer Browsers
   try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
   }catch (e) {
      try{
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){
         // Something went wrong
         alert("Your browser broke!");
         return false;
      }
   }
 }
 // Create a function that will receive data 
 // sent from the server and will update
 // div section in the same page.
 ajaxRequest.onreadystatechange = function(){
   if(ajaxRequest.readyState == 4){
      var ajaxDisplay = document.getElementById('ajaxDiv');
      ajaxDisplay.innerHTML = ajaxRequest.responseText;
   }
 }
 // Now get the value from user and pass it to
 // server script.
 var age = document.getElementById('age').value;
 var wpm = document.getElementById('wpm').value;
 var sex = document.getElementById('sex').value;
 var queryString = "?age=" + age ;
 queryString +=  "&wpm=" + wpm + "&sex=" + sex;
 ajaxRequest.open("GET", "ajax-example.php" + 
                              queryString, true);
 ajaxRequest.send(null); 
}
//-->
</script>
<form name='myForm'>
Max Age: <input type='text' id='age' /> <br />
Max WPM: <input type='text' id='wpm' />
<br />
Sex: <select id='sex'>
<option value="m">m</option>
<option value="f">f</option>
</select>
<input type='button' onclick='ajaxFunction()' 
                              value='Query MySQL'/>
</form>
<div id='ajaxDiv'>Your result will display here</div>
</body>
</html>

(4)write code for php file  "ajax-example.php":



<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "ajax";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query String
$age = $_GET['age'];
$sex = $_GET['sex'];
$wpm = $_GET['wpm'];
// Escape User Input to help prevent SQL Injection
$age = mysql_real_escape_string($age);
$sex = mysql_real_escape_string($sex);
$wpm = mysql_real_escape_string($wpm);
//build query
$query = "SELECT * FROM ajax_example WHERE sex = '$sex'";
if(is_numeric($age))
$query .= " AND age <= $age";
if(is_numeric($wpm))
$query .= " AND wpm <= $wpm";
//Execute query
$qry_result = mysql_query($query) or die(mysql_error());

//Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Age</th>";
$display_string .= "<th>Sex</th>";
$display_string .= "<th>WPM</th>";
$display_string .= "</tr>";

// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
$display_string .= "<tr>";
$display_string .= "<td>$row[name]</td>";
$display_string .= "<td>$row[age]</td>";
$display_string .= "<td>$row[sex]</td>";
$display_string .= "<td>$row[wpm]</td>";
$display_string .= "</tr>";
}
echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;
?>


Monday, 2 March 2015

PHP Script for Sending email

<?php

   $to = "omchandmaurya1@gmail.com";
  
   $subject = "This is subject";
  
   $message = "This is simple text message.";
  
   $header = "From:omnetworkom@gmail.com \r\n";
  
   $retval = mail ($to,$subject,$message,$header);
  
   if( $retval == true ) 
   {
      echo "Message sent successfully...";
   }
   else
   {
      echo "Message could not be sent...";
   }

?>

Saturday, 28 February 2015

PHP script for email verification or confirmation in php?

(1) first create table user:

create table user
(id int primary key auto_increment,

name varchar(200),
email varchar(200),
phone varchar(200),
password varchar(200),
com_code varchar(200));


(2)write code for connect.php file:

<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("itexamtime",$con) or die(mysql_error());

?>

(3)write code for registerform.php file:
<?php


echo '<form action="insert1.php"   method="post">
      Name<input type=text name="name">
 Email<input type=email name="email">
 phone  <input type=text    name="phone">
 password<input type=password name="password">
 <input  type=submit  name=submit value=submit>';

 
?>

(4)write code for insert1.php file:



<?php
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$password=$_POST['password'];
$com_code=md5(uniqid(rand()));
include_once("connect.php");

$sql="insert into user(name,email,phone,password,com_code)values('$name','$email','$phone','$password','$com_code')";

$result=mysql_query($sql) or die(mysql_error());

if($result)
  {
   $to = $email;
   $subject = "Confirmation from TutsforWeb to $username";
   $header = "ItexamTime: Confirmation LInk";
 
   $message .= "http://funmoviemasti.com/confirm.php?passkey=$com_code";

   $sentmail = mail($to,$subject,$message,$header);

   if($sentmail)
            {
   echo "Your Confirmation link Has Been Sent To Your Email Address.";
   }
   else
         {
    echo "Cannot send Confirmation link to your e-mail address";
   }

}

?>

(5)write code for confirm.php file:

<?php

include_once("connect.php");

$passkey = $_GET['passkey'];

 $sql="update user set com_code='1'  where com_code='$passkey'";

$result=mysql_query($sql) or die(mysql_error());

if($result)

{


echo "your account is active ";




}



?>







Friday, 27 February 2015

PHP Script for Dynamic Drop down List

(1)create table   account:


create table account
(
id  int(5) primary key auto_increment,
name  varchar(20),
contact  varchar(20),
address  varchar(200),
emailid  varchar(20),
account   varchar(20),
password   varchar(20),
image      varchar(200),
idproof     varchar(200),
addressproof varchar(200));

(2)insert some values  into table
(3) then use following code :


<html>
<head>
<title>
Deposit money
</title>
</head>

<body>
<form   >
<select name=account>
<?php

include("connect.php");

$sql="select * from account";

$result=mysql_query($sql);

while($row=mysql_fetch_array($result))
{

echo '<option value="'.$row['id'].'">';

echo $row['id'];

echo '</option>';

}

?>
</select>
<form>
</body>
</html>

Undefined Index problem in PHP


this problem comes when you do not declare  name
so overcome this use following code:


$mobile=isset($_POST['mobile']) ? $_POST['mobile'] : 'Null';

Thursday, 26 February 2015

PHP script for file Upload

PHP script for file Upload

(1)step 1: write code for  "slider.html"

<form action="sliderupload.php"   method="post"  enctype="multipart/form-data">
UPload<input type="file"  name="image">
 <input type="submit"  name="submit"  value="Upload">


</form>

(2)step 2: write code for  "sliderupload.html"


<?php

$image=$_FILES['image']['name'];

include("connect.php");

$sql="insert into slider(image)values('$image')";

mysql_query($sql) or die(mysql_errror());

move_uploaded_file($_FILES['image']['tmp_name'],"slider/".$_FILES['image']['name']) or die(mysql_error());

?>

(3)write code for class.php file :

<?php


class db

{

var $host="servername";

var  $username="username";

var  $password="password";

function connect()
{

$connect=mysql_connect($this->host,$this->username,$this->password);

mysql_select_db("a1390987_om",$connect) or die(mysql_error());

if(!$connect)
{

echo "connection error ";
}





}

function display($table)
{


$sql="select * from $table ";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);




return $row;

}


}

$obj=new db();
$obj->connect();



?>


(4)use img tag to display image & write following code for displayslider.php:

<?php

include("class.php");

$table="slider";

$slider=$obj->display($table);

$image=$slider['image'];

echo $image;



echo "<img  src='slider/".$image."'   height=200 width=200   border=10 >";





?>

PHP Script for Admin login with session management & Logout

(1)first  write following code for Login form   "loginform.html"  file

<form action="login.php"  method="post">
<table border=0>
<tr><td>User name:</td>
<td>
                                         <input name="username" type="TEXT"></td> </tr>
                                  <tr><td>Password</td><td>
                                         <input name="Password" type="PASSWORD"></td>
                                         </tr>
                                       
                                   

                                    <tr>
                                        <td></td><td><input value="submit" name="SubmitUser" type="submit"></td>
</form>
                                    </tr>
                                </table>


(2) Then write code for  "login.php" file

<?php



$username=$_POST['username'];
$password=$_POST['password'];
$con=mysql_connect("hostname","username","password") or die(mysql_error());

mysql_select_db("databasename",$con);

$sql="select username,password  from login where username='$username'  AND password='$password'";

$result=mysql_query($sql) or die(mysql_error());

$num=mysql_num_rows($result) or die(mysql_error());


if($num>0)
{

 session_start();

 $_SESSION['username']=$username;

header("Location:dashboard.php");

}

else if($num==0)
{
header("Location:error.php");
}



?>


(3)Session management code:



<?php

session_start();
if(!isset($_SESSION['username']))
{

header("Location:index.php");

}


?>
NOte:
write this code on every your page starting or simply save it as

session.php  & on every page use it as

<?php  include("session.php");   ?>


(4)Logout code : save file name as "logout.php"

see below code:

<?php

session_start();

session_destroy();

header("Location:loginform.html");

?>


Tuesday, 24 February 2015

how to use ckeditor in php ?
(1)download   (ckeditor_3.6.6)
(2)user following code to link:
<?php
    include("ckeditor/ckeditor.php");
    $CKeditor = new CKeditor();
    $CKeditor->BasePath = 'ckeditor/';
    $CKeditor->editor('editor1');
?>


& use can also use another following code :



<?php require("ckeditor/ckeditor.php"); ?>

 <script type="text/javascript" src="ckeditor/ckeditor.js"></script>
 <script type="text/javascript" src="somedirectory/ckeditor/ckeditor.js"></script>






<textarea class="ckeditor" name="editor1">Write any thing</textarea>

Wednesday, 18 February 2015

Login page code in PHP


 (1)first create file index.html


<form action="login.php"  method="post">
                     <span class="loginText">User name:</span><br>
                                         <input name="username" type="TEXT"><br>
                                         <span class="loginText">Password:</span><br>
                                         <input name="Password" type="PASSWORD">
                                         <br>
                                         <span class="loginText">login as user: <i>demo</i><br>with password: <i>anahaw</i></span>
                                        </td>
                                    </tr>

                                    <tr>
                                        <td align="right"><input value="submit" name="SubmitUser" type="submit"></td>
                                        </form>

(2)create login.php file

<?php

$username=$_POST['username'];
$password=$_POST['Password'];
$con=mysql_connect("localhost","root","") or die(mysql_error());

mysql_select_db("payroll",$con);

$sql="select username,password  from login where username='$username'  AND password='$password'";

$result=mysql_query($sql) or die(mysql_error());

$num=mysql_num_rows($result) or die(mysql_error());


if($num>0)
{
 session_start();
 $_SESSION['username']=$username;
header("Location:welcome.php");

}

else
{
echo "invalid login";
}

?>



Pagination in PHP



<?php

    mysql_connect("localhost","root","")or die("Problems...");
    mysql_select_db("payroll");

    $per_page = 2;
    $pages_query = mysql_query("SELECT COUNT('id') FROM  employee");
    echo $pages_query."<br>";
    $pages = ceil(mysql_result($pages_query, 0) / $per_page);
    echo $pages;
  
    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
    $start = ($page - 1) * $per_page;
  
    $query = mysql_query("SELECT *  FROM employee LIMIT $start, $per_page");
  
    while($row = mysql_fetch_assoc($query))
  
  
    {
  
  

echo '<form action=update.php  method=post      enctype="multipart/form-data">
<table border=1  cellspacing=3 cellpadding=2 bgcolor=orange  bordercolor=black>
<tr><td>DATE OF SIGN UP</td><td><INPUT TYPE=TEXT NAME="name"  value="'.$row['name'].'"></td></tr>
<tr><td>NAME</TD><td><INPUT TYPE=TEXT NAME="phone"      value="'.$row['phone'].'"></td></tr>
<tr><td>ADDRESS</TD><TD><input type=text name="salary"        value="'.$row['salary'].'"></td></tr>

<tr><td> </td><td>
<input type=hidden name=hidden  value="'.$row['emp_id'].'">
<input type=submit name=update value=update></td></tr>


</TABLE>


</FORM>';


  
  
  
    }
  
    $prev = $page - 1;
    $next = $page + 1;
  
    if(!($page<=1)){
        echo "<a href='pagination1.php?page=$prev'>Prev</a> ";
    }

    if($pages>=1 && $page<=$pages){
  
        for($x=1;$x<=$pages;$x++){
            echo ($x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></strong> ' : '<a href="?page='.$x.'">'.$x.'</a> ';
      
        }
  
    }
  
    if(!($page>=$pages)){
        echo "<a href='pagination1.php?page=$next'>Next</a>";
    }
  
  
?>

Ajax with PHP & Mysql database

(1) create file  index.html



 <html>
<head>
<script>
function showUser() {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","getuser.php?",true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>

<form name="users"  onclick="showUser()">

<input type=submit value=submit   >

</form>
<form>
<select name="users" onchange="showUser()">
  <option value="">Select a person:</option>
  <option value="1">Peter Griffin</option>
  <option value="2">Lois Griffin</option>
  <option value="3">Joseph Swanson</option>
  <option value="4">Glenn Quagmire</option>
  </select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>

</body>
</html>

(2) then create file

getuser.php






<!DOCTYPE html>
<html>
<head>
<style>
table {
    width: 100%;
    border-collapse: collapse;
}

table, td, th {
    border: 1px solid black;
    padding: 5px;
}

th {text-align: left;}
</style>
</head>
<body>

<?php
//$q = intval($_GET['q']);

$con = mysqli_connect('localhost','root','','my_db');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"ajax_demo");
//$sql="SELECT * FROM user WHERE id = '".$q."'";

$sql="SELECT * FROM user";

$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['FirstName'] . "</td>";
    echo "<td>" . $row['LastName'] . "</td>";
    echo "<td>" . $row['Age'] . "</td>";
    echo "<td>" . $row['Hometown'] . "</td>";
    echo "<td>" . $row['Job'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>


Wednesday, 24 December 2014

How to convert html website to wordpress theme?

(1)create folder name as mytheme in wp_content
(2)copy index.html file to mytheme & rename it as index.php
(3)copy the content of header from index.php and save it is as header.php
(4)in header.php  link of href for stylesheet url code is  <?php bloginfo('stylesheet_url')?>
(5)then type <?php get_header(); ?>
(6)copy the code of index.php and paste into footer.php
(7)then open index.php and type  <?php get_footer(); ?>
(8)then type  <?php wp_footer();?> in end of footer.php file.
(9)then copy sidebar html code form index.php to sidebar.php file.
(10)and then type  <?php get_sidebar(); ?> into the index.php file.
(11)create single .php form index.php code.
(12)copy  loop code
to index.php

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
  <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <div class="post-header">
        <div class="date"><?php the_time( 'M j y' ); ?></div>
        <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <div class="author"><?php the_author(); ?></div>
    </div><!--end post header-->
    <div class="entry clear">
        <?php if ( function_exists( 'add_theme_support' ) ) the_post_thumbnail(); ?>
        <?php the_content(); ?>
        <?php edit_post_link(); ?>
        <?php wp_link_pages(); ?>
    </div><!--end entry-->
    <div class="post-footer">
        <div class="comments"><?php comments_popup_link( 'Leave a Comment', '1 Comment', '% Comments' ); ?></div>
    </div><!--end post footer-->
  </div><!--end post-->
<?php endwhile; /* rewind or continue if all posts have been fetched */ ?>
  <div class="navigation index">
    <div class="alignleft"><?php next_posts_link( 'Older Entries' ); ?></div>
    <div class="alignright"><?php previous_posts_link( 'Newer Entries' ); ?></div>
  </div><!--end navigation-->
<?php else : ?>
<?php endif; ?>


Read the code for  wordpress file... website links..

http://code.tutsplus.com/tutorials/creating-a-wordpress-theme-from-static-html-uploading-your-theme-to-wordpress--wp-33955

to image link:

<img alt="" src="<?php bloginfo( 'stylesheet_url' ); ?>/images/logo.jpg" />

(13)to create new page for use template file page.php
following is the code for that

<?php get_header(); ?>

 <div id="content">
<?php if(have_posts()): ?>
<?php while(have_posts()):the_post()?>
<?php the_content(''); ?>
<?php endwhile; ?>
<?php endif; ?>
</div>

<?php get_footer(); ?>

Monday, 18 February 2013



how to upload  any website to another  web hosting server ?
(1)first intall Filezilla FTP client side  software.(it is open source free)
(2)then  prepare your website   like images in image  folder and data in data folder and index.html or index.php as main page all linked pages inside a folder  like websitename
(3)when you run filezilla  it will ask for you host   ,username,password ,port  etc
(4)the type  host means your website address
(5)username and password will be given when you purchase domain name.
(6) enter it 
(7)the at left side you will find  local server and right side remote server which is hosting server.
(8)you just drag it  your website folder from  you localcomputer to  remote 
note first drag main page index.php  and all other linked pages to it then  image and data folder.
it is too easy.