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.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a booking form and I successfully connected my database to my website. When I go to my database in MYSQL to see the data I inserted, all fields are in zeros. I tried changing my type of my data fields in the database but it didn't work.
My website connection with the MYSQL database:
<?php
$conexao=mysqli_connect("localhost", "root", "", "sitepap2");
?>
Code to sends the data inserted in the form to the database and to check if there's already a similar record in the database:
<?php
include("conecta.php");
$pnome=$_POST['pnome'];
$numerotmv=$_POST['numerotmv'];
$localsessao=$_POST['localsessao'];
$data=$_POST['data'];
$hora=$_POST['hora'];
$pesquisaUsuario = mysqli_num_rows(mysqli_query($conexao,"
SELECT nome FROM agendamento
WHERE numerotmv = '$numerotmv'
"));
if($pesquisaUsuario >0){
echo 1;
}else{
mysqli_query($conexao, "
insert into agendamento
(pnome, numerotmv, localsessao, data, hora)
values
('$pnome','$numerotmv', '$localsessao','$data','$hora')
");
echo 0;
}
?>
My index file where the form is displayed:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PHP form</title>
</head>
<body>
<div class="modal fade" id="calendario" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-body">
<!-- Registro -->
<form name="calendario" id="calendario" method="post" class="form-horizontal">
<div>
<button type="button" class="close" data-dismiss="modal">×</button>
<h2 class="text-uppercase align-self-end text-center">Agendamento</h2>
<hr class="divider">
</div>
<div class="form-group">
<label class="control-label col-xs-4">Primeiro Nome</label>
<div class="col-xs-8">
<input type="text" class="form-control" name="pnome" required="required">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-4">Número de telemóvel</label>
<div class="col-xs-8">
<input type="text" class="form-control" name="numerotmv" required="required">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-4">Local da Sessão</label>
<div class="col-xs-8">
<input type="text" class="form-control" name="localsessao" required="required">
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input class="form-control" name="data" type="date" required>
<span class="form-label">Data</span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input class="form-control" name="hora" type="time" required>
<span class="form-label">Hora</span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-8 col-xs-offset-4">
<button type="submit" onClick="verificarAgendamento()" class="btn btn-primary btn-lg">Agendar</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<script>
function verificarAgendamento(){
event.preventDefault();
$.ajax({
type: "POST",
url: "agendamento.php",
data: $("#calendario").serialize(),
success: function(resultado){
if(resultado==0){
alert("Booking made with success");
}else{
alert("error");
}
return false;
}
});
$('#calendario')[0].reset();
return false;
}
</script>
</body>
</html>
My database structure:
database
What happens when I try to insert data through my PHP form in the database:
problem
Thank to all the people that spent time reading this post to help me. I'm grateful.
You have two id's called calendario. The form and the div. So your ajax request is serializing the div, not the form.
Change one of the ids to a unique value and it will work e.g.:
NOTE: If you change the id of your form you need to change it in your ajax call as well
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PHP form</title>
</head>
<body>
<div class="modal fade" id="calendario" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-body">
<!-- Registro -->
<form name="calendario" id="calendario-form" method="post" class="form-horizontal">
<div>
<button type="button" class="close" data-dismiss="modal">×</button>
<h2 class="text-uppercase align-self-end text-center">Agendamento</h2>
<hr class="divider">
</div>
<div class="form-group">
<label class="control-label col-xs-4">Primeiro Nome</label>
<div class="col-xs-8">
<input type="text" class="form-control" name="pnome" required="required">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-4">Número de telemóvel</label>
<div class="col-xs-8">
<input type="text" class="form-control" name="numerotmv" required="required">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-4">Local da Sessão</label>
<div class="col-xs-8">
<input type="text" class="form-control" name="localsessao" required="required">
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input class="form-control" name="data" type="date" required>
<span class="form-label">Data</span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input class="form-control" name="hora" type="time" required>
<span class="form-label">Hora</span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-8 col-xs-offset-4">
<button type="submit" onClick="verificarAgendamento()" class="btn btn-primary btn-lg">Agendar</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<script>
function verificarAgendamento(){
event.preventDefault();
$.ajax({
type: "POST",
url: "agendamento.php",
data: $("#calendario-form").serialize(),
success: function(resultado){
if(resultado==0){
alert("Booking made with success");
}else{
alert("error");
}
return false;
}
});
$('#calendario')[0].reset();
return false;
}
</script>
</body>
</html>
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/
Can anyone please help me with my code i registered it to my localhost but the script does not pop up and it does not register to my database.
by the way this is a search engine I'm making thank you for your answers.
<body>
<div class="container-fluid">
<br>
<center>
<h2><b> Insert Website</b></h2></center>
<br>
<form action="insert_site.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<div class="row">
<label class="col-sm-2" for="stitle"> Site title</label>
<div class="col-sm-10">
<input type="text" class="form-control" is="stitle" name="s_title" placeholder="Enter Site Title">
</div>
</div>
</div>
</form>
<br>
<form>
<div class="form-group">
<div class="row">
<label class="col-sm-2" for="slink"> Site link</label>
<div class="col-sm-10">
<input type="text" class="form-control" is="slink" name="s_link" placeholder="Enter Site Link">
</div>
</div>
</div>
</form>
<br>
<form>
<div class="form-group">
<div class="row">
<label class="col-sm-2" for="skey"> Site key</label>
<div class="col-sm-10">
<input type="text" class="form-control" is="skey" name="s_key" placeholder="Enter Site key">
</div>
</div>
</div>
</form>
<form>
<div class="form-group">
<div class="row">
<label class="col-sm-2" for="sdes"> Site Description</label>
<div class="col-sm-10">
<textarea class="form-control" is="sdes" name="s_des" placeholder="Enter Site Description"></textarea>
</div>
</div>
</div>
</form>
<form>
<div class="form-group">
<div class="row">
<label class="col-sm-2" for="simg"> Site Image</label>
<div class="col-sm-10">
<input type="file" class="form-control-file" is="simg" name="s_img" placeholder="Enter Site Image">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<center>
<input type="submit" class="btn btn-outline-success" name="submit" value="Add Website">
<input type="reset" class="btn btn-outline-danger" name="submit" value="Cancel">
</center>
</div>
</div>
</form>
</div>
<script src="https://ajax.googleapis.com/ajaz/libs/kquery/3.0.0/jquery.min.js" integrity="sha384-THPy051/pyDQGamwU6poAc/hOdqxjnoEXzbT+OuUAFqNqFjL+41GLBgCJC3ZOShY" crossorigin="anonymous"></script>
<script tsrc="https://odnjs.cloudflare.com/ajax/libs/tether/1.2.0/js/tether.min.js" integrity="sha384-Plbewg8JY28KFelvJVai01l0WyZzrYWG825m+oZOeDDS1f7d/js61kvy1+X+guPIB" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
<?php
$mysqli = new mysqli("localhost", "root", "", "search");
$result = $mysqli->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);
if(isset($_POST["submit"]))
{
$s_title = $_POST["s_title"];
$s_link = $_POST["s_link"];
$s_key = $_POST["s_key"];
$s_des = $_POST["s_des"];
$_simg = $_FILES["simg"] ["name"];
if(move_uploaded_file($_FILES["simg"] ["tmp_name"], "img/". $_FILES["simg"] ["name"]))
{
$sql = "insert into website(site_title, site_link, site_key, site_des, site_img) values('$s_title','$s_link','$s_key','$s_des','$s_img')";
$rs = mysqli_query($sql);
if($rs)
{
echo"<script> alert('Site uploaded Successfully') </script>";
}
else
{
echo"<script> alert('Uploaded failed please try again')</script>";
}
}
}
?>
I sow only this mistake.
echo $_simg = $_FILES...
I think you need to change in
INSERT INTO : ... '$s_img');
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.
I have a list of product on mypage each product is having a enquiry button on it.
By clicking enquiry button bootstrap pop up opens.
I want that form to work for every product with product id of each product
I have achieved this by putting bootstrap pop up model in loop
my Question is. Instead of repeation blok of bootstrap pop up model , instead of keeping popup html in loop,is it possible to achieve this with function and keeping popup html outside loop ?
<?php if(isset($ser_gal)){
$gal_count =1;
foreach($ser_gal as $s){?>
<div class="element">
<div class="tz-inner" >
<div class="tz-image-item"> <img alt="" src="<?php echo URL;?>gallery/thumbs/thumb_<?php echo $s->IMAGE_PATH ; ?>"> </div>
<h6><?php echo $s->interior_cat;?></h6>
<hr />
<div class="cl"></div>
<?php echo $s->title;?>
<div align="center" >Enquiry</div>
</div>
</div>
<!--form-->
<div class="bs-example">
<div id="myModal<?php echo $gal_count;?>" class="modal fade">
<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">Enquiry</h4>
</div>
<div class="modal-body">
<form role="form">
<div class="form-group">
<label for="recipient-name" class="control-label">Name</label>
<input type="text" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">Email Id</label>
<input type="text" class="form-control texttrans_none" id="recipient-name">
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">Mobile No</label>
<input type="text" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">Product</label>
<input type="text" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="message-text" class="control-label">Message</label>
<textarea class="form-control" id="message-text"></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<!---->
<?php $gal_count++;} }?>