Translate

Thursday 30 October 2014

Simple Login Form Using PHP

Explaination:

In this tutorial i have explained the way of login concept.In this tutorial i has using PHP to check the conditions whether the username
 is Correct or Wrong.If the Username is Correct it further procedd the Step orelse it shows the error by using <?php if(isset($error)){ echo $error;}?>
 this PHP Condition.Because of using isset condition suppose if your not upload the files it seems notice error so we avoid this problem we'll use 
 isset PHP condition.




index.php:


    <html>
    <head>
    <style>
    #loginform{background-color;#000;color:#FFF;margin:0px auto;}
    </style>
    </head>
    <form id="loginform" method="post" action="select.php">
    <p class="animate4 "><input type="text" id="username" name="username" placeholder="Username" autocomplete="off" required /></p>
    <p class="animate5 "><input type="password" id="password" name="password" placeholder="Password"  autocomplete="off" required /></p>
    <p class="animate6 "><input type="submit" class="btn-block" name="submit" value="Submit"></p>
    <p class="warningmsg" style="color:#F00;"><?php if(isset($error)){ echo $error;}?></p>
    </form>
    </html>


 
  In his first conditon is to connect the databse anf if isset condition is used for to check is the if the value is set and it is not null.
 If the condition is not null it enters into if condition otherwise it shows mysql_error.Then we have to write to do one more steps to
 store the username in one variable that variable SESSION.Why we use session means we can use it any files for our future use.
 Then we have to select  the query wherether we POST that Value and DB field value is equal r not equal(i.e..)username and Password
 values.Before the sessions we have to start session ie,session_start.


select.php:


<?php
 include("db.php");
session_start();
if(isset($_POST['submit']))
{
    if(!$_POST['username'] || !$_POST['password'] ) {
        $error = "Please enter all details !!";
    } else {
        $username=$_POST['username'];
        $password=$_POST['password'];
       $sql="select * from adminlogin where username='$username' and password='$password'";
       $select=mysql_query($sql);
       $row=mysql_fetch_array($select);
       $_SESSION['login']=$row['username'];
       if ($row['username'])
       {
           echo '<script type="text/javascript">alert("Successfully Login");</script>';
           header("location:dashboard.php");
           }

        else
       {
               $error="<span class='error-box'>Your Login Username or Password is invalid </span>";
       }}
}
?>

 

 $_SESSION['login'] before we store  the username in one session variable means we can declare here and check here the condition 
 if the session is not equal to that session value it redirect to index.php page else it proceed further login page successfully



session.php:

 <?php
include('db.php');
session_start();
$user_check=$_SESSION['login'];

$ses_sql=mysql_query("select username from adminlogin where username='$user_check' ");

$row=mysql_fetch_array($ses_sql);

$login_session=$row['username'];

if(!isset($login_session))
{
header("Location: index.php");
}
?>

I hope this Tutorial is very Usefull for the beginners who is fresher to core PHP this tutorial is very usefull for to learn
the Login COncepts and how the session is to be work on in that.

Monday 20 October 2014

By using Onchange and Onkeyup Function to Display the Records Using Ajax

Explaination:

                   
In this tutorial i has posted the we ahve two categories one is Category and other is Sub Category In this will select the category that category according datas will dispaly in subcategory by using.Then for price and quantity it will total and will display in the total field  by using Javascript.
then the Image Upload .This all is display  by fetching with Database.


Database fields are:
                  Id->Auto Increment
                  2.Category
                  3.Subcategory
                  4.Image

Form Design:


Form :


<div style="width: 34%; border: 1px solid rgb(0, 0, 0); background-color: #000; color: #FFF;">
<form name="form" method="post" enctype="multipart/form-data">
<p><label for="category">Category</label>
<select name="categorys" onchange="showCustomer(this.value)">
<option value="">--select--</option>
<?php 
$select=mysql_query("select * from categories");
while($cat=mysql_fetch_array($select))
{
?>
<option value="<?php echo $cat['cat_id'];?>"><?php echo $cat['cat_name'];?></option>
<?php }?>
</select>
</p>
<p><label>Sub Category</label>
<span id="txtHint">
    <select name="sub_category"><option><----Select----></option></select>
</p>
<p><label>Price</label>
<input id="box1" type="text" name="price" oninput="calculate()" />
</p>
<p><label>Quantity</label>
<input id="box2" type="text"  name="quantity" oninput="calculate()" />
</p>
<p><label>Total</label>
<input id="result" name="total"/>
</p>
<p><label>Image Upload</label>
<input type="file" name="image" value=""/>
</p>
<p><input type="submit" name="submit" value="Submit"/></p>
</form>
</div>



By Using This Form We have to display the results that has to fetch from the database
oninput="calculate()" is used to calaculate the Price and Quantity 

Display Results:



Coding:

By Using This Form We have to display the results that has to fetch from the database
oninput="calculate()" is used to calaculate the Price and Quantity 
This  onchange="showSearch(this.value)" and onkeyup="showSearchs(this.value)" is used for
to dispaly the results while typing the category onkeyup="showSearchs(this.value)" Onkeyup function 
is used to display the results and onchange="showSearch(this.value)" Onchange funciton is used to
display the results by dropdown.






<tr>
<td><select name="search" onchange="showSearch(this.value)" >
    <option value=""><----Search----></option>
    <?php 
$selects=mysql_query("select * from categories");
while($cats=mysql_fetch_array($selects))
{
?>
<option value="<?php echo $cats['cat_id'];?>"><?php echo $cats['cat_name'];?></option>
<?php }?>
</select>

</td>
</tr>
<form method="post" action="" id="form1">
<div  style="margin: 0px 0px 0px 516px;">
<input type="text" id="category" name="category"   value=""  onkeyup="showSearchs(this.value)"/>
<input type="submit" class="but_style" name="search_list" value="Search" style="width:auto; border:solid thin  #b32d00;
border-radius:.3em; color:#FFF; background-color:#f7803b; cursor:pointer; height:27px;" ></div>
</form>



To display the results  while fetching with database id="txtHint2" this id is used for to disaply the reuslts by using AJAX
Then the inline edit will use in <tr> or <div> class="edit_td" use the class as edit_td then for display the results 
id="category_<?php echo $id; ?>" will pass this id and in input field also will pass this id
id="category_input_<?php echo $id; ?>"


<table width="70%" border="1"  id="txtHint1" cellpadding="0" cellspacing="0" class="table-list" style="text-align:center;">
<tr>
<th width="20%">S.no</th>
<th width="20%">Category</th>
<th width="20%">Category Name</th>
<th width="40%">Sub Category name</th>
<th width="40%">Sub Category </th>
<th width="20%">Price</th>
<th width="20%">Quantity</th>
<th width="20%">Total</th>
<th width="20%">Image</th>
</tr>
<tbody id="txtHint2">
<tr>
<?php 
$i=1;
$select3=mysql_query("select * from demo");
while($cat3=mysql_fetch_array($select3))
{
$id=$cat3['id'];
$pr=$cat3['price'];
$cat=$cat3['category'];
$cat1=$cat3['category_name'];
$sub=$cat3['sub_category'];
$images=$cat3['image'];
$selcts=mysql_query("select * from categories where cat_id='$cat'");
$ro=mysql_fetch_array($selcts);
$ca=$ro['cat_name'];
$selcts1=mysql_query("select * from sub_categories where sub_cat_id='$sub'");
$ro1=mysql_fetch_array($selcts1);
$su=$ro1['sub_cat_name'];
?>

<tr id="<?php echo $id; ?>" class="edit_tr">
<td><?php echo $i++;?></td>
<td class="edit_td">
<span id="category_<?php echo $id; ?>" class="text"><?php echo $cat; ?></span> 
<input type="text" value="<?php echo $cat; ?>" class="editbox" id="category_input_<?php echo $id; ?>"/>
</td>

<td class="edit_td">
<span id="category_name_<?php echo $id; ?>" class="text"><?php echo $ca; ?></span>
<input type="text" value="<?php echo $ca; ?>" class="editbox" id="category_name_input_<?php echo $id; ?>" /&gt;
</td>

<td class="edit_td">
<span id="sub_category_<?php echo $id; ?>" class="text"><?php echo $su; ?></span> 
<input type="text" value="<?php echo $su; ?>" class="editbox" id="sub_category_input_<?php echo $id; ?>"/>
</td>

<td class="edit_td">
<span id="price_<?php echo $id; ?>" class="text"><?php echo $pr; ?></span> 
<input type="text" value="<?php echo $pr; ?>" class="editbox" id="price_input_<?php echo $id; ?>"/>
</td>
<td class="edit_td">
<span id="quantity_<?php echo $id;?>" class="text"> <?php echo $cat3['quantity'];?></span>
<input type="text" value="<?php echo $cat3['quantity'];?>" class="editbox" id="quantity_input_<?php echo $id;?>"/>
</td>

<td>
<span id="total_<?php echo $id;?>" class="text"><?php echo $cat3['total'];?></span>
<input type="text" value="<?php echo $cat3['total'];?>" class="editbox" id="total_input_<?php echo $id;?>"/>
</td>

<td><img src="upload/<?php echo $cat3['image'];?>"width="30px" height="30px"/></td>
<td><a href="#" id="'.$row['id'].'" class="del">Delete</a></td>
</tr>
<?php
}
?>
<tbody>
</table>


Ajax Coding For  id="txtHint" for Sub Category:


<script>
  function showCustomer(str)
        {
        var xmlhttp;    
        if (str=="")
          {
          document.getElementById("txtHint").innerHTML="";
          return;
          }
        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","ajax_cat.php?q="+str,true);
        xmlhttp.send();
        }
        </script>



Ajax Coding For  id="txtHint1" for select the options:



<script>
  function showSearch(str)
        {
        var xmlhttp;    
        if (str=="")
          {
          document.getElementById("txtHint1").innerHTML="";
          return;
          }
        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("txtHint1").innerHTML=xmlhttp.responseText;
            }
          }
        xmlhttp.open("GET","ajax_search.php?q1="+str,true);
        xmlhttp.send();
        }
        </script>




Ajax Coding For  id="txtHint2" for Onkeyup:

function showSearchs(str)
        {
        var xmlhttp;    
        if (str=="")
          {
          document.getElementById("txtHint2").innerHTML="";
          return;
          }
        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("txtHint2").innerHTML=xmlhttp.responseText;
            }
          }
        xmlhttp.open("GET","search.php?q2="+str,true);
        xmlhttp.send();
        }
        </script>



Javascript Coding For calaculate Price and Quantity :

<script type="text/javascript">
function calculate() {
  var myBox1 = document.getElementById('box1').value; 
  var myBox2 = document.getElementById('box2').value;
  var result = document.getElementById('result'); 
  var myResult = myBox1 * myBox2 ;
  result.value = myResult; 
 }
 </script>
 

search.php for to display table records :


Through the Ajax by passing the Values to the div r table there we'll get the datas to search.php page.
by passing the values through id="txtHint2" or we can give any vlaue but we should pass the id's
correctly.Then we should GET that Passing Datas in search.php page.



<?php
include('conn.php');
if($_GET['q2'])
{
$id3=$_GET['q2'];
$select=mysql_query("select * from demo where category_name='$id3'");
        while($search=mysql_fetch_array($select))
        {
        $id=$search['id'];
        $pr=$search['price'];
        $qu=$search['quantity'];
        $to=$search['total'];
        $cat=$search['category'];
        $cat1=$search['category_name'];
        $sub=$search['sub_category'];
        $su=$search['sub_cat_name'];
        $images=$search['image'];
        echo "<tr>";
        echo "<td>" . $id . "</td>";
        echo "<td>" . $cat. "</td>";
        echo "<td>" . $cat1. "</td>";
        echo "<td>" . $sub . "</td>";
        echo "<td>" . $su . "</td>";
        echo "<td>" . $pr . "</td>";
        echo "<td>" . $qu . "</td>";
        echo "<td>" . $to. "</td>";
        echo "<td><img src=upload/$images width='50px' height='50px'></td>";
        echo "</tr>";
        }}
else 
{
echo "No Records Found";

}
?> 



ajax_cat.php for sub-category:

For Sub categorys when we select that categeory list that subcategory list releated with category 
datas is coming by using the ajax.



<?php
include('conn.php');
?>
<?php
if(isset($_GET['q']))
{
    //print_r($_GET);
    $id1=$_GET['q'];
    
    ?>
<p>
<select name="sub_category">
    <option><----Select----></option>
<?php
$select1=mysql_query("select * from sub_categories where cat_id=$id1") ;
while($cat1=mysql_fetch_array($select1))
{
 ?>
<option value="<?php echo $cat1['sub_cat_id'];?>"><?php echo $cat1['sub_cat_name'];?></option>
<?php }?>
</select>
</p>
<?php }?>

Saturday 18 October 2014

Multiple Images Upload using Ajax and Javascript

Explaination:
            Multiple images Upload and deleted using AJAX.In this tutorial i have briefly  explained about 
            the image uplaoding and deleted that images using ajax.Here Added Multiple images in add_images.php
            file i mentioned and edting and updating coding also i written coding for your reference.If we click on edit it will go to
            edit_packages.php page there we can delete the images using ajax and Update the images through PHP.When we Click on(X)
            ther we written ONclick Function to delete the images throgh Javascript ajax request.We will pass this request to delete_images.php
            there we have written the coding for to delete the images.I hope this is to be easily understand for ur reference to upload multiple 
            usinh AJAX,JAVASCRIPT and PHP.


Steps For 

Add_images.php:


Steps:
 =>First of all we must have written the database connect coding and include in ur file.
    
 =>enctype="multipart/form-data" ->this is must to upload images.
    
 =>Give the Image Path direction for to uplaod the images.  

Form To Upload Multiple Images:







<html>
<body>
<form method="post" action="" enctype="multipart/form-data" onsubmit="return  Valid();">
<div class="reg_labels" style="margin-left:100px;">Images <span class='required'>*</span></div>
<div class="reg_textbox"><input  type="file" name="image2[]" multiple="multiple"  style="width:225px; height:20px;" />
</div> 
<div >
<input type="submit" name="add_images" value="Submit"/>
</div></form>

</body>

</html>
<?php
$select=mysql_query("select * from multiple_images");
$image=mysql_fetch_array($select);
?>
<div><a href="edit_images.php?id=<?php echo $image['id']; ?>" class="icon-edit">EDIT</a></div>






PHP Coding:


<?php 
$conn=mysql_connect('localhost','root','');
mysql_select_db('dbname',$conn);
if(isset($_POST['add_images'])){
$gallary_Dir = "product/";
$gallary_Dir_db = "product/";
for ($i = 0; $i < count($_FILES['image2']['name']); $i++) 
{
$ext = substr(strrchr($_FILES['image2']['name'][$i], "."), 1); 
$gal=md5(rand()*time());
$fPath = $gal.".$ext";
$fPath_db = $gallary_Dir_db.$gal.".$ext";
$FILE_PATH[] = $fPath;
$FILE_PATH_db[] = $fPath_db;
$result = move_uploaded_file($_FILES['image2']['tmp_name'][$i],'product/'.$fPath );
}
$gallary_path = (implode(',',$FILE_PATH_db));
$sql = "INSERT INTO `multiple_images`(`images`)VALUES('$gallary_path')";
mysql_query($sql);
echo"<script>alert('Added successfully');</script>";
echo "<script>window.location='add_images.php'</script>";
}
?>

edit_images.php:

Explaination:

=>Pass that id in edit_images and we get the id throuh GET or REQUEST.
=> $_GET['id'];->We have to GET the id and edit the image in edit_images.php.
=>Here also we have to select the image path.
=> <span title="Delete"  onclick="return delete_img('<?php echo $image['id']; ?>','<?php echo $imgs; ?>');">x</span>
    Here in Onclick function we have to use to delete the images.And Pass it into javascript ajax.And we will mentionmed the datas 
    to pass that values to ajax page as delete_images.php.
=>we have to include the libraray files for to use an ajax function and javascript function.

Edit-Form:

<html>
<body>
<form method="post" action="" enctype="multipart/form-data">
<div class="reg_labels" style="margin-left:100px;">Images <span class='required'>*</span></div>
<div class="reg_textbox" style="height:105px;"> <?php  if($image['images']){ ?>

<?php 
$imgrow=explode(',',$image['images']);  
foreach($imgrow as $imgs){
?>
<div>

<div style="float:left; z-index:-10; margin-top:-19px;" >
<img src="<?php echo $imgs;?>"  width="70" height="70" > 
<span title="Delete"  onclick="return delete_img('<?php echo $image['id']; ?>','<?php echo $imgs; ?>');">x</span>
</div> 
</div> 
<?php                                              
}
?>  
</div> 
<?php  }?>
<input  type="file" name="image2[]" multiple="multiple"  />
</div> 
<div class="reg_buttons">
<input type="hidden" name="hid" value="<?php echo $_GET['id'];  ?>"  >
<input type="hidden" name="oldgalimgs" value="<?php  echo $image['images'];  ?>"  >
<input type="submit" class="but_style" name="update_profile" value="Submit" />
</div>
</form>
</body>
</html>

Update PHP Coding:

<?php 
$conn=mysql_connect('localhost','root','');
mysql_select_db('dbname',$conn);
if(isset($_POST['update_profile'])){
$hid=$_POST['hid'];
$galimgs=array();
foreach($_FILES['image2']['name'] as $imgs){
$imgs=trim($imgs);
if($imgs){
$galimgs[]=$imgs;
}
}
if(count($galimgs)>0){
$gallary_Dir = "product/";
$gallary_Dir_db = "product/";
for ($i = 0; $i < count($_FILES['image2']['name']); $i++) 
{
$ext = substr(strrchr($_FILES['image2']['name'][$i], "."), 1); 
$gal=md5(rand()*time());
$fPath = $gal.".$ext";
$fPath_db = $gallary_Dir_db.$gal.".$ext";
$FILE_PATH[] = $fPath;
$FILE_PATH_db[] = $fPath_db;
$result = move_uploaded_file($_FILES['image2']['tmp_name'][$i],'product/'.$fPath);
}
$gallary_path = (implode(',',$FILE_PATH_db));   
$oldgalimgs=$_POST['oldgalimgs'];
if($oldgalimgs!=""){

$newlist=$oldgalimgs.",".$gallary_path;
}else{
$newlist=$gallary_path;
}
$sql ="UPDATE  `multiple_images` SET `images`='$newlist' WHERE id='$hid' ";
mysql_query($sql)or die(mysql_error());

}
echo"<script>alert('Updated successfully');</script>";
echo "<script>window.location=edit_images.php'</script>";
}
$select=mysql_query("select * from multiple_images");
$image=mysql_fetch_array($select);
?>

Javascript Ajax Function:


<link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<script type="text/javascript">

function delete_img(id,img) {
    //alert(id);
        $.ajax({
        type : "POST",
        data : "imgid="+id+"&image="+img+"&action="+"deleteimage",
        url : "delete_images.php",
        success : function(result) {
          window.location.reload();
        }   
    });
}
</script>

delete_images.php:

<?php 
$conn=mysql_connect('localhost','root','');
mysql_select_db('dbname',$conn);

if($_REQUEST['action']=='deleteimage'){

$getimgs=mysql_query("SELECT images FROM multiple_images WHERE id='".$_REQUEST['imgid']."'") or die(mysql_error());
$galrow=mysql_fetch_array($getimgs);

$gallary_image=$galrow['images'];

$imgrow=explode(',',$gallary_image);  

for($i=0;$i<count($imgrow);$i++){

if($imgrow[$i]==$_REQUEST['image']){ 
unset($imgrow[$i]); 
}    
}
$imgstr=implode(',',$imgrow);

mysql_query("UPDATE multiple_images SET images='".$imgstr."' WHERE id='".$_REQUEST['imgid']."'") or die(mysql_error());

}
else
{
mysql_query("DELETE images FROM multiple_images WHERE id='".$_REQUEST['imgid']."'") or die(mysql_error());

}
?>

I hope this tutorial is very helpfull for to upload multiple images.

Friday 17 October 2014

Send Mail using PHPMailer Function

Explaination:

In his tutorial we can send Mail using PHP mailer function.For that first we will
download PHPMailer file and included in ur file where we want to send mail in that place.
Then in that message we want to daisplay the details from Database or statically you can
write your coding in u r message tag.By using this we can use i for to send any confirmatio
mail's to ur coustmer.I hope this tutorial is very useful for ur reference to send ur mail.



Mail Function:

$mail = new PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled

$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465; // or 465
$mail->IsHTML(true);
$mail->Username = "Clientmail";
$mail->Password = "Clientpassword";
$mail->SetFrom("urmail"); 
$mail->FromName = 'Details';
$mail->AddAddress('urmailid');
$mail->Subject = "Give ur Subject";
$mail->Body = $message; 
$mail->IsHTML(true);
$mail->WordWrap = 50;
$done =  $mail->Send();
$mail->ClearAddresses();

PHP Coding:

Here error_reporting(0) is used to delete ur notice errror in ur file.In the username and password is 
to plaace  ur client mail-id and password they will give that you can mentioned there(ie)Email-id and Password for whose mail have to send ur details their mail;id have to given there.







<?php 
error_reporting(0);
include("connect.php");
include('PHPMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled

$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465; // or 465
$mail->IsHTML(true);
$mail->Username = "Clientmail";
$mail->Password = "Clientpassword";
$mail->SetFrom("urmail"); 
$mail->FromName = 'Details';
if(isset($_POST['submit']))
{
$message='<html>
<table width="100%" border="1px solid " cellpadding="10" cellspacing="0" class="top_table">
<tr>
<th>Name</th>
<th>Email-ID</th>
<th>Contact Number</th>
</tr>
<tr>
<td>'.$name.'</td>
<td>'.$email_id.'</td>
<td>'.$mobileno.'</td>
</tr>
</table>
</html>';
}
$mail->AddAddress('urmailid');
$mail->Subject = "Give ur Subject";
$mail->Body = $message; 
$mail->IsHTML(true);
$mail->WordWrap = 50;
$done =  $mail->Send();
$mail->ClearAddresses();


Here error_reporting(0) is used to delete ur notice errror in ur file.In the username and password is 
to plaace  ur client mail-id and password they will give that you can mentioned there(ie)Email-id and
Password for whose mail have to send ur details their mail; id have to given there.

Download PHPMailer Function:CLICK HERE TO DOWNLOAD PHPMAILER FOLDER