Search This Blog

27 July 2018

PHP using Shopping Cart

PHP Using Shopping Cart


Index.php:

  <!DOCTYPE html>
<html>
<head>
<style>
.overlay {
  background: #304352;
  opacity: 0.4;
  position: absolute;
 right: 0;
}
.overlay1 {
  background: #fff;
  opacity: 0.4;
  position: absolute;

  top: 0;
  left: 0;
  right: 0;

}
</style>
<title> Admin Panel </title>
</head>
<script src="js/jquery.js"> </script>
<link rel="stylesheet" type="text/css" href=" css/bootstrap.min.css">
<body>
<div class="container">
<div class="row">
<center> <h1> Shopping Cart </h1> </center>
<div class="col-md-12">
<ul type="none">
<?php $con=mysqli_connect("localhost","root","","test");
   $sql=" select * from product ";
   $res=$con->query($sql);
   while($row= mysqli_fetch_array($res)){
?>
<li >
<div class="col-md-3" >
<img width="100%" src="images/<?php echo $row['image']; ?>" >
<center>
<h4> <?php echo $row['pname']; ?> </h4>
<h4> <?php echo $row['pdesc']; ?> </h4>
<h4> <?php echo "$".$row['price']; ?> </h4>
<a href="index.php?id=<?php echo $row['id']; ?>" class="btn btn-danger"> Add cart </a>
</center>
</div>
</li><?php } ?>
</ul><br>
<h4><a href="show.php" target="_blank">Show cart</a></h4>
</div>
</div>
</div>
</body>
</html>
<?php
session_start();

if(empty($_SESSION['cart']))
$_SESSION['cart']=array();
if(isset($_GET['id'])){
array_push($_SESSION['cart'],@$_GET['id']);
}
?>

Show Product:
<?php
$con=mysqli_connect("localhost","root","","test");

if(!isset($_SESSION['cart']) or $_SESSION['cart']==NULL)
echo " No product";
else{
$id=implode(',',$_SESSION['cart']);
$sql=" select * from product where id in($id) ";
$res=$con->query($sql);
echo "<table class='table table-hover'><tr>
<th> Id </th> <th> name </th> <th> Description </th><th> Image </th> <th> Quantity </th> <th> price </th>
</tr>";
$price=0;
while($row=mysqli_fetch_array($res)){
$price+=$row['price']*$row['quantity'];
echo "

<tr>
<td> ".$row['id']." </td>
<td> ".$row['pname']." </td>
<td> ".$row['pdesc']." </td>
<td> <img  width='18%' src='images/".$row['image']."' </td>
<td> <input style='width:50px;' value='".$row['quantity']."' type='text' onkeyup='inc(".$row['id'].")' name='qty[]' id='qty".$row['id']."'></td>
<td id='s".$row['id']."'>".$row['price']*$row['quantity']." </td>
<td> <a href='?rm=".$row['id']."'> Remove </a> </td>
</tr>
";



}echo " <tr>
<td colspan='5'>
Total
</td>
<td> ".$price." </td>
</tr>

</table> <br>";

echo " <h4> <a href='?all'> remove all </a> </h4>";

}

Remove Product :


<?php     //Remove All
session_start();
if(isset($_GET['all'])){
unset($_SESSION['cart']);
}


else if (isset($_GET['rm'])){  //Remove Particular cart
foreach ($_SESSION['cart'] as $key => $pro) {
if($pro==$_GET['rm']){
unset($_SESSION['cart'][$key]);
}

}
}
?>

Increase Quantity:

<script type="text/javascript">
function inc(e){
var qty=document.getElementById('qty'+e).value;
$.post("inc.php",{id:e,qty:qty},function(data){
document.getElementById('s'+e).innerHTML=data;
});
}

</script>


<?php
session_start();
$con=mysqli_connect("localhost","root","","test");
if(isset($_POST['id'])){
$id=$_POST['id'];
$qty=$_POST['qty'];
$sql=" update product set quantity='$qty' where id='$id' ";
$con->query($sql);
$sql=" select * from product where id='$id' ";
$res=$con->query($sql);
while($row=mysqli_fetch_array($res)){
$price=$row['quantity']*$row['price'];
echo $_SESSION['price']=$price;
}
}

?>

# 3 Files 

1.index.php
2.show.php
3.inc.php


Output:
















26 July 2018

Javascript Validation

JavaScript Form Validation

<!DOCTYPE html>
<html>
<head>
<title>Javascript Validation</title>
</head>
<style type="text/css">
.inpt {
    padding: 10px;
    width: 290px;
    border-radius: 4px;
    border: 1px solid #a59c9c;
    font-size: 16px;
    box-sizing: content-box;
}
input[type="button"] {
    padding: 8px;
    width: 8%;
    border-radius: 4px;
    border: 1px solid #c5bfbf;
    margin-left: 4%;
}
</style>
<script type="text/javascript">
var frm=document.getElementById('frm');
var sub=document.getElementById('sub');

 function empty(e){
        user = document.getElementById("unm").value;
    pwd = document.getElementById("pass").value;
 
    if(user=="" && pwd==""){
        alert('username and password is Empty');
      event.preventDefault();
     
    }else{
     
    }
}
function ckdate(){
    var dt=document.getElementById('dt').value;
    var today = new Date();
    var y = today.getFullYear();                     
    var m = ("0" + (today.getMonth() + 1)).slice(-2);
    var dy = ("0" + today.getDate()).slice(-2); 
    var cur=y+"-"+m+"-"+dy;
     if(cur < dt){
       alert('NOT select Future date');
       event.preventDefault();
       document.getElementById('dt').value='';
    }else{
       
    }
}



function val(e){
var fname=document.getElementById('fname').value;
var lname=document.getElementById('lname').value;
var email=document.getElementById('email').value;
var pwd=document.getElementById('pwd').value;
var cpwd=document.getElementById('cpwd').value;
var pno=document.getElementById('pno').value;





if(fname==""){
document.getElementById('e1').innerHTML="Enter Your First Name";
document.getElementById('e1').style.color="red";
}else{
document.getElementById('e1').innerHTML="";
}
if (lname=="") {document.getElementById('e2').innerHTML="Enter Your Last Name";
document.getElementById('e2').style.color="red";}
else{
document.getElementById('e2').innerHTML="";
}
if(email==""){document.getElementById('e3').innerHTML="Enter Your Email";
document.getElementById('e3').style.color="red";}
else{
document.getElementById('e3').innerHTML="";
}
if(pwd==""){document.getElementById('e4').innerHTML="Enter Your Password";
document.getElementById('e4').style.color="red";}
else{
document.getElementById('e4').innerHTML="";
}
if(cpwd==""){document.getElementById('e5').innerHTML="Enter Your Confirm Password";
document.getElementById('e5').style.color="red";}
else{
document.getElementById('e5').innerHTML="";
}
if(pno==""){document.getElementById('e6').innerHTML="Enter Your phone Number";
document.getElementById('e6').style.color="red";}
else{
document.getElementById('e6').innerHTML="";
}

}

function fnm(){
var fname=document.getElementById('fname').value;
if(fname.length > 10){
document.getElementById('e1').innerHTML="The Character is less then 10";
document.getElementById('e1').style.color="red";
}else{
document.getElementById('e1').innerHTML="";
}
}
function lnm(){
var lname=document.getElementById('lname').value;
if(lname.length > 10){
document.getElementById('e2').innerHTML="The Character is less then 10";
document.getElementById('e2').style.color="red";
}else{
document.getElementById('e2').innerHTML="";
}
}

function passwd(){
var pwd=document.getElementById('pwd').value;
if(pwd.length < 8){
document.getElementById('e4').innerHTML="Password is greater then 8";
document.getElementById('e4').style.color="red";
}else{
document.getElementById('e4').innerHTML="";
}
}
function cnfrm(){
var pwd=document.getElementById('pwd').value;
var cpwd=document.getElementById('cpwd').value;
if(pwd==cpwd){
 document.getElementById('e5').innerHTML="";
}else{
     document.getElementById('e5').innerHTML="Password is NOT Mached";
document.getElementById('e5').style.color="red";
//document.getElementById('cpwd').value='';
document.getElementById('cpwd').focus();
}
}

function phone(){
var pno=document.getElementById('pno').value;
if(pno.length > 10){
document.getElementById('e6').innerHTML="Phone Number is max 10";
document.getElementById('e6').style.color="red";
}else{
document.getElementById('e6').innerHTML="";
}
}


</script>
<body>
<center><h1>Javascript Form Validation</h1></center>
<center>
<form id="frm" >
<table>
<tr><td>First Name</td>
<td><input class="inpt" onkeyup="fnm()"  placeholder="Enter First name" type="text" id="fname"></td>
<td><span id="e1"></span></td></tr>
<tr><td>Last Name</td>
<td><input class="inpt" onkeyup="lnm()" placeholder="Enter Last name" type="text" id="lname"></td>
<td><span id="e2"></span></td</tr>
<tr><td>Email</td>
<td><input class="inpt" placeholder="Enter Email" type="text" id="email"></td>
<td><span id="e3"></span></td</tr>
<tr><td>Password</td>
<td><input class="inpt" onkeyup="passwd()" placeholder="Enter Password" type="password" id="pwd"></td>
<td><span id="e4"></span></td</tr>
<tr><td>Confirm Password</td>
<td><input class="inpt" onchange="cnfrm()" placeholder="Enter Confirm Passowrd" type="password" id="cpwd"></td>
<td><span id="e5"></span></td</tr>
<tr><td>Phone Number</td>
<td><input class="inpt" onkeyup="phone()" placeholder="Enter Phone Number" type="text" id="pno"></td>
<td><span id="e6"></span></td</tr>
</table><br>
<input type="button" id="sub" onclick="val(event)"  value="check">
</form>
</center>

</body>
</html>






Jquery or Javascript Start Exam Time

 <script> function startTimer() {      var date = "<?php echo $date ?>"; // dynamic date      var time = "<?...