add data to mongodb using jquery and php - php

my HTML code
<section>
<div class="container " style="height: 55vh;">
<div class="row">
<div class="col-1 icon-content-wraper">
<h2> add products</h2>
</div>
</div>
<div class="row">
<div class="col-1">
<form id="form" action="newadd.php" method="post">
<div class="col-1 ">
add product:
<div class="input-container">
<input type="text" id="reference" name="reference" placeholder=" " required>
<label for="reference">Product reference number</label>
</div>
<div class="input-container">
<input type="text" id="stock_amount" name="stock_amount" placeholder=" " required>
<label for="stock_amount">Product stock amount</label>
</div>
<div class="input-container">
<input type="text" id="product_name" name="product_name" placeholder=" " required>
<label for="product_name">Product name</label>
</div>
Product cattegory: <div class="input-container">
<select class="sort" name="category" id="category">
<option value="1">Salty
<option value="2" selected="">Sweet
<option value="3" selected="">Chocolate
<option value="4" selected="">Sour
</select>
</div>
<div class="input-container">
<textarea id="description" name="description" rows="4" cols="50" required></textarea>
<span for="description">description</span>
</div>
<div class="input-container">
<input type="text" id="price" name="price" placeholder=" " required>
<label for="price">Product price</label>
</div>
<div class="input-container">
<input type="text" id="tax" name="tax" placeholder=" " required>
<label for="tax">Product tax</label>
</div>
<div class="input-container">
<input type="text" id="discount" name="discount" placeholder=" " required>
<label for="discount">Product discount</label>
</div>
</div>
<div class="col-1 mt-60">
<input id="Add" class="btn-standard" type="submit" name="Add" value="Add" />
</div>
</form>
</div>
</div>
</div>
</section>
<div class="article-progress-bar"></div>
<script type="text/javascript" >
$(document).ready(function(){
$("#form").submit(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: "newadd.php",
dataType:"json",
data:{
Reference:$("#reference").val(),
Stock: $("#stock_amount").val(),
ProductName: $("#product_name").val(),
Category: $("#category: selected").text(),
Description: $("#description").val(),
Price: $("#price").val(),
Tax: $("#tax").val(),
Discount: $("#discount").val()
},
success: function(response){
alert(response);
if (response == 1){
window.location="cms-view-products.php";
}
else if (response == 0){
alert("error to add data");
}
}
})
});
});
</script>
my PHP code
<?php
....
//Extract the data that was sent to the server
if($_SERVER['REQUEST_METHOD'] === 'POST'){
$reference = $_POST['reference'];
$stock = $_POST['Stock'];
$name = $_POST['ProductName'];
$category = $_POST['category'];
$description = $_POST['Description'];
$price = $_POST['Price'];
$tax = $_POST['Tax'];
$discount = $_POST['Discount'];
//Convert to PHP array
$dataArray = [
$Tag => [
"Reference" => $reference,
"StockAmount" => $stock
],
"Name" => $name,
"Description" => $description,
"Category" => $category,
"Tax" => $tax,
"Rating" => " ",
"Discount" => $discount,
"Price" => $price
];
//Add the new product to the database
$insertResult = $collection->insertOne($dataArray);
//Echo result back to user
if($insertResult->getInsertedCount()){
echo 1;
}
else {
echo 0;
}
}
else {
echo 'connection Error';
}
The error is that it adds null fields to the database plus:
Warning: Undefined array key "Stock" in C:\xampp\htdocs\newadd.php on line 19
Warning: Undefined array key "ProductName" in C:\xampp\htdocs\newadd.php on line 20
Warning: Undefined array key "Description" in C:\xampp\htdocs\newadd.php on line 22
Warning: Undefined array key "Price" in C:\xampp\htdocs\newadd.php on line 23
etc.

Related

Change data requirements of an input field based on the value of a select field in a web form

I am working on a web form that includes a select drop down with two options: "Cedula" (in English, "Identification") and "Pasaporte" (in English, "Passport").
Here is an image of my web form so far.
Please help me achieve the following goal: when the user selects "Cedula", they are limited to 10 digits, but when they select "Pasaporte, they are not limited to 10 digits.
Here is my code so far:
<?php
if ($_GET['id']) {
$cliente = $clienteNegocio->recuperar($_GET['id']);
$txtAction = 'Editar';
}else{
$cliente = new cliente();
$txtAction = 'Agregar';
}
?>
<div class="container">
<div class="page-header">
<h1><?php echo $txtAction; ?> Cliente</h1>
</div>
<form role="form" method="post" id="principal">
<input type="hidden" name="id" value="<?php echo $cliente->getId();?>" >
<div class="form-group">
<label for="nombre">Nombre</label>
<input type="text" class="form-control" id="nombre" name="nombre" placeholder="Nombre" value="<?php echo $cliente->getNombre();?>" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="apellido">Apellidos</label>
<input type="text" class="form-control" id="apellido" name="apellido" placeholder="Apellido" value="<?php echo $cliente->getApellido();?>" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="tipoDoc">Tipo de Documento</label>
<select class="form-control" id="tipoDoc" name="tipoDoc">
<option value="Cedula" <?php if($cliente->getTipoDoc() == 'Cedula') {echo "selected";} ?> >Cedula</option>
<option value="Pasaporte" <?php if($cliente->getTipoDoc() == 'Pasaporte') {echo "selected";} ?> >Pasaporte</option>
</select>
</div>
<div class="form-group">
<label for="nroDoc">Numero de Documento</label>
<input type="number" class="form-control" id="nroDoc" maxlength=10 oninput="if(this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);"
name="nroDoc" placeholder="Numero de Documento" value="<?php echo $cliente ->getNroDoc();?>" required>
<div class="help-block with-errors"></div>
</div>
There you go.
I have written a javascript function to check the length with you select Pasaporte/Cedula.
Secondly, in <input type = "number"/> you cannot set maxLength. Hence you have to set <input type = "text" />. Also, an onKeyPress event to verify the input as number
<?php
if ($_GET['id']) {
$cliente = $clienteNegocio->recuperar($_GET['id']);
$txtAction = 'Editar';
}else{
$cliente = new cliente();
$txtAction = 'Agregar';
}
?>
<script>
function setMaxLength(){
var inputVal = document.getElementById("tipoDoc")
var selIndex = inputVal.options[inputVal.selectedIndex].value
var inputNum = document.getElementById("nroDoc");
if( selIndex === "Cedula"){
inputNum.maxLength = 10
selIndex.substr(0, 9);
inputNum.value = inputNum.value.substr(0, 9);
} else{
// Set your own limit here
// if selIndex === "Pasaporte"
inputNum.maxLength = 20
}
}
</script>
<div class="container">
<div class="page-header">
<h1><?php echo $txtAction; ?> Cliente</h1>
</div>
<form role="form" method="post" id="principal">
<input type="hidden" name="id" value="<?php echo $cliente->getId();?>" >
<div class="form-group">
<label for="nombre">Nombre</label>
<input type="text" class="form-control" id="nombre" name="nombre" placeholder="Nombre" value="<?php echo $cliente->getNombre();?>" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="apellido">Apellidos</label>
<input type="text" class="form-control" id="apellido" name="apellido" placeholder="Apellido" value="<?php echo $cliente->getApellido();?>" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="tipoDoc">Tipo de Documento</label>
<select class="form-control" id="tipoDoc" name="tipoDoc" onChange="setMaxLength()">
<option value="Cedula" <?php if($cliente->getTipoDoc() == 'Cedula') {echo "selected";} ?> >Cedula</option>
<option value="Pasaporte" <?php if($cliente->getTipoDoc() == 'Pasaporte') {echo "selected";} ?> >Pasaporte</option>
</select>
</div>
<div class="form-group">
<label for="nroDoc">Numero de Documento</label>
<!--
Here onKeyPress method is used to check if the input is a number
in <input type = "number"/> you cannot set maxLength
hence you have to set <input type = "text" />
-->
<input type="text" class="form-control" id="nroDoc" maxlength=10 onkeypress="if ( isNaN(this.value + String.fromCharCode(event.keyCode) )) return false;"
name="nroDoc" placeholder="Numero de Documento" value="<?php echo $cliente ->getNroDoc();?>" required>
<div class="help-block with-errors"></div>
</div>
</form>
</div>

Showing Alert of update successful but not able to click alert, and database not updated

When I click on update, a page is only getting refreshed, values are not updated. I have 4 tables in database, namely -> stud,country_master_academic,master_state, master_city. In a console, values are not showing when I click on update button.
my update.php page
<div class="container" style="width:700px;height:1100px;margin-top:10%;box-shadow:4px 3px 3px 3px grey;margin-left:25%;background-color:#eaf2fa;padding:3%;">
<h4 style="font-weight:bold;"><span class="glyphicon glyphicon-edit"></span>Update Student</h4>
<div class="form-group">
<label for="photo"></label>
<?php
$img = "images/".trim($vrow["photo"]);
echo '<img src='.$img.' id="resultedPhoto" class="image" style="margin-left:75%;margin-top:2%;width:120px;height:120px;border:2px solid #bbbbbb;border-radius:10px;">';
?><br/>
<input type="file" name="upphoto" id="upphoto" style="margin-left:70%;" required />
</div>
<form class="form-horizontal" name="form1" id="form1" method="post" action="<?php $_PHP_SELF?>" enctype="multipart/form-data">
<div class="form-group">
<label for="no"><span class="glyphicon glyphicon-lock"></span><b> Student No: </b></label>
<input type="text" class="form-control" name="upno" id="upno" disabled value="<?php echo $vrow['stud_no'];?>" required />
</div>
<div class="form-group">
<label for="name"><span class="glyphicon glyphicon-user"></span><b> Student Name: </b></label>
<input type="text" class="form-control" name="upname" id="upname" value="<?php echo $vrow['stud_name'];?>" required pattern="[a-zA-Z]{3,}" title="Name should only contain letters and atleast 3 letters"/>
</div>
<div class="form-group">
<label for="no"><span class="glyphicon glyphicon-phone"></span><b> Mobile No: </b></label>
<input type="text" class="form-control" value="<?php echo $vrow['mobile']; ?>" name="upmob_no" required id="upmob_no" pattern="[0-9]{10}" title="Mobile number should be of 10 digits"/>
</div>
<div class="form-group">
<label for="dob"><span class="glyphicon glyphicon-calendar"></span><b> Birth Date: </b></label>
<input type="date" required class="form-control" value="<?php echo $vrow['dob'];?>" name="updob" id="updob" />
</div>
<div class="form-group">
<label for="add"><span class="glyphicon glyphicon-map-marker"></span><b> Address: </b></label>
<textarea rows="4" cols="33" class="form-control" name="upadd" id="upadd" required><?php echo $vrow['address'];?></textarea>
</div>
<div class="form-group">
<label for="gen"><b> Gender: </b></label>
<input type="radio" name="gender" id="genderMale" value="M"<?php echo ($vrow['gender']=='M')?'checked':' ' ?> required="required">Male
<input type="radio" name="gender" id="genderFemale" value="F"<?php echo ($vrow['gender']=='F')?'checked':' ' ?> required="required">Female
</div>
<div class="form-group">
<label for="cntry"><span class="glyphicon glyphicon-map-marker"></span><b> Country: </b></label>
<select required name="upcountry" id="upcountry" class="form-control">
<option value="">Select</option>
<?php
$country="SELECT * from country_master_academic";
$res= $conn->query($country);
if($res->num_rows>0){
while($row=$res->fetch_assoc()){
if($row["country_name"]==$vcountry or $vrow['country'] == $row["country_code"] )
{
echo '<option value='.$row["country_code"].' selected>'.$row["country_name"].'</option>';
}
else
{
echo '<option value='.$row["country_code"].'>'.$row["country_name"].'</option>';
}
}
}
?>
</select>
</div>
<div class="form-group">
<label for="state"><span class="glyphicon glyphicon-map-marker"></span><b> State: </b></label>
<select required name="upstate" id="upstate" class="form-control">
<option value="">Select</option>
<?php
$state="SELECT * from master_state";
$res= $conn->query($state);
if($res->num_rows>0){
while($row=$res->fetch_assoc()){
if($row["state_name"]==$vstate or $vrow['state'] == $row["state_code"] )
{
echo '<option value='.$row["state_code"].' selected>'.$row["state_name"].'</option>';
}
else
{
echo '<option value='.$row["state_code"].'>'.$row["state_name"].'</option>';
}
}
}
?>
</select>
</div>
<div class="form-group">
<label for="city"><span class="glyphicon glyphicon-map-marker"></span><b> City: </b></label>
<select required name="upcity" id="upcity" class="form-control">
<option value="">Select</option>
<?php
$city="SELECT * from master_city";
$res= $conn->query($city);
if($res->num_rows>0){
while($row=$res->fetch_assoc()){
if($row["city_name"]==$vcity or $vrow['city'] == $row["city_code"] )
{
echo '<option value='.$row["city_code"].' selected>'.$row["city_name"].'</option>';
}
else
{
echo '<option value='.$row["city_code"].'>'.$row["city_name"].'</option>';
}
}
}
?>
</select>
</div>
<br/>
<div class="form-group">
<button type="submit" name="update" id="update" style="font-weight:bold;" class="btn btn-primary">Update</button>
</div>
</form>
</div>
upresult.php page
<?php
include("connection.php");
$no=trim($_POST['upno']);
$name=trim($_POST['upname']);
$mob=trim($_POST['upmob_no']);
$dob=trim($_POST['updob']);
$add=trim($_POST['upadd']);
$photo=trim($_FILES['upphoto']['name']);
$gen=trim($_POST['gender']);
$cn=trim($_POST['upcountry']);
$st=trim($_POST['upstate']);
$ct=trim($_POST['upcity']);
$qry="update stud set stud_name='".$name."',mobile='".$mob."',dob='".$dob."',address='".$add."',gender='".$gen."',country='".$cn."',state='".$st."',city='".$ct."' where stud_no='".$no."'";
$data=mysqli_query($conn,$qry);
if($data)
{
echo '<script language="javascript">';
echo 'alert("Updated Successfully")';
echo '</script>';
}
else {
echo '<script language="javascript">';
echo 'alert("Cannot update record")';
echo '</script>';
}
?>
jquery
$(document).ready(function(){
$("#form1").submit(function(event){
event.preventDefault();
var formData = new FormData(this);
$.ajax({
url:"upresult.php",
type:"POST",
data:{formData:formData},
async:true,
success:function(data) {
alert(data);
},
cache:false,
contentType:false,
processData:false
});
});
});
Thank you in advance.
Don't use the same name
data:{stud_no:stud_no}, //here change the name of vbl or value
data:{
formData:formData, //here change the name of vbl or value
stud_no:stud_no, //here change the name of vbl or value
}

Internal Server Error while doing an insertion in Codeigniter

I am working on a simple CRUD application using Codeigniter, and this section of code was working perfectly well until I added more functions.
Here is the problem:
I was adding 3 items to the database from user inputs, and it had no error. But when I added more input fields to get more inputs from the user, I ended up getting an Internal Server Error when I click on the "Add Item" button.
Below is the View Code:
<?php
if ($_SESSION[AppStrings::$NOMENCLATURE] != AppStrings::$SERVICE) {
if ($_SESSION[AppStrings::$SIZE] != AppStrings::$MICRO) {
?>
<form data-toggle="validator" class="form-horizontal form-material" method="post" action="#" onsubmit="addProduct(); return false;">
<div class="form-group m-t-40">
<div class="col-xs-12">
<div class="input-group">
<div class="input-group-addon"><i class="mdi mdi-barcode-scan"></i></div>
<input class="form-control" type="text" required="" placeholder=" <?= $nomenclature ?> Code" id="product_code" required/>
</div>
</div>
</div>
<div class="form-group m-t-40">
<div class="col-md-6">
<input class="form-control" type="text" required="" placeholder="<?= $nomenclature ?> Name" id="product_name" required/>
</div>
<div class="col-md-6">
<div class="input-group">
<div class="input-group-addon"><i class="mdi mdi-currency-ngn"></i></div>
<input class="form-control" type="number" required="" placeholder=" <?= $nomenclature ?> Cost Price" name="product_cp" id="product_cp" required/>
</div>
</div>
</div>
<div class="form-group m-t-40">
<div class="col-md-6">
<div class="input-group">
<div class="input-group-addon"><i class="mdi mdi-currency-ngn"></i></div>
<input class="form-control" type="number" required="" placeholder=" <?= $nomenclature ?> Selling Price" name="product_sp" id="product_sp" required/>
</div>
</div>
</div>
<div class="form-group m-t-40">
<div class="col-md-6">
<textarea class="form-control" type="text" required="" placeholder=" <?= $nomenclature ?> Description" id="product_des" required/></textarea>
</div>
<div class="col-md-6">
<input class="form-control" type="text" required="" placeholder=" <?= $nomenclature ?> Expiry Date" id="product_exp" onfocus="(this.type = 'date')" onblur="if (this.value == '') {
this.type = 'text'
}" required/>
</div>
</div>
<div class="form-group m-t-40">
<div class="col-md-6">
<div class="input-group">
<div class="input-group-addon"><i class="mdi mdi-clipboard-account"></i></div>
<select name="supplier" class="form-control form-control-line" type="text" id="supplier" placeholder="<?= $nomenclature ?> Supplier">
<option value="">Supplier</option>
<?php
foreach ($suppliers as $supplier) {
?>
<option value='<?= $supplier[DbStrings::$SUPPLIER_NAME] ?>'><?= $supplier[DbStrings::$SUPPLIER_NAME] ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="col-md-6">
<div class="input-group">
<div class="input-group-addon"><i class="mdi mdi-chevron-double-up"></i></div>
<select name="department" class="form-control form-control-line" type="text" id="department" placeholder="<?= $nomenclature ?> Department">
<option value="">Department</option>
<?php
foreach ($departments as $department) {
?>
<option value='<?= $department[DbStrings::$DEPARTMENT_NAME] ?>'><?= $department[DbStrings::$DEPARTMENT_NAME] ?></option>
<?php
}
?>
</select>
</div>
</div>
</div>
<div class="form-group m-t-40">
<div class="col-md-6">
<div class="input-group">
<div class="input-group-addon"><i class="mdi mdi-chevron-up"></i></div>
<select name="sub_departments" class="form-control form-control-line" type="text" id="sub_department" placeholder="<?= $nomenclature ?> Department">
<option value="">Sub-Department</option>
<?php
foreach ($sub_departments as $sub_department) {
?>
<option value='<?= $sub_department[DbStrings::$SUB_DEPARTMENT_NAME] ?>'><?= $sub_department[DbStrings::$SUB_DEPARTMENT_NAME] ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="col-md-6">
<div class="input-group">
<div class="input-group-addon"><i class="mdi mdi-cash"></i></div>
<input class="form-control" type="number" placeholder=" <?= $nomenclature ?> Vat" id="vat"/>
</div>
</div>
</div>
<div class="form-group text-center m-t-20">
<div class="col-xs-12">
<button class="btn btn-primary btn-login btn-lg btn-block text-uppercase waves-effect waves-light" type="submit" name="product-btn">Add <?= $nomenclature ?></button>
</div>
</div>
</div> <!-- added in the edit -->
</div> <!-- added in the edit -->
</form>
<?php
}
}
?>
Here is the AJAX code:
function addProduct() {
var product_code = $("#product_code").val();
var product_name = $("#product_name").val();
var product_cp = $("#product_cp").val();
var product_sp = $("#product_sp").val();
var product_des = $("#product_des").val();
var product_exp = $("#product_exp").val();
var supplier = $("#supplier").val();
var department = $("#department").val();
var sub_department = $("#sub_department").val();
var vat = $("#vat").val();
var addUrl = "home/addproduct";
addUrl += "/" + product_code;
addUrl += "/" + product_name;
addUrl += "/" + product_cp;
addUrl += "/" + product_sp;
addUrl += "/" + product_des;
addUrl += "/" + product_exp;
addUrl += "/" + supplier;
addUrl += "/" + department;
addUrl += "/" + sub_department;
addUrl += "/" + vat;
$.ajax({type: 'GET', url: addUrl, data: {},
success: function (result) {
$.alert({
content: result
});
$("#product_code").val("");
$("#product_name").val("");
$("#product_cp").val("");
$("#product_sp").val("");
$("#product_des").val("");
$("#product_exp").val("");
$("#supplier").val("");
$("#department").val("");
$("#sub_department").val("");
$("#vat").val("");
location.reload();
},
error: function (xhr, status, error) {
$.alert({
content: 'Could not complete the process. ' + error
});
}
});
}
Here is the Controller Function:
private function addproduct($product_code = null, $product_name = null, $product_cp = null, $product_sp = null, $product_des = null, $product_exp = null, $supplier = null, $department = null, $sub_department = null, $vat = null) {
if (isset($product_code, $product_name, $product_cp, $product_sp, $product_des, $product_exp, $supplier, $department, $sub_department, $vat)) {
$email = $_SESSION[DbStrings::$EMAIL];
$product_code = $this->test_input($product_code);
$product_name = $this->test_input($product_name);
$product_cp = $this->test_input($product_cp);
$product_sp = $this->test_input($product_sp);
$product_des = $this->test_input($product_des);
$supplier = $this->test_input($supplier);
$department = $this->test_input($department);
$sub_department = $this->test_input($sub_department);
$vat = $this->test_input($vat);
$product_exp = strtotime($product_exp);
$insertedProduct = $this->member->insertProduct($email, $product_code, $product_name, $product_cp, $product_sp, $product_des, $supplier, $department, $sub_department, $vat, $product_exp);
if ($insertedProduct) {
echo "Your " . $_SESSION[AppStrings::$NOMENCLATURE] . " has been added succesfully";
} else {
echo "There was a problem inserting your " . $_SESSION[AppStrings::$NOMENCLATURE] . ". Please try again.";
}
} else {
echo 'Please fill all fields';
}
}
And this is the Model that does the insertion before sending back a result:
public function insertProduct($email, $product_code, $product_name, $product_cp, $product_sp, $product_des, $supplier, $department, $sub_department, $vat, $product_exp) {
$data = array(
DbStrings::$PRODUCTID => "",
DbStrings::$EMAIL => $email,
DbStrings::$PRODUCT_CODE => $product_code,
DbStrings::$PRODUCT_NAME => $product_name,
DbStrings::$PRODUCT_COST => $product_cp,
DbStrings::$PRODUCT_SELLING => $product_sp,
DbStrings::$PRODUCT_MARKUP => 9,
DbStrings::$PRODUCT_DESCRIPTION => $product_des,
DbStrings::$SUPPLIER => $supplier,
DbStrings::$DEPARTMENT => $department,
DbStrings::$SUB_DEPARTMENT => $sub_department,
DbStrings::$VAT => $vat,
DbStrings::$STOCK_BALANCE => 1,
DbStrings::$MIN_LEVEL => 1,
DbStrings::$MAX_QUANTITY => 1,
DbStrings::$QUANTITY_SOLD => 1,
DbStrings::$EXPIRY_DATE => $product_exp,
DbStrings::$DATE_CREATED => time(),
DbStrings::$DATE_STOCKED => time()
);
return $this->db->insert(DbStrings::$PRODUCTS_TABLE_NAME, $data);
}
I still get an error from the server, and don't know what else to do.
Correcting the above issue, the code works fine. I used: $this->output->enable_profiler(TRUE); and i was able to see the MySQL query sent by the model to the server, and when i copied the query and pasted it in my localhost:phpmyadmin, i got flagged for errors and there seem to be no syntax error.
Now this is creeping me out, as i can't fix it.

Pass php variable to html input type tag in php

In PHP, I need to pass a variable to a html input type tag. How can I pass this?
I have tried to pass the html tag id, but nothing is happening.
Here my mysql code:
include('db_connection.php');
if (isset($_POST['submit1'])) {
$skills = $_POST["skills"];
}
if (isset($_POST['submit1'])) {
$skills = $_POST["skills"];
//print_r($skills);die;
}
$sql ="SELECT * FROM `tbl_master_property` WHERE `name` ='$skills'";
$result=$conn->query($sql);
while( $row = $result->fetch_assoc() ) {
$project[] = $row;
}
foreach ($project as $value) {
$pg_address = $value['pg_address'];
$pg_type = $value['pg_type'];
$pg_owner = $value['pg_incharge_name'];
$pg_mobile = $value['pg_incharge_mobile'];
//print_r($pg_mobile);die;
}
Here my html code:
<div class="search">
<label>PG Name</label>
<input type="text" id="pgname" placeholder="Search by PG Name" name="pgname" list="locations" class="searchlocation" />
<button type="submit1" id ="submit1" class="searchButton">
<i class="fa fa-search searchButtonSearch"></i>
</button>
</div>
<script>
$(function() {
$( "#pgname" ).autocomplete({
source: "autocomplete.php"
});
});
</script>
<div class="col-lg-12 form-group">
<label>Address</label>
<input class="border col-lg-12" type="text" name= "address" value="<?php echo $value['pg_address']; ?>" required/>
</div>
<div class="col-lg-12 form-group">
<label>PG Owner Name</label>
<input class="border" type="text" name= "pg_owner" value="<?php echo $value['pg_owner ']; ?> required />
</div>
Does this work?
<?php
include('db_connection.php');
if (isset($_POST['submit1'])) {
$skills = $_POST["skills"];
}
$sql ="SELECT * FROM `tbl_master_property` WHERE `name` LIKE '%$skills%'";
$result=$conn->query($sql);
while( $row = $result->fetch_assoc() ) {
$project[] = $row;
}
?>
<form action="" method="post">
<div class="search">
<label>PG Name</label>
<input type="text" id="skills" placeholder="Search by PG Name" name="skills" list="locations" class="searchlocation" />
<button type="submit1" id ="submit1" class="searchButton">
<i class="fa fa-search searchButtonSearch">Search</i>
</button>
</div>
</form>
<?php
foreach ($project as $value) {
$pg_address = $value['pg_address'];
$pg_type = $value['pg_type'];
$pg_owner = $value['pg_incharge_name'];
$pg_mobile = $value['pg_incharge_mobile'];
//print_r($pg_mobile);die;
?>
<script>
$(function() {
$( "#pgname" ).autocomplete({
source: "autocomplete.php"
});
});
</script>
<div class="col-lg-12 form-group">
<label>Address</label>
<input class="border col-lg-12" type="text" name= "address" value="<?php echo #$value['pg_address']; ?>" required/>
</div>
<div class="col-lg-12 form-group">
<label>PG Owner Name</label>
<input class="border" type="text" name= "pg_owner" value="<?php echo #$value['pg_owner']; ?>" required/>
</div>
<?php } ?>
Check this out
<div class="search">
<label>PG Name</label>
<input type="text" id="pgname" placeholder="Search by PG Name" name="pgname" list="locations" class="searchlocation" />
<button type="submit1" id ="submit1" class="searchButton">
<i class="fa fa-search searchButtonSearch"></i>
</button>
</div>
<script>
$(function() {
$( "#pgname" ).autocomplete({
source: "autocomplete.php"
});
});
</script>
<div class="col-lg-12 form-group">
<label>Address</label>
<input class="border col-lg-12" type="text" name= "address" value="<?php if(isset( $pg_address)) echo $pg_address; ?>" required/>
</div>
<div class="col-lg-12 form-group">
<label>PG Owner Name</label>
<input class="border" type="text" name= "pg_owner" value="<?php if(isset( $pg_owner) )echo $pg_owner; ?> required />
</div>

Codeigniter - Forms with multiple dropzones for Picture Gallery & PDF files

Hi I'm working on a real estate project. I have a form which has some inputs & then 3 forms incorporated in the 1st form which have the dropzone class for a simple picture, a picture gallery & pdf files. I tried a lot of techniques but couldn't get the dropzones to work with the main form.
My Issues:
Dropzone sends the inputs & picture before submit button gets hit.
If after that, the submit button gets clicked then some inputs get null & 2 records get inserted.
If I remove or hide the submit button or change it to anchor tag for redirecting then the picture doesn't get uploaded.
Listings_controller:
function insert_listing(){
$pass = rand(0,999);
$data_seller = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'cell' => $this->input->post('cell'),
'sms' => $this->input->post('sms'),
'pass' => MD5($pass),
'status' => 'seller',
'last_date' => date('Y-m-d')
);
$result1 = $this->users_model->add_seller($data_seller);
$data_listing = array(
'code' => $this->input->post('code'),
'address' => $this->input->post('address'),
'city' => $this->input->post('city'),
'state' => $this->input->post('state'),
'zipcode' => $this->input->post('zipcode'),
'process' => $this->input->post('process'),
'step' => $this->input->post('step'),
'price' => $this->input->post('price'),
'agent_id' => $this->input->post('agent'),
'seller_id' => $this->db->insert_id()
);
$result2 = $this->listings_model->add_listing($data_listing);
if($result2){
$this->session->set_flashdata('success','Listing Added Successfully');
}else{
$this->session->set_flashdata('error','Listing Already Exists !');
}
//Dropzone - Pic
if(!empty($_FILES)){
$tempFile = $_FILES['file']['tmp_name'];
$fileName = substr(sha1(rand(000,9999)),0,7).$_FILES['file']['name'];
$targetPath = getcwd().'/assets/admin/images/image-gallery/';
$targetFile = $targetPath.$fileName;
move_uploaded_file($tempFile,$targetFile);
$listing_id = $result2['id'];
$data_listing_pic['listing_id'] = $listing_id;
$data_listing_pic['pic'] = $fileName;
$result3 = $this->listings_model->add_listing_pic($data_listing_pic);
}
redirect('admin/listings');
}
Users_model:
//Seller Add
function add_seller($data_seller){
//Check Duplicate
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('email',$data_seller['email']);
$query = $this->db->get();
if($query->num_rows() > 0){
$seller_id = $query->result_array();
$data_seller['id'] = $seller_id[0]['id'];
//Update Info
$this->db->where('email',$data_seller['email']);
$this->db->update($this->table,$data_seller);
return $data_seller;
}else{
//INSERT Seller
$result = $this->db->insert($this->table,$data_seller);
//GET Last Inserted ID
$seller_id = $this->db->insert_id();
$data_seller['id'] = $seller_id;
return $data_seller;
}
}
Listings_model:
//Listing Add
function add_listing($data_listing){
$result = $this->db->insert($this->table,$data_listing);
$listing_id = $this->db->insert_id();
$data_listing['id'] = $listing_id;
return $data_listing;
}
//Listing Pic Add
function add_listing_pic($data_listing_pic){
$result = $this->db->insert($this->pics_table,$data_listing_pic);
return $result;
}
My View:
<form action="<?php echo base_url();?>admin/insert_listing" class="dropzone dz-clickable" method="POST" enctype="multipart/form-data">
<input type="hidden" name="code" value="<?php echo $listing_code; ?>" />
<div class="row">
<div class="col-lg-6">
<strong>Property Information:</strong>
<br><br>
<div class="form-group form-float form-group-lg">
<div class="form-line">
<input type="text" class="form-control" name="address" autofocus required />
<label class="form-label">Street Address:</label>
</div>
<br>
<div class="form-line">
<input type="text" class="form-control" name="city" required />
<label class="form-label">City/Town:</label>
</div>
<br>
<div class="form-line">
<input type="text" class="form-control" name="state" required />
<label class="form-label">State:</label>
</div>
<br>
<div class="form-line">
<input type="text" class="form-control" name="zipcode" required />
<label class="form-label">Zip Code:</label>
</div>
</div>
</div>
<div class="col-lg-6">
<strong>Seller Information:</strong>
<br><br>
<div class="form-group form-float form-group-lg">
<div class="form-line">
<input type="text" class="form-control" name="name" required />
<label class="form-label">Full Name(s):</label>
</div>
<br>
<div class="form-line">
<input type="email" class="form-control" name="email" required />
<label class="form-label">Seller's Email:</label>
</div>
<br>
<div class="form-line">
<input type="text" class="form-control" name="cell" required />
<label class="form-label">Cell Phone Number:</label>
</div>
<br>
<div class="demo-checkbox">
<input type="checkbox" id="md_checkbox_26" name="sms" class="filled-in chk-col-blue" checked />
<label for="md_checkbox_26">Send status updates via SMS</label>
</div>
</div>
</div>
</div>
<hr />
<div class="row">
<div class="col-lg-6">
<strong>Listing Agent:</strong>
<br /><br />
<select class="form-control show-tick" name="agent" required >
<option value="">-- Please choose from the drop down --</option>
<?php for($j=0; $j < count($agent_list); $j++){ ?>
<option value="<?php echo $agent_list[$j]['id']; ?>" ><?php echo $agent_list[$j]['name']; ?></option>
<?php } ?>
</select>
<br />
</div>
<div class="col-lg-6">
<strong>Listing Status Information:</strong>
<br /><br />
<select class="form-control show-tick" name="process" required >
<option value="">-- Please choose from the drop down --</option>
<option value="Listing Process">Listing Process</option>
</select>
<br /><br />
<select class="form-control show-tick" name="step" required >
<option value="">-- Please choose from the drop down --</option>
<option value="1">Step 1: The Prep</option>
<option value="2">Step 2: File Set Up</option>
<option value="3">Step 3: Pre-listing Checklists</option>
<option value="4">Step 4: Sign Installation</option>
<option value="5">Step 5: Feature Sheets</option>
<option value="6">Step 6: Open Houses</option>
<option value="7">Step 7: You Received an Offer!</option>
</select>
</div>
</div>
<hr />
<div class="row">
<div class="col-lg-12">
<strong>Photo and Price:</strong>
<br><br>
<div class="form-group form-float form-group-lg">
<div class="form-line">
<input type="text" class="form-control" name="price" required />
<label class="form-label">Listing Price: $</label>
</div>
</div>
<form action="<?php echo base_url();?>admin/insert_listing_pdf" id="PDFUpload" class="dropzone dz-clickable" method="POST" enctype="multipart/form-data">
<div class="dz-message">
<div class="drag-icon-cph">
<i class="material-icons">touch_app</i>
</div>
<h3>Drop the Photo here or Click to Upload.</h3>
<em>(It is recommended to upload the most attractive photo that will catch the user's eye)</em>
</div>
</form>
</div>
</div>
<hr />
<div class="row">
<div class="col-lg-6">
<strong>Upload Gallery Photos:</strong>
<br><br>
<form action="<?php echo base_url();?>admin/insert_listing_gallery" id="GalleryUpload" class="dropzone dz-clickable" method="POST" enctype="multipart/form-data">
<div class="dz-message">
<div class="drag-icon-cph">
<i class="material-icons">touch_app</i>
</div>
<h3>Drop files here or click to upload.</h3>
<em>(Upload all the gallery photos of the property in this section)</em>
</div>
</form>
</div>
<div class="col-lg-6">
<strong>Upload PDF Files:</strong>
<br><br>
<form action="<?php echo base_url();?>admin/insert_listing_pdf" id="PDFUpload" class="dropzone dz-clickable" method="POST" enctype="multipart/form-data">
<div class="dz-message">
<div class="drag-icon-cph">
<i class="material-icons">touch_app</i>
</div>
<h3>Drop files here or click to upload.</h3>
<em>(Upload all the PDF files related to the property in this section)</em>
</div>
</form>
</div>
</div>
<hr />
<div class="row">
<div class="col-lg-12">
<button type="submit" class="btn btn-primary btn-lg waves-effect">ADD LISTING</button>
</div>
</div>
</form>

Categories