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.
Related
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>
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
<html>
<head>
<title>
Installation
</title>
<script src="public/js/jquery-1.9.0.min.js"></script>
<script src="public/js/bootstrap3/js/bootstrap.js"></script>
<link media="all" type="text/css" rel="stylesheet" href="public/js/bootstrap3/css/bootstrap.min.css">
<link media="all" type="text/css" rel="stylesheet" href="public/js/bootstrap3/css/font-awesome.min.css">
<link media="all" type="text/css" rel="stylesheet" href="public/js/bootstrap3/css/main.css">
</head>
<body>
<div
class="navbar navbar-default navbar-fixed-top">
<div
class="container">
<div
class="navbar-header">
<button
type="button"
class="navbar-toggle"
data-toggle="collapse"
data-target=".navbar-collapse">
<span
class="icon-bar"></span>
<span
class="icon-bar"></span>
<span
class="icon-bar"></span>
</button>
<a
class="navbar-brand"
href="<?php // echo base_url()?>">Kingpabel
ATN</a>
</div>
<div
class="navbar-collapse collapse">
</div>
<!--/.nav-collapse -->
</div>
</div>
<div
class="rc">
<div
class="container main">
<div
class="col-md-3">
</div>
<div
class="col-md-6">
<form
method="post"
accept-charset="utf-8"
role="form"
class="form- signin form-horizontal">
<h2
class="form-signin-heading">Installation</h2>
<div
style="margin-top: 20px;">
<label
for="host_name"
class="col-sm-3 control-label"
style="color: black">
Host
Name
</label>
<div
class="input text database_host col-md-9">
<input
type="text"
name="host_name"
class="form-control"
placeholder="Database Host Name"
autofocus=""
required="required"
id="host_name">
</div>
</div>
<div>
<label
for="database_name"
class="col-sm-3 control-label"
style="color: black">
Database
Name
</label>
<div
class="input text database_name col-md-9">
<input
type="text"
name="database_name"
class="form-control"
placeholder="Database Name"
autofocus=""
required="required"
id="database_name">
</div>
</div>
<div>
<label
for="user_name"
class="col-sm-3 control-label"
style="color: black">
User
Name
</label>
<div
class="input text user_name col-md-9">
<input
type="text"
name="user_name"
class="form-control"
placeholder="Database User Name"
autofocus=""
required="required"
id="user_name">
</div>
</div>
<div>
<label
for="password"
class="col-sm-3 control-label"
style="color: black">
Password
</label>
<div
class="input text password col-md-9">
<input
type="password"
name="password"
class="form-control"
placeholder="Database Password"
autofocus=""
id="password">
</div>
</div>
<div>
<label
for="project_url"
class="col-sm-3 control-label"
style="color: black">
Project
Url
</label>
<div
class="input text project_url col-md-8 input-group input-group-sm">
<input
style=" margin-left: 13px;"
type="text"
name="project_url"
class="form-control"
placeholder="Project URL"
autofocus=""
id="project_url"
aria-describedby="basic-addon2">
<span
class="input-group-btn">
<button
class="btn btn-default"
type="button"
style="margin-top: -15px;">/public</button>
</span>
</div>
</div>
<button
type="submit"
class="btn btn-lg btn-login btn-block">Install</button>
</form>
</div>
</div>
</div>
<section
id="bottom"
style="margin-top: 50px">
</section>
<footer
id="footer">
<div
class="container">
<div
class="footer">
<div
class="row">
<div
class="col-md-12">
</div>
</div>
</div>
</div>
</footer>
</body>
</html>
<?php
if ($_POST) {
$envFile = file_get_contents('.env');
if (isset($_POST['host_name']) && $_POST['host_name'])
$envFile = str_replace('DB_HOST=localhost', "DB_HOST= {$_POST['host_name']}", $envFile);
if (isset($_POST['database_name']) && $_POST['database_name'])
$envFile = str_replace('DB_DATABASE=homestead', "DB_DATABASE={$_POST['database_name']}", $envFile);
if (isset($_POST['user_name']) && $_POST['user_name'])
$envFile = str_replace('DB_USERNAME=homestead', "DB_USERNAME={$_POST['user_name']}", $envFile);
if (isset($_POST['password']) && $_POST['password'])
$envFile = str_replace('DB_PASSWORD=secret', "DB_PASSWORD={$_POST['password']}", $envFile);
file_put_contents('.env', $envFile);
if (isset($_POST['project_url']) && $_POST['project_url']) {
file_get_contents('index.php');
file_put_contents('index.php', "<?php
header('Location: {$_POST['project_url']}/public');
");
}
}`
In above code, after submitting the form, the idex.php is rewriiten and changes. And i wanted to redirect to another page after submitting the form. But it is not being redirected.
Not sure what all your block of codes are for but perhaps try this..
On your
if (isset($_POST['project_url']) && $_POST['project_url']) {
file_get_contents('index.php');
file_put_contents('index.php', "<?php
header('Location: {$_POST['project_url']}/public');
");
}
change to
if (isset($_POST['project_url']) && $_POST['project_url']) {
$url = $_POST['project_url'] + '/public';
header('Location: $url');
}
I want to create a form to submit a product which has a features to upload images inside the form.
my difficulties are :
I find it hard to upload files without losing data that already fill in by the user in the form.
I want the user upload the file first then user presses the submit button
this is the sequence that i want to achieve :
user open addProduct.php-> then the user fill the form -> the user
upload picture -> finally the user press submit button to save their
data
this is my form page
<?php
require_once './model/functions.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="center-block" style="width: 130px;">
<h3><strong>Add Book</strong></h3>
</div>
</div>
<form id="productform" method="post" action="test_add_product.php" class="form-horizontal">
<div class="form-group">
<label for="kategori" class="col-lg-4 control-label">Kategori : </label>
<div class="col-lg-2">
<input type="hidden" class="form-control" id="main-sub-pages-id" name="category-id">
<div class="input-group">
<input type="text" class="form-control" id="main-sub-pages">
<span class="input-group-btn">
<button type="button" class="btn btn-default" data-toggle="modal" data-target=".category-modal"><span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span></button>
</span>
</div>
<div class="modal fade category-modal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<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">Category</h4>
</div>
<div class="modal-body">
<div class="container">
<div class="row">
<div class="col-lg-4">
<div class="text-center">
<h4>Pages</h4>
</div>
</div>
<div class="col-lg-4">
<div class="text-center">
<h4>Sub Pages</h4>
</div>
</div>
<div class="col-lg-4">
<div class="text-center"></div>
<h4></h4>
</div>
</div>
<div class="row">
<div class="col-lg-4">
<select id="pages" name="pages" class="form-control" size="6" onchange="loadCategory('pages','sub-pages')">
<?php
$subjects_set = find_all_subjects();
while($subject = mysqli_fetch_assoc($subjects_set)){
?>
<option value="<?php echo $subject["id"];?>"><?php echo $subject["name"]; ?></option>
<?php
} ?>
</select>
</div>
<div class="col-lg-4">
<select id="sub-pages" name="pages" class="form-control" size="6">
</select>
</div>
<div class="col-lg-4">
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal" onclick="saveCategory()">Save changes</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<label for="tipeIklan" class="col-lg-4 control-label">Tipe Iklan : </label>
<div class="col-lg-6">
<label class="radio-inline">
<input type="radio" name="tipeIklan" id="tipeIklan" value=0>Dicari
</label>
<label class="radio-inline">
<input type="radio" name="tipeIklan" id="tipeIklan" value=1 checked="checked">Dijual
</label>
<label class="radio-inline">
<input type="radio" name="tipeIklan" id="tipeIklan" value=2>Disewakan
</label>
<label class="radio-inline">
<input type="radio" name="tipeIklan" id="tipeIklan" value=3>Jasa
</label>
</div>
</div>
<div class="form-group">
<label for="judul" class="col-lg-4 control-label">Judul : </label>
<div class="col-lg-6">
<input type="text" class="form-control" id="judul" name="judul" placeholder="Judul iklan anda">
</div>
</div>
<div class="form-group">
<label for="deskripsi" class="col-lg-4 control-label">Deskripsi : </label>
<div class="col-lg-6">
<textarea id="deskripsi" class="form-control" rows="5" name="description"></textarea>
</div>
</div>
<div class="form-group">
<label for="harga" class="col-lg-4 control-label">Harga(Rp.) : </label>
<div class="col-lg-3">
<input type="text" class="form-control" id="harga" name="harga" aria-describedby="helpBlock" placeholder="cukup tuliskan angka" onfocus="toolTipHarga()">
</div>
</div>
<div class="form-group">
<label for="kondisi" class="col-lg-4 control-label">Kondisi : </label>
<div class="col-lg-6">
<label class="radio-inline">
<input type="radio" name="kondisi" id="kondisi" value=0>Bekas
</label>
<label class="radio-inline">
<input type="radio" name="kondisi" id="kondisi" value=1 checked="checked">Baru
</label>
</div>
</div>
<div class="form-group">
<label for="provinsi" class="col-lg-4 control-label">Provinsi : </label>
<div class="col-lg-3">
<select id="provinsi" name="provinsi" class="form-control" onchange="loadLocation('provinsi', 'kota')">
<option>Select one</option>
<?php
$provinsi_set = find_all_province();
while($provinsi = mysqli_fetch_assoc($provinsi_set)){
?>
<option value="<?php echo $provinsi["id"];?>"><?php echo $provinsi["nama"]; ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="kota" class="col-lg-4 control-label">Kota : </label>
<div class="col-lg-3">
<select id="kota" name="kota" class="form-control">
</select>
</div>
</div>
<div class="form-group">
<label for="foto" class="col-lg-4 control-label">Upload Foto : </label>
<div class="col-lg-6">
<div class="row">
<?php
//scan "uploads" folder and display them accordingly
$folder = "uploads";
$results = scandir('uploads');
foreach ($results as $result) {
if ($result === '.' or $result === '..') continue;
if (is_file($folder . '/' . $result)) {
echo '
<div class="col-md-3">
<div class="thumbnail">
<img src="'.$folder . '/' . $result.'" alt="...">
<div class="caption">
<p>Remove</p>
</div>
</div>
</div>';
}
}
?>
</div>
<div class="row">
<form class="well" action="upload.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="file">Select a file to upload</label>
<input type="file" name="file">
<p class="help-block">Only jpg,jpeg,png and gif file with maximum size of 1 MB is allowed.</p>
</div>
<input type="submit" class="btn btn-lg btn-primary" value="Upload">
</form>
</div>
</div>
</div>
<div class="col-lg-offset-4">
<button type="submit" id="registrationbutton" class="btn btn-default">Tayangkan!</button>
</div>
</form>
</div>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script type="text/javascript">
$("#registrationbutton").click(function(){
if(!validateText("main-sub-pages"))
{
return false;
}
if(!validateText("judul"))
{
return false;
}
if(!validateText("deskripsi"))
{
return false;
}
if(!validateText("harga"))
{
return false;
}
if(!validateNumber("harga"))
{
}
if(!validateText("provinsi"))
{
return false;
}
if(!validateText("kota"))
{
return false;
}
$("form#productform").submit();
});
</script>
<?php close_connection(); ?>
</body>
</html>
note : as you can see, i had form inside a form
what i want to achieve was this
When working with uploading data,say, image. you should add the enctype form as others suggested.
<form id="productform" method="post"
enctype="multipart/form-data" action="test_add_product.php"
class="form-horizontal">
.............
</form>
Here's the uploader script of Javascript as follows:
<script>
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("file").files[0]);
oFReader.onload = function (oFREvent) {
document.getElementById("imgTampil").src = oFREvent.target.result;
};
</script>
<img id="imgTampil" alt="" src="" class="img-responsive"/>
<input id="file" type="file" name="file" onchange="displ_img();" class="btn btn-default" />
<script>
function displ_img() {
//to display the image
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("file").files[0]);
oFReader.onload = function (oFREvent) {
document.getElementById("imgTampil").src = oFREvent.target.result;
};
};
</script>
Place the scriptabove wherever you wish,e.g. above submit button.
UPDATED:
Codes to upload image in PHP (for ex.):
$uploadpath='../usrphoto/'; //image path
$allowtype=array('gif','jpg','png');
$allowsize=71250;
if(isset($_FILES['file']) && strlen($_FILES['file']['name']) > 1){
$uploadpath=$uploadpath . basename( $_FILES['file']['name']);
$sepext=explode('.', strtolower($_FILES['file']['name']));
$type=end($sepext);
if(!in_array($type, $allowtype)){
echo "<script>alert('Format file tidak valid! (format:gif,jpg & png)');</script>";
header("Location: http://www.mywebsite.com/");
}
elseif ($allowsize>71250){
echo "<script>alert('Ukuran photo anda terlalu besar! (maks.41 Kilobytes)');</script>";
header("Location: http://www.mywebsite.com/");
}
else{
if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadpath)){
echo 'successfully uploaded!';
else{
header("Location: http://www.mywebsite.com/");
}
}
}
<form enctype="multipart/form-data"
just add an attribute 'enctype="multipart/form-data"' into your form attribute.
Just add this enctype type attribute in your form it will not loose the quality of the uploaded data
<form enctype="multipart/form-data">
use swfupload upload technique..it uploads data while browsing and returns the name of the uploaded file.. After that when user will click submit, store that filename with other form fields into database.
I have been having real trouble getting columns in the right order on my site. I'd like the red box (under the sign in box) to be another panel for news. But i cant work out how to do this any ideas.
https://www.dropbox.com/s/vgwhpurwo15gvet/Capture.PNG?dl=0
<div class="container">
<div class="col-lg-6">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Sign In</h3>
<div style="float:right; font-size: 80%; position: relative; top:-10px">Forgot password?</div>
</div>
<div class="panel-body">
<form action="index.php" method="post">
<div class="form-group">
<label for="username">Username</label>
<input type="text" name="username" class="form-control" placeholder="Username" id="username" value="<?php echo $submitted_username; ?>" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="Password" name="password" value="">
</div>
<input type="submit" class="btn btn-success" value="Login" />
<span>
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#missionmodal">
Read Me!</button>
<!-- Modal -->
<div class="modal fade" id="missionmodal" 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"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Our Mission</h4>
</div>
<div class="modal-body">
People Tank is far from finished but we have a clear perception of what we want People Tank to be. We want it to be clear and easy to use for everyone, revolutionize the way we socialize with friends and family but most importanlty we want it to be free but we can only achieve this with your early support for our idea. That's why we ask if you can afford to give us a small donation to help us cover our costs. Any amount is greatly appreciated.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary disabled">Donate With PayPal</button>
</div>
</div>
</div>
</div></form>
</div>
</div>
</div>
<div class="col-lg-6">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Register</h3>
</div>
<div class="panel-body">
<form action="register.php" method="post">
<div class="form-group">
<label for="firstname">First Name</label>
<input type="text" class="form-control" id="firstname" placeholder="First Name" name="firstname" value="" >
</div>
<div class="form-group">
<label for="secondname">Second Name</label>
<input type="text" class="form-control" id="secondname" placeholder="Second Name" name="secondname" value="" >
</div>
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" placeholder="Username" name="username" value="" >
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" placeholder="Email" name="email" value="" >
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="Password" name="password" value="" >
</div>
<div class="form-group">
<label for="gendeer">Select Gender</label>
<select name="gender" id="gender" class="form-control">
<option>Male</option>
<option>Female</option>
</select>
</div>
<p>DOB:<br />
<?php
$con = mysql_connect('localhost','root',"");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("peopletank", $con);
$name= mysql_query("select * from month");
echo '<select name="month" id="user" class="textfield1">';
while($res= mysql_fetch_assoc($name))
{
echo '<option>';
echo $res['month'];
echo'</option>';
}
echo'</select>';
mysql_close($con)
?>
<?php
$con = mysql_connect('localhost','root',"");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("", $con);
$name= mysql_query("select * from day order by day_id asc");
echo '<select name="day" id="user" class="textfield1">';
while($res= mysql_fetch_assoc($name))
{
echo '<option>';
echo $res['day'];
echo'</option>';
}
echo'</select>';
mysql_close($con)
?>
<?php
$con = mysql_connect('localhost','root',"");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("", $con);
$name= mysql_query("select * from year");
echo '<select name="year" id="user" class="textfield1">';
while($res= mysql_fetch_assoc($name))
{
echo '<option>';
echo $res['year'];
echo'</option>';
}
echo'</select>';
mysql_close($con)
?>
<br /><br />
<input type="submit" class="btn btn-success" value="Register" />
<a class="btn btn-primary disabled" href="#">Register with Facebook</a>
<a class="btn btn-info disabled" href="#">Register with Twitter</a>
</p>
</form>
</div>
</div>
</div>
</div>
</div>
Ok, this Bootply should give you the general idea.
Bootply
And here's the code:
<div class="container">
<div class="col-lg-6">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Sign In <span class="pull-right">Forgot password?</span></h3>
</div>
<div class="panel-body">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">News</h3>
</div>
<div class="panel-body">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-6">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Register</h3>
</div>
<div class="panel-body">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Basically, it's a col structure within a col structure, so that each column can have multiple "rows"; or in your case panels. Just fill in the panel-body with your inputs, and it should work fine for you.
Cheers!