Insert records to mysql database with php using Ajax - php

I am trying to added record using a modal. I have use the following code but my record is not added into the database.. help me through pls
<link rel="stylesheet" type="text/css" href="/Project/Style/bootstrap.css"/>
<script src="Style/jQuery/jquery-1.11.1.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<?php
include('header.php');
require_once('Connections/conn.php');
$sql2= "SELECT * from type where cid='1' ";
$results =mysqli_query($conn,$sql2);
?>
<div id="main">
<div class="container" >
<div class="row">
<div class="form-group col-md-3">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
++Add Farm Product
</button>
</div>
<div class="input-group col-md-4 col-sm-offset-2">
<span class="input-group-addon">Search</span>
<input type="text" class="form-control" name="search-text" id="search-text" placeholder="Search By Product Name">
</div>
</div>
<div id="result"></div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Add Farm Product</h4>
</div>
<div class="modal-body">
<form>
<div class="row">
<div class="col-md-6 padding-top-10">
<label for="proname" class="control-label">Product Name:</label>
<input type="text" class="form-control" id="proname" placeholder="Product Name" name="proname" />
</div>
<div class="col-md-5 padding-top-10">
<label for="pro-type" class="control-label">Product Type:</label>
<select class="form-control" id="pro_type" name="pro_type" >
<option>Select Product type...</option>
<?php foreach($results as $result): ?>
<option value= " <?php echo $result['tid']; ?>" > <?php echo $result['type_name']; ?>
</option>
<?php endforeach ;?>
</select>
</div>
</div><br/>
<div class="row">
<div class="col-md-4 padding-top-10">
<label for="qty" class="control-label">Quantity:</label>
<input type="text" class="form-control" id="qty" placeholder="Quantity" name="qty" />
</div>
<div class="col-md-4 padding-top-10">
<label for="price" class="control-label">Unit Price :</label>
<input type="text" class="form-control" id="price" placeholder="Unit Price" name="price" />
</div>
<div class="col-md-4 padding-top-10">
<label for="pdate" class="control-label"> Production Date :</label>
<input type="date" class="form-control" id="pdate" placeholder="Production Date" name="pdate" />
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" >Close</button>
<button type="button" id="add" name="add" class="btn btn-primary">Add Product</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<script>
// haddling the post for adding record
$('#add').click(function(){
var proname = $('#proname').val();
var pro_type = $('#pro_type').val();
var qty = $('#qty').val();
var price = $('#price').val();
var pdate = $('#pdate').val();
var datas="proname="+proname+"&pro_type="+pro_type+"&qty="+qty+"&price="+price+"&pdate"+pdate;
$.ajax({
type: "POST",
url: "addpro_exec.php",
data: datas,
success:function(data){
},
error: function(){
}
});
});
</script>
The code handling the posted data is below
//addpro_exec.php file
<?php
require_once('Connections/conn.php');
if(isset($_POST['add'])){
$proname=$_POST['proname'];
$pro_type= $_POST['pro_type'];
//$typname=$_POST['type_name'];
$qty= $_POST['qty'];
$price= $_POST['price'];
$pdate= $_POST['pdate'];
$asdate = \date('F');
$sql="INSERT into product VALUES('','$proname','','$qty','$price','$pdate','','$pro_type','1') ON DUPLICATE KEY UPDATE qty = qty+$qty";
$sql2= "INSERT into year_estm VALUES('','$proname','$asdate','$qty','') ON DUPLICATE KEY update Production_input=Production_input+$qty ";
$AddProduction= mysqli_query($conn, $sql2) or die('Cannot add to production'. mysqli_error($conn));
$RecAdd= mysqli_query($conn,$sql) or die('Cannot add Product' . mysqli_error());
if($RecAdd){
header('location:viewfarm.php');
}
}
else{
}
?>

Try the following line in your javascript:
var datas="add=1&proname="+proname+"&pro_type="+pro_type+"&qty="+qty+"&price="+price+"&pdate="+pdate;
That should fix the problem.
WARNING: Always filter and a properly quote user input before inserting into a database in order to avoid SQL injection hacks. Use prepared statements to help achieve this.
http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

Related

How to Update Mysql Data with a button on Home Page that is passing id on URL?

I have created an Edit Item popup Bootstrap modal on home page. Now what I want is to update the selected item details using the PHP.
Here i am passing item id through the URL in order to select the item
Now when I press edit Button it opens a Popup with all the details in it( already filled) but when I try to click on Save Changes Button it just do nothing and My data not updated. I have also used POST method but its not working.
Here is the Button that opens the Popup Modal
<?php
$res= mysqli_query($db,"SELECT* FROM `books`;");
if(mysqli_num_rows($res)>0){
while($row= mysqli_fetch_assoc($res)){
if($row!=0)
{
$bid= $row['book_id'];
?>
<ul>
<li>
<a onclick="$('#editModal<?php echo $row['book_id']?>').modal('show');" class="btn-show-modal edit-btn" data-toggle="modal"><i style="padding-right: 140px;" class="fas fa-edit"></i></a>
</li>
<!--Here is the Popup Modal for Edit -->
<div id="editModal<?php echo $row['book_id']?>" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit This Book</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="updatebook.php" method="POST">
<div class="form-group">
<label for="isbn" class="col-form-label">ISBN</label>
<input type="text" class="form-control" name="isbn" value="<?php echo $row['isbn'];?>">
</div>
<div class="form-group">
<label for="title" class="col-form-label">Enter Book Title</label>
<input type="text" class="form-control" name="title" value="<?php echo $row['title'];?>">
</div>
<div class="form-group">
<label for="author" class="col-form-label">Enter Author Name</label>
<input type="text" class="form-control" name="author" value="<?php echo $row['author'];?>">
</div>
<div class="form-group">
<label for="image" class="col-form-label">Image URL</label>
<input type="text" class="form-control" name="image" value="<?php echo $row['image'];?>">
</div>
<div class="form-group">
<label for="description" class="col-form-label">Enter Book Description</label>
<textarea class="form-control" name="description" value="<?php echo $row['description'];?>"></textarea>
</div>
<div class="form-group">
<label for="url" class="col-form-label">Book URL</label>
<input type="text" class="form-control" name="url" value="<?php echo $row['url'];?>"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" name="save">Save Changes</button>
</div>
</div>
</div>
</div>
<?php
}
}
}
else
{
echo"<h1 style='color: white;font-size:58px; font-family: Poppins; font-weight: 600;' class=m-2>U!Oh Nothing Found Here!</h1>
<button onclick=\"window.location.href='add_books_form.php'\" class=\"btn btn-info bg-primary m-5\">Contribute</button>";
}
?>
</ul>
Here is the updatebook.php Page
<?php
include "connection.inc.php";
if(isset($_POST['save']))
{
$id= $_POST['id'];
$isbn= $_POST['isbn'];
$title= $_POST['title'];
$author= $_POST['author'];
$image= $_POST['image'];
$desc= $_POST['description'];
$url= $_POST['url'];
$res = mysqli_query($db, "UPDATE books SET isbn= '$isbn', title= '$title', author= '$author',image= '$image', description= '$desc', url= '$url' WHERE book_id= '$id'");
?>
if($res)
{
<script type="text/javascript">
alert("Data Updated Successfully");
window.location.href= "home.php";
</script>
}
else
{
<script>
alert("Not Updated!");
window.location.href="home.php";
</script>
}
<?php
}
?>
Please Help me to solve this Update Issue i am facing for a long time. I have researched all through but didn't got any solution.
Change your Save button type to "submit" and remove the <a> hyperlink.
Use <form> as a part of both modal-body and modal-footer so the submit button will be part of it.
Take book_id in a hidden field so it will be the part of POST.
Look at the modal code below:
<div id="editModal<?php echo $row['book_id']?>" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit This Book</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="updatebook.php" method="POST">
<input type="hidden" name="id" value="<?php echo $row['book_id'];?>">
<div class="modal-body">
<div class="form-group">
<label for="isbn" class="col-form-label">ISBN</label>
<input type="text" class="form-control" name="isbn" value="<?php echo $row['isbn'];?>">
</div>
<div class="form-group">
<label for="title" class="col-form-label">Enter Book Title</label>
<input type="text" class="form-control" name="title" value="<?php echo $row['title'];?>">
</div>
<div class="form-group">
<label for="author" class="col-form-label">Enter Author Name</label>
<input type="text" class="form-control" name="author" value="<?php echo $row['author'];?>">
</div>
<div class="form-group">
<label for="image" class="col-form-label">Image URL</label>
<input type="text" class="form-control" name="image" value="<?php echo $row['image'];?>">
</div>
<div class="form-group">
<label for="description" class="col-form-label">Enter Book Description</label>
<textarea class="form-control" name="description" value="<?php echo $row['description'];?>"></textarea>
</div>
<div class="form-group">
<label for="url" class="col-form-label">Book URL</label>
<input type="text" class="form-control" name="url" value="<?php echo $row['url'];?>"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="save">Save Changes</button>
</div>
</form>
</div>
</div>
</div>

Add ckeditors fields dynamically in modal

i have a cms where on click open modal and inside i have few fields and then i have another button inside of modal where i can add dynamically ckeditors fields by clicking on button, here is what i was trying to do:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="col-md-12">
<button style="margin-bottom:5px;" type="button" data-toggle="modal" data-target="#add_data_Modal" class="btn btn-success"><i class="fa fa-plus" aria-hidden="true"></i> Adauga</button>
</div>
<div id="add_data_Modal" class="modal fade" role="dialog">
<div class="modal-dialog modal-lg">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Adauga categorie</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="container">
<form action="add.php" method="POST">
<div class="form-group">
<label for="input-1">Nume Categorie</label>
<input type="text" class="form-control" name="nume" id="nume" placeholder="Nume categorie">
</div>
<div class="form-group">
<label for="input-1">Categorie</label>
<select class="form-control" id="categorie" name="categorie">
<option value="0">Categorie principala</option>
</select>
</div>
<div class="form-group">
<label for="input-1">Vizibil</label>
<select class="form-control" id="activ" name="activ">
<option selected>Selecteaza vizibilitatea</option>
<option value="1">Da</option>
<option value="0">Nu</option>
</select>
</div>
<div class="form-group">
<label for="input-1">Pozitia</label>
<input type="text" class="form-control" name="pozitie" id="pozitie" placeholder="Pozitie">
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<div class="icheck-material-primary">
<input type="checkbox" id="user-checkbox1" checked="">
<label for="user-checkbox1">Modele Produse</label>
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<div class="icheck-material-primary">
<input type="checkbox" id="user-checkbox2" checked="">
<label for="user-checkbox2">Paletar Materiale</label>
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<div class="icheck-material-primary">
<input type="checkbox" id="user-checkbox3" checked="">
<label for="user-checkbox3">Estimare Pret</label>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="rowa">
<div class="add-plus">Adauga tab-uri</div>
<div class="col-md-12">
<span id="add_aut" class="btn btn-primary">+</span>
<div id="auth-rows"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal"><i class="fa fa-times"></i> Inchide</button>
<button type="submit" name="submit" class="btn btn-primary"><i class="fa fa-check-square-o"></i> Salveaza</button>
</div>
</form>
</div>
</div>
</div>
<script>
$(document).ready(function(e){
// variable
var html3 = '<div class="row" id="parent-autor"> <p id="auth-del"><i class="fa fa-minus-square" aria-hidden="true"></i></p> <div class="row"> <div class="col-md-4"> <div class="form-group"> <input type="text" class="form-control" placeholder="titlu_tab" name="titlu_tab[]"> </div> </div> <div class="col-md-4"> <div class="form-group"> <input type="text" class="form-control" placeholder="Pozitie" name="pozitie_tab[]"> </div> </div> </div> <div class="col-md-12"> <div class="form-group"> <textarea name="editor[]" class="ckeditor" id="continut" rows="8" cols="80"></textarea> </div> </div></div>';
$("#add_aut").click(function(e){
$('#auth-rows').append(html3);
});
$('#auth-rows').on('click', '#auth-del', function(E){
$(this).parent('div').remove();
});
});
</script>
In this modal when i click on add i want to add more fields, is done this but not appear ckeditor just a plain textarea which i don`t need that.
Thank you!
You are probably going to want to give the textarea a custom id if you plan to have multiple of them.
let currentId = 0;
I moved the 'var html' code inside the create function so that we can edit the id every time the add button is clicked because we need a unique id each time. After the textarea exists, I call .ckeditor() on that textarea which should enable the ckeditor tools for that field. Then I increment the customId to make sure that the next form that is added has a different id.
$("#add_aut").click(function(e){
var html3 = '<div class="row" id="parent-autor"> <p id="auth-del"><i class="fa fa-minus-square" aria-hidden="true"></i></p> <div class="row"> <div class="col-md-4"> <div class="form-group"> <input type="text" class="form-control" placeholder="titlu_tab" name="titlu_tab[]"> </div> </div> <div class="col-md-4"> <div class="form-group"> <input type="text" class="form-control" placeholder="Pozitie" name="pozitie_tab[]"> </div> </div> </div> <div class="col-md-12"> <div class="form-group"> <textarea name="editor[]" class="ckeditor" id="continut' + currentId + '" rows="8" cols="80"></textarea> </div> </div></div>';
$('#auth-rows').append(html3);
$('#continut' + currentId).ckeditor();
currentId += 1;
});
You may not want the customId in the id of the textarea depending on what you are trying to accomplish, but this should get you going in the correct direction.
Also, you need to add the ckeditor.js as well as a jquery adapter script for this to work.
See this fiddle https://jsfiddle.net/bkqxnu8f/3/

how to set value in input type = file from database

<div class="modal fade validate" id="modal-6">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Edit Advertisement</h4>
<label class="idhere"><?php echo $id; ?></label>
</div>
<?php $sel = $d->select('advertisements', 'ad_id=12');
while ($result = mysqli_fetch_assoc($sel)) {
?>
<div class="modal-body">
<div class="row">
<div class="form-group col-md-6">
<label class="control-label">Image Upload</label>
<div>
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new thumbnail" style="width: 200px; height: 150px;" data-trigger="fileinput">
<img src="<?php echo $result['photo']; ?>">
</div>
<div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px"></div>
<div>
<span class="btn btn-white btn-file">
<span class="fileinput-new">Select image</span>
<span class="fileinput-exists">Change</span>
<input type="file" name="advertise_photo" accept="image/*">
</span>
Remove
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info">Send</button>
<button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
</div>
<?php } ?>
</div>
</div>
</div>
I am unable to set the value of input type=file from database,
and if the image is exits then change button appear instead of select image.
when i am choosing new file it works properly,and change and remove button appear instead of select file.
Please help me to solve it.
Note: Above question is continuous of this question so the following answer is also continuous of this answer
You can skip the <label class="idhere"><?php echo $id;?></label>
in JS what yo need is ajax call method to fetch data from database against <?php $id;?> and display in modal
$(document).ready(function() {
$('#modal-6').on('shown.bs.modal', function(e) {
var id = $(e.relatedTarget).data('id');
$.ajax({
type : 'post',
url : 'file.php', //Here you will fetch records or images
data : 'id='+ id, //Pass id
success : function(data){
$('#fetched-data').html(data);//Show fetched data from database
}
});
});
});
Modal HTML will be
<div class="modal fade validate" id="modal-6">
<div class="modal-dialog">
<div class="modal-content">
<div id="fetched-data"></div>
</div>
</div>
</div>
and file.php will be
<?php
//database connection
if($_POST['id']) {
$id = $_POST['id']; //escape the string
//run query
//fetch data from database
//poplutae the HTML with values
?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Edit Advertisement</h4>
</div>
<?php $sel = $d->select('advertisements','ad_id=12');
while($result = mysqli_fetch_assoc($sel)){
?>
<div class="modal-body">
<div class="row">
<div class="form-group col-md-6">
<label class="control-label">Title</label>
<input class="form-control" value="<?php echo $result['title'];?>" name="advertise_title" data-validate="required" data-message-required="Please Enter Title" placeholder="Enter Title" type="text">
</div>
<div class="form-group col-md-6">
<label class="control-label">URL</label>
<input class="form-control" name="advertise_url" value="<?php echo $result['url'];?>" data-validate="required" data-message-required="Please Enter URL" placeholder="Enter URL" type="text">
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group no-margin">
<label for="description" class="control-label">Description</label>
<textarea class="form-control autogrow" id="description" name="advertise_desc" placeholder="Describe Description Regarding Query">
<?php echo trim($result['description']); ?>
</textarea>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label class="control-label">Image Upload</label>
<div>
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new thumbnail" style="width: 200px; height: 150px;" data-trigger="fileinput">
<img src="<?php echo $result['photo']; ?>">
<!-- <img src="images/thumbnail.jpeg" alt="..."> -->
</div>
<div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px"></div>
<div>
<span class="btn btn-white btn-file">
<span class="fileinput-new">Select image</span>
<span class="fileinput-exists">Change</span>
<input type="file" name="advertise_photo" accept="image/*" >
</span>
Remove
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label class="control-label">Active Flag</label>
<input type="checkbox">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info">Send</button>
<button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
</div>
<?php } } ?>
As suggested by #devpro you need <form></form> to edit / update the populated data inside modal.

Only 1 item stored in my database

checkout.php
...............
I use this code it. When i buy 2 or more items, only 1 item stored in my database. thanks in advance...................
<?php
session_start();
include('conn.php');
require('item.php');
if(isset($_POST['confirm'])){
$order=$_POST['order'];
$date = date('Y-m-d H:i:s');
$lname=$_POST['lname'];
$fname=$_POST['fname'];
$mname=$_POST['mname'];
$add=$_POST['add'];
$em=$_POST['email'];
$num=$_POST['number'];
if(empty($order)){
echo"<script>alert('Please choose your payment method.')</script>";
}
mysqli_query($con,"INSERT INTO `godgets`.`order` (`Lastname`, `Firstname`, `Middlename`, `Address`, `Email`, `Number`, `Date`) VALUES ('$lname', '$fname', '$mname', '$add', '$em', '$num', '$date')");
$orderid=mysqli_insert_id($con);
$cart=unserialize(serialize($_SESSION['cart']));
for ($i=0; $i<count($cart); $i++) {
$p_name=$cart[$i]->name;
$p_price=$cart[$i]->price;
$p_des=$cart[$i]->des;
$p_quantity=$cart[$i]->quantity;
$p_image=$cart[$i]->image;
$p_total=$cart[$i]->price * $cart[$i]->quantity;
mysqli_query($con,"INSERT INTO order_details (`ORDER_ID`, `P_name`, `P_price`, `P_quantity`, `Picture`,`Total`) VALUES
('.$orderid.', '$p_name.', '.$p_price.', '.$p_quantity.', '.$p_image.','.$p_total.')");
}
echo"<script>alert('Thankyou For Shopping!')</script>";
echo"<script>window.open('index.php','_self')</script>";
unset($_SESSION['cart']);
exit();
}
?>
help me please i`m a newbie :(
tnx
fds
item.php
<?php
class Item{
var $id;
var $name;
var $des;
var $quantity;
var $price;
var $image;
}
?>
addcart.php
<?php
session_start();
?>
<head>
<meta name="viewport" content="width=device-width, initial- scale=1">
<link rel="stylesheet" type="text/css" href="css\bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css\bootstrapValidator.min.css">
<script src="jq/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrapValidator.min.js"></script>
</head>
<body background="pic/bg3.jpg" style="background-attachment:fixed;background-size:cover;">
<?php include("navbar.php");
require('conn.php');
require('item.php');
if(!isset($_SESSION['username'])){
echo"<script>window.open('sign.php','_self')</script>";
exit();
}
if(isset($_GET['id']) && !isset($_POST['update'])){
$result=mysql_query('select * from products_db where P_ID ='.$_GET['id']);
$product=mysql_fetch_object($result);
$item= new Item();
$item->id= $product->P_ID;
$item->name=$product->P_NAME;
$item->des=$product->DESCRIPTION;
$item->price=$product->P_PRICE;
$item->image=$product->PICTURE;
$item->quantity=1;
//check product
$index=-1;
$cart=unserialize(serialize($_SESSION['cart']));
for($i=0; $i<count($cart);$i++)
if ($cart[$i]->id==$_GET['id']){
$index=$i;
break;
}
if($index==-1){
$_SESSION['cart'][]=$item;
}
else{
$_SESSION['cart']=$cart;
}
}
//delete
if(isset($_GET['index']) && !isset($_POST['update'])){
$cart=unserialize(serialize($_SESSION['cart']));
unset($cart[$_GET['index']]);
$cart=array_values($cart);
$_SESSION['cart']=$cart;
}
if (isset($_POST['update'])){
$arrQuantity=$_POST['quantity'];
// check quantity
$valid=1;
for($i=0; $i<count($arrQuantity);$i++)
if(!is_numeric($arrQuantity[$i]) || $arrQuantity[$i]< 1){
$valid=0;
break;
}
if($valid==1){
$cart=unserialize(serialize($_SESSION['cart']));
for($i=0; $i<count($cart);$i++){
$cart[$i]->quantity=$arrQuantity[$i];
}
$_SESSION['cart']=$cart;
}
else
$error='Quantity is Invalid';
}
?>
<div class="container">
<div class="row">
<div class="col-md-12 col-xs-12">
<div class="panel panel-info">
<div class="panel-heading">
<form method="POST">
<div class="panel-title">
<h5><span class="glyphicon glyphicon-shopping-cart" style="float:left;"></span> YOUR ITEM(s)
<label style="color:red;margin-left:400px;"><?php echo isset($error) ? $error : '';?></label>
<span class="glyphicon glyphicon-share-alt"></span> Continue Shopping
<button type="submit" name="update" style="float:right; margin-right:3%;"class="btn btn-info btn-sm "><span class="glyphicon glyphicon-floppy-saved"></span>Update Cart</button>
</h5>
</div>
</div>
<?php
$cart=unserialize(serialize($_SESSION['cart']));
$s=0;
$index=0;
for ($i=0;$i<count($cart); $i++){
$s+=$cart[$i]->price * $cart[$i]->quantity;
?>
<div class="panel-body">
<div class="row">
<div class="col-md-2 col-xs-12">
<img class="img-responsive" src="<?php echo $cart[$i]->image;?>" style="height:30%;width:100%;">
</div>
<div class="col-md-4 col-xs-12">
<h4 style="font-family:agency fb;"><strong><?php echo $cart[$i]->name;?></strong></h4>
<div><h4 style="font-family:agency fb;"><small><?php echo $cart[$i]->des;?></small></h4></div>
</div>
<div class="col-md-6 col-xs-12">
<div class="col-md-6 text-right">
<h4 style="font-family:agency fb;"><strong>Php <?php echo $cart[$i]->price;?></strong> x </h4>
</div>
<div class="col-md-4 col-xs-9">
<input type="text" name="quantity[]" value="<?php echo $cart[$i]->quantity;?>" class="form-control" style="width:50px;">
</div>
<div class="col-md-3 col-xs-2" style="margin-top:1%;">
<span class="glyphicon glyphicon-trash"></span>
</div>
</div>
</div>
</div>
<hr class="divider">
<?php
$index++;
}
?>
</form>
<div class="panel-footer" style="height:9%;">
<div>Empty Cart</div>
<div>Checkout</div>
<div style="float:right;margin-right:2%;font-family:agency fb;"><p class="form-control"><b>Total</b> Php: <?php echo $s;?></p></div>
</div>
</div>
</div>
</div>
</div>
<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header" style="background-color:#cceaf6;">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel" style="font-family:agency fb;font-size:30px;">Costumer`s Information</h4>
</div>
<form method="POST" action="checkout.php">
<div class="modal-body">
<div class="form-group">
<div class="row col-md-12">
<div class="container inline col-md-4" >
<input type="radio" class="form-control" name="order" onclick="showhidediv(this);" value="paypal">
<img src="pic/paypal.jpg" style="width:200px;height:100px;">
</div>
<div class="col-md-4">
<center><span style="font-family:agency fb; font-size: 50px;">or</span></center>
</div>
<div class="col-md-4">
<input type="radio" class="form-control" name="order" onclick="showhidediv(this);" value="Cash On Delivery">
<img src="pic/cash.png" style="width:200px;height:100px;">
</div>
</div>
<div class="form-inline">
<input type="text" class="form-control" name="lname" placeholder="Lastname" required>
<input type="text" class="form-control" name="fname" placeholder="Firstname"required>
<input type="text" class="form-control" name="mname" placeholder="Middlename"required>
</div>
<br>
<input type="text" class="form-control" name="add" placeholder="Complete Address" style="width:596px;"required>
<br>
<input type="email" class="form-control" name="email" placeholder="Email Address"style="width:596px;"required>
<br>
<input type="text" class="form-control" name="number" placeholder="Contact Number"style="width:196px;" required>
<br>
<div id="one" style="display:none;">
<div style="background-color:#cceaf6;"><p>Bill to another address. (Optional)</p></div>
<div class="form-inline">
<input type="text" class="form-control" name="lname2" placeholder="Lastname">
<input type="text" class="form-control" name="fname2" placeholder="Firstname">
<input type="text" class="form-control" name="mname2" placeholder="Middlename">
</div>
<br>
<input type="text" class="form-control" name="add2" placeholder="Complete Address" style="width:596px;">
<br>
<input type="email" class="form-control" name="email2" placeholder="Email Address"style="width:596px;">
<br>
<input type="text" class="form-control" name="number2" placeholder="Contact Number"style="width:196px;">
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" name="confirm" class="btn btn-success" style="font-family:agency fb;font-size:15px;width:100px;">OK</button>
</div>
</form>
</div>
</div>
</body>
<script>
function goBack() {
window.history.back();
}
function showhidediv( rad )
{
var rads = document.getElementsByName( rad.name );
document.getElementById( 'one' ).style.display = ( rads[1].checked ) ? 'block' : 'none';
}
</script>
Error
only 1 item stored.
Hi you need to correct this: '.$cart[$i]->image.'
for ($i=0; $i<count($cart); $i++) {
mysql_query("INSERT INTO order_details VALUES ('.$orderid.', '.$cart[$i]->name.', '.$cart[$i]->price.', '.$cart[$i]->quantity.', '.$cart[$i]->image.', '.$cart[$i]->price * $cart[$i]->quantity.','.$order.')");
}
Also please highlight the error line in the code next time you ask such question.

PHP & Bootstrap - Modal Shown just before content updated

In the database I have a table called articles. On one page of my website there is a list of titles of all articles - and each title is actually a link to edit the article. It looks like this:
<a onclick="launch_edit_modal(<?php echo $article->getId(); ?>)" data-toggle="modal" data-target="#edit_modal" href="#">Edit</a>
When you click on the link - function launch_edit_modal(id_of_article) is triggered:
<script type="text/javascript">
$('#edit_modal').modal({
show: false
});
function launch_edit_modal(id) {
$.ajax({
type: "POST",
url: "return_article_data.php",
data: {
id:id
},
dataType: "json",
success: function(data) {
$("#category").val(data.category_id);
$("#title").val(data.title);
$("#content").val(data.content);
$('#edit_modal').show();
}
});
}
</script>
This script is at the bottom.
So, using AJAX I find the other data (category, title, content) about that article and then I update the form which is modal's content:
<div class="modal fade" id="edit_modal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span>×</span></button>
<h3 class="modal-title">Edit article</h3>
</div>
<div class="modal-body">
<div class="row">
<form id="edit_forma" class="form-horizontal" method="post" action="edit_article.php">
<fieldset>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">Category:</label>
<div class="col-md-4">
<select id="category" name="category" class="form-control">
<?php foreach($categories as $c): ?>
<option value="<?php echo $c->getId(); ?>"><?php echo $c->getName(); ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="title">Title:</label>
<div class="col-md-4">
<input id="title" name="title" placeholder="" class="form-control input-md" type="text">
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="content">Content:</label>
<div class="col-md-4">
<textarea class="form-control" id="content" name="content" rows="10"></textarea>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<button type="submit" id="submit" name="submit" class="btn btn-success">Izmeni</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal" class="btn btn-default">
Close
</button>
</div>
</div>
</div>
</div>
All of this works - but there is a problem - when I click on edit link - Modal is shown, and then after 2 or 3 seconds the form is filled (updated) with data.
Is there a way to fix this - to show Modal after the form is filled (updated)?
Thanks in advance.
As Ghazanfar Mir wrote in the comments below my question, I need just to set:
async: false
for ajax call, and the problem is solved.
Thank you Ghazanfar Mir.

Categories