tutorial101 is the one place for high quality web development, Web Design and software development tutorials and Resources programming. Learn cutting edge techniques in web development, design and software development, download source components and participate in the community.
article
Showing posts with label web-development (ajax). Show all posts
Showing posts with label web-development (ajax). Show all posts
//details.php
<?php include 'header.php'; ?>
<div class="container">
<p><h1>PHP MySQLi Shopping Cart Using Jquery Ajax</h1></p>
<div id="message"></div>
<div class="row mt-2 pb-3">
<?php
include 'config.php';
$id = $_GET['item'];
$stmt = $conn->prepare('SELECT * FROM product WHERE id=?');
$stmt->bind_param('i',$id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()):
?>
<div class="col-sm-4">
<img src="<?php echo $row['product_image']; ?>">
</div>
<div class="col-sm-8">
<form action="" class="form-submit">
<h4 class="card-title text-info"><?php echo $row['product_name']; ?></h4>
<h5 class="card-text text-danger"> <?php echo number_format($row['product_price'],2); ?></h5>
<div class="row p-2">
<div class="col-md-6 py-1 pl-4">
<b>Quantity : </b>
</div>
<div class="col-md-6">
<input type="number" class="form-control pqty" value="<?php echo $row['product_qty']; ?>">
</div>
</div>
<input type="hidden" class="pid" value="<?php echo $row['id']; ?>">
<input type="hidden" class="pname" value="<?php echo $row['product_name']; ?>">
<input type="hidden" class="pprice" value="<?php echo $row['product_price']; ?>">
<input type="hidden" class="pimage" value="<?php echo $row['product_image']; ?>">
<input type="hidden" class="pcode" value="<?php echo $row['product_code']; ?>">
<button class="btn btn-primary btn-block addItemBtn"><i class="fas fa-cart-plus"></i> Add to cart</button>
</form>
<p><h5>Product Specifications</h5></p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
</div>
<?php endwhile; ?>
</div>
</div>
<?php include 'footer.php'; ?>
action.php
//action.php
<?php
session_start();
require 'config.php';
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
$pname = $_POST['pname'];
$pprice = $_POST['pprice'];
$pimage = $_POST['pimage'];
$pcode = $_POST['pcode'];
$pqty = $_POST['pqty'];
$total_price = $pprice * $pqty;
$stmt = $conn->prepare('SELECT product_code FROM cart WHERE product_code=?');
$stmt->bind_param('s',$pcode);
$stmt->execute();
$res = $stmt->get_result();
$r = $res->fetch_assoc();
$code = $r['product_code'] ?? '';
if (!$code) {
$query = $conn->prepare('INSERT INTO cart (product_name,product_price,product_image,qty,total_price,product_code) VALUES (?,?,?,?,?,?)');
$query->bind_param('ssssss',$pname,$pprice,$pimage,$pqty,$total_price,$pcode);
$query->execute();
echo '<div class="alert alert-success alert-dismissible mt-2">
<strong>Item added to your cart!</strong>
</div>';
} else {
echo '<div class="alert alert-danger alert-dismissible mt-2">
<strong>Item already added to your cart!</strong>
</div>';
}
}
// Get no.of items available in the cart table
if (isset($_GET['cartItem']) && isset($_GET['cartItem']) == 'cart_item') {
$stmt = $conn->prepare('SELECT * FROM cart');
$stmt->execute();
$stmt->store_result();
$rows = $stmt->num_rows;
echo $rows;
}
// Remove single items from cart
if (isset($_GET['remove'])) {
$id = $_GET['remove'];
$stmt = $conn->prepare('DELETE FROM cart WHERE id=?');
$stmt->bind_param('i',$id);
$stmt->execute();
$_SESSION['showAlert'] = 'block';
$_SESSION['message'] = 'Item removed from the cart!';
header('location:cart.php');
}
// Remove all items at once from cart
if (isset($_GET['clear'])) {
$stmt = $conn->prepare('DELETE FROM cart');
$stmt->execute();
$_SESSION['showAlert'] = 'block';
$_SESSION['message'] = 'All Item removed from the cart!';
header('location:cart.php');
}
// Checkout and save customer info in the orders table
if (isset($_POST['action']) && isset($_POST['action']) == 'order') {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$products = $_POST['products'];
$grand_total = $_POST['grand_total'];
$address = $_POST['address'];
$pmode = $_POST['pmode'];
$data = '';
$stmt = $conn->prepare('INSERT INTO orders (name,email,phone,address,pmode,products,amount_paid)VALUES(?,?,?,?,?,?,?)');
$stmt->bind_param('sssssss',$name,$email,$phone,$address,$pmode,$products,$grand_total);
$stmt->execute();
$stmt2 = $conn->prepare('DELETE FROM cart');
$stmt2->execute();
$data .= '<div class="text-center">
<h1 class="display-4 mt-2 text-success">Thank You!</h1>
<h2 class="text-success">Your Order Placed Successfully!</h2>
<h4 class="bg-success text-light rounded p-2">Items Purchased : ' . $products . '</h4>
<h4>Your Name : ' . $name . '</h4>
<h4>Your E-mail : ' . $email . '</h4>
<h4>Your Phone : ' . $phone . '</h4>
<h4>Total Amount Paid : ' . number_format($grand_total,2) . '</h4>
<h4>Payment Mode : ' . $pmode . '</h4>
</div>';
echo $data;
}
?>
update.php
//update.php
<?php
session_start();
require 'config.php';
// Set total price of the product in the cart table
if (isset($_POST['qty'])) {
$qty = $_POST['qty'];
$pid = $_POST['pid'];
$pprice = $_POST['pprice'];
$tprice = $qty * $pprice;
$stmt = $conn->prepare('UPDATE cart SET qty=?, total_price=? WHERE id=?');
$stmt->bind_param('isi',$qty,$tprice,$pid);
$stmt->execute();
}
?>
script.js
//script.js
$(document).ready(function() {
$(".addItemBtn").click(function(e) {
e.preventDefault();
var $form = $(this).closest(".form-submit");
var pid = $form.find(".pid").val();
var pname = $form.find(".pname").val();
var pprice = $form.find(".pprice").val();
var pimage = $form.find(".pimage").val();
var pcode = $form.find(".pcode").val();
var pqty = $form.find(".pqty").val();
$.ajax({
url: 'action.php',
method: 'post',
data: {
pid: pid,
pname: pname,
pprice: pprice,
pqty: pqty,
pimage: pimage,
pcode: pcode
},
success: function(response) {
$("#message").html(response);
window.scrollTo(0, 0);
load_cart_item_number();
}
});
});
// Change the item quantity
$(".itemQty").on('change', function() {
var $el = $(this).closest('tr');
var pid = $el.find(".pid").val();
var pprice = $el.find(".pprice").val();
var qty = $el.find(".itemQty").val(); //alert(qty);
//location.reload(true);
$.ajax({
url: 'update.php',
method: 'post',
cache: false,
data: {
qty: qty,
pid: pid,
pprice: pprice
},
success: function(response) {
console.log(response);
location.reload(true);
}
});
});
$("#placeOrder").submit(function(e) {
e.preventDefault();
$.ajax({
url: 'action.php',
method: 'post',
data: $('form').serialize() + "&action=order",
success: function(response) {
$("#order").html(response);
}
});
});
// Load total no.of items added in the cart and display in the navbar
load_cart_item_number();
function load_cart_item_number() {
$.ajax({
url: 'action.php',
method: 'get',
data: {
cartItem: "cart_item"
},
success: function(response) {
$("#cart-item").html(response);
}
});
}
});
//conn.php
<?php
$conn = new mysqli("localhost", "root", "", "projectdb");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
login.php
//login.php
<?php
include('conn.php');
session_start();
if(isset($_POST['username'])){
$username=$_POST['username'];
$password=md5($_POST['password']);
$query=$conn->query("select * from user where username='$username' and password='$password'");
if ($query->num_rows>0){
$row=$query->fetch_array();
$_SESSION['user']=$row['userid'];
}
else{
?>
<span>Login Failed. User not Found.</span>
<?php
}
}
?>
signup.php
//signup.php
<?php
include('conn.php');
if(isset($_POST['susername'])){
$username=$_POST['susername'];
$password=$_POST['spassword'];
$query=$conn->query("select * from user where username='$username'");
if ($query->num_rows>0){
?>
<span>Username already exist.</span>
<?php
}
elseif (!preg_match("/^[a-zA-Z0-9_]*$/",$username)){
?>
<span style="font-size:11px;">Invalid username. Space & Special Characters not allowed.</span>
<?php
}
elseif (!preg_match("/^[a-zA-Z0-9_]*$/",$password)){
?>
<span style="font-size:11px;">Invalid password. Space & Special Characters not allowed.</span>
<?php
}
else{
$mpassword=md5($password);
$conn->query("insert into user (username, password) values ('$username', '$mpassword')");
?>
<span>Sign up Successful.</span>
<?php
}
}
?>
session.php
//session.php
<?php
session_start();
include('conn.php');
$query=$conn->query("select * from user where userid='".$_SESSION['user']."'");
$row=$query->fetch_array();
$user=$row['username'];
?>