automatically change and can edit unit price in php, ajax - php

If I select item name then automatically change price in unit price text box.
But the problem is if I edit the unit price and change again item name, price not change according to item name. Last edited value of unit price display for every item name. What is the solution for this.
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Item Name</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<select class="form-control select2" class="form-control" onchange="fetch_select(this.value);">
<option selected="selected">Select a Category</option>
<?php
$select = mysqli_query($con, "select ItemName from bincard group by ItemName");
while ($row = $select->fetch_assoc()){
echo "
<option value=".$row['ItemName'].">".$row['ItemName']."</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Unit Price</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<textarea rows="1" cols="1" style="resize: none;" class="form-control select2"
class="form-control" id="UnitPrice" name="UnitPrice"></textarea>
</div>
</div>
<script type="text/javascript">
function fetch_select(val) {
$.ajax({
type: 'post',
url: 'fetch_data.php',
data: {
get_option: val
},
success: function(response) {
document.getElementById("UnitPrice").innerHTML = response;
}
});
}
</script>
****** fetch_data.php ******
<?php
include ("common/DBCon.php");
if (isset($_POST['get_option']))
{
$ECatagory = $_POST['get_option'];
$find = mysqli_query($con, "select SellingPrice from bincard where ItemName='$ECatagory'");
while ($row = $find->fetch_assoc())
{
echo "" . $row['SellingPrice'] . "";
}
exit;
}
?>

I think you must set id to select box and try:
$(function(){
$("#your select id").change(function(){
fetch_select($(this).val());
});
});

Related

How to auto fill textbox depending upon value selected from dropdown

I have created one IMS System. In that i have to create bill on order page and in order page when i select customer name from drop down than customer_address,customer_phone and gst number should automatically fill in text box.In database i have created one table named partys in that all data are available(Customer_name,Customer_address,customer_phone and gst) If anybody knows solution than please help.
Below is my code
<?php
$dbc = mysqli_connect('localhost', 'root', '', 'stock')
or die('Error connecting to MySQL server.');
$query = "SELECT * FROM partys";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['party_name'] ?>"><?php echo $row['party_name'] ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="form-group">
<label for="gross_amount" class="col-sm-5 control-label" style="text-align:left;">Customer Address</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="customer_address" name="customer_address" placeholder="Enter Customer Address" autocomplete="off">
</div>
</div>
<div class="form-group">
<label for="gross_amount" class="col-sm-5 control-label" style="text-align:left;">Customer Phone</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="customer_phone" name="customer_phone" placeholder="Enter Customer Phone" autocomplete="off">
</div>
</div>
<div class="form-group">
<label for="gstin" class="col-sm-5 control-label" style="text-align:left;">GSTIN</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="gstin" name="gstin" placeholder="Enter GST Number" autocomplete="off">
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
// On change of the dropdown do the ajax
$("#client").change(function() {
$.ajax({
// Change the link to the file you are using
url: '/create.php',
type: 'post',
// This just sends the value of the dropdown
data: {customer_name : party_name},
dataType: 'json',
success: function(response) {
// Parse the jSON that is returned
// Using conditions here would probably apply
// incase nothing is returned
var Vals = JSON.parse(response);
// These are the inputs that will populate
$("#customer_address").val(response.customer_address);
$("#customer_phone").val(response.customer_phone);
$("#gstin").val(response.gstin);
}
});
});
});
</script>

modal is not displaying data separately

I want to update my table using bootstrap dialog , but I when I retrieve my datatable from server side and embed php code into my modal to show the table data into the modal, and choose what the user wants to update in that form. it is showing the data from every row of my table into the modal , how can I fix it?
form_modal
<form id="product_update">
<div class="row">
<div class="form-group">
<?php foreach ($product as $value): ?>
<div class="col-xs-12">
<label for="description">Name: </label>
<input type="text" class="form-control" id="description" name="description" title="product description" value="<?php echo $value['descripcion']; ?>" required>
<div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-xs-8 col-sm-6">
<label for="cost_price">Cost Price:</label>
<div class="input-group"> <span class="input-group-addon">$</span>
<input type="text" class="form-control input-group-lg reg_name" id="cost_price" name="cost_price" title="cost_price" placeholder="Last name" value="<?php echo $value['precio_compra']; ?>" required>
</div>
</div>
<div class="col-xs-8 col-sm-6">
<label for="selling_price">Selling price: </label>
<input type="text" class="form-control input-group-lg reg_name" id="selling_price" name="selling_price" title="selling_price" placeholder="Last name" value="<?php echo $value['precio_venta']; ?>" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-xs-8 col-sm-6">
<label for="wprice">Wholeprice: </label>
<input type="text" class="form-control" id="wprice" name="wprice" title="wprice" value="<?php echo $value['precio_mayoreo']; ?>" required>
</div>
<div class="col-xs-8 col-sm-6">
<label for="min_stock">Min stock: </label>
<input type="text" class="form-control" id="min_stock" name="min_stock" title="min_stock" value="<?php echo $value['existencia_minima']; ?>" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-xs-8 col-sm-6">
<label for="stock">Stock: </label>
<input type="text" class="form-control" id="stock" name="stock" title="stock" value="<?php echo $value['existencia']; ?>" required>
</div>
<div class="col-xs-8 col-sm-6">
<label for="max_stock">Max stock: </label>
<input type="text" class="form-control" id="max_stock" name="max_stock" title="max_stock" value="<?php echo $value['existencia_maxima']; ?>" required>
</div>
</div>
</div>
<?php endforeach ?>
<div class="row">
<div class="form-group">
<div class="col-xs-8 col-sm-6">
<label for="provider">Provider: </label>
<select name="select-provider" id="select-provider">
<option value="0">Select a provider</option>
<?php foreach ($data as $value): ?>
<option value="<?php echo $value['id']; ?>"><?php echo $value['first_name'].' '.$value['last_name'] ?></option>
<?php endforeach ?>
</select>
</div>
</div>
</div>
</form>
modal js
$('#example tbody').on('click', 'a', function(event) {
event.preventDefault();
var data = table.row($(this).parents('tr')).data();
hash = data[0];
$("#product_update").validate();
BootstrapDialog.show({
type: BootstrapDialog.TYPE_WARNING,
message: function(dialog) {
var $message = $('<div></div>');
var pageToLoad = dialog.getData('pageToLoad');
$message.load(pageToLoad);
return $message;
},
data: {
'pageToLoad': URL_GET_VIEW_PRODUCT_UPDATE
},
closable: false,
buttons: [{
id: 'btn-ok',
cssClass: 'btn-primary',
icon: 'glyphicon glyphicon-send',
label: ' Save',
action: function(e) {
var description = $('#description').val();
var description = $('#description').val();
var cost_price = $('#cost_price').val();
var selling_price = $('#selling_price').val();
var wprice = $('#wprice').val();
var min_stock = $('#min_stock').val();
var stock = $('#stock').val();
var max_stock = $('#max_stock').val();
var provider_id = $('#select_provider').val();
if ($("#product_update").valid()) {
$.ajax({
url: URL_GET_UPDATE_PRODUCT,
type: 'POST',
data: { hash: hash, provider_id: provider_id, description: description, cost_price: cost_price, selling_price: selling_price, wprice: wprice, min_stock: min_stock, stock: stock, max_stock: max_stock },
success: function(data) {
console.log(data);
if (data.msg == 'successfully updated') {
$('#product_update')[0].reset();
table.ajax.reload();
} else if (data.min_stock == 'el stock no puede ser mayor al min') {
BootstrapDialog.show({
type: BootstrapDialog.TYPE_DANGER,
message: 'el stock no puede ser mayor al min'
});
}
}
});
}
}
},{
id: 'btn-cancel',
cssClass: 'btn-danger',
icon: 'glyphicon glyphicon-remove',
label: ' Cancel',
action: function(e) {
e.close();
}
}]
});
});
model product
public function datatable(){
$this->db->select('hash_id,codigo,descripcion,precio_compra,precio_venta,precio_mayoreo,existencia_minima,existencia,existencia_maxima,storelte_articulos.status');
$this->db->from('storelte_articulos');
$query = $this->db->get();
return $query->result_array();
}
public function isExistsProduct($data){
$this->db->select('descripcion');
$this->db->from('storelte_articulos');
$this->db->where('descripcion',$data['descripcion']);
$query = $this->db->get();
return $query->num_rows() == 0 ? false : true;
}
public function addProduct($data){
$query = 'UPDATE storelte_articulos SET hash_id = MD5(id) WHERE id = LAST_INSERT_ID()';
$this->db->insert('storelte_articulos',$data);
$this->db->query($query);
}
public function updateProduct($data) {
$this->db->where('md5(id)',$this->input->post('hash'));
$this->db->update('storelte_articulos',$data);
}
public function get_product() {
$this->db->select('codigo,descripcion,precio_compra,precio_venta,precio_mayoreo,existencia_minima,existencia_maxima,existencia');
$this->db->from('storelte_articulos');
$this->db->where('md5(id)', $this->input->post('hash'));
$query = $this->db->get();
return $query->result_array();
}
modal img
I suspect your query is not targeting the single row that you intend to access.
If you are not querying on your table Primary/Unique Key, you should add LIMIT 1 --but really you should be using the PK.
Also, you seem to be looping through all of the results from the query.
You'll need to post more of your code, because I can only make assumptions from what you've provided.
Why are you putting the Warning element at the top?
Where did you copy/paste your scripts from?
public function datatable(){
$this->db->select('hash_id,codigo,descripcion,precio_compra,precio_venta,precio_mayoreo,existencia_minima,existencia,existencia_maxima,storelte_articulos.status');
You will need some sort of $this->db->where() statement here that references the appropriate row id.
like: $this->db->where('id',$this->input->post('id'));
$this->db->from('storelte_articulos');
$query = $this->db->get();
return $query->result_array();
}
$this->db->select('hash_id,codigo,descripcion,precio_compra,precio_venta,precio_mayoreo,existencia_minima,existencia,existencia_maxima,storelte_articulos.status');
You should add WHERE clause here. Basically you are selecting all rows if you don't add any kind of filtering in that clause. For example:
$this->db->select('hash_id,codigo,descripcion,precio_compra,precio_venta,precio_mayoreo,existencia_minima,existencia,existencia_maxima,storelte_articulos.status' WHERE hash_id='YOURHASHID')

Fetch Database Value in multiple textbox while selecting dropdown in php

I have two pages add_admin.php and ajax_admin.php when i select name in dropdown it display FIRSTNAME, MIDDLENAME and LASTNAME in TEXTBOXES.
HERE is My Code:
add_admin.php
<!-- Department -->
<div class="col-xs-12 col-sm-12 col-md-12" >
<div class="form-group">
<select id="faculty_name" name="faculty_name" class="form-control" onchange='fetch_select(this.value)' required>
<option selected="selected" disabled="disabled">Please Select Faculty</option>
<?php
$query = mysql_query("select * from faculty_details");
while($row = mysql_fetch_array($query))
{
?>
<option value="<?php echo $row['FACULTY_ID'];?>"><?php echo $row['FIRSTNAME']." ".$row['MIDDLENAME']." ".$row['LASTNAME'];?></option>
<?php
}
?>
</select>
</div>
</div>
<!-- First Name -->
<div class="col-xs-12 col-sm-4 col-md-4">
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-fw fa-user"></i></div>
<input type="text" id="fname" name="fname" class="form-control input-md" placeholder="First Name" value="">
</div>
</div>
</div>
other text box are there contaisn middlename, lastname
Ajax Code
function fetch_select(val)
{
$.ajax
({
type: 'post',
url: 'ajax_admin.php',
data:
{
get_option:val
},
success: function (response)
{
$('#fname').val(response);
}
});
ajax_admin.php
<?php
if(isset($_POST['get_option']))
{
$state = $_POST['get_option'];
$find=mysql_query("select * from faculty_details where FACULTY_ID=$state");
while($row=mysql_fetch_array($find))
{
echo "$row[FIRSTNAME]";
}
}
?>
Get the result as json from the server so that you can easily use it in jQuery:
ajax-admin.php
if(isset($_POST['get_option']))
{
$state = $_POST['get_option'];
$row1=array();
$find=mysql_query("select firstname,middlename,lastname from faculty_details where FACULTY_ID=$state");
while($row=mysql_fetch_array($find))
{
$row1[]=$row;
}
die(json_encode($row1));
}
Ajax Code
function fetch_val(val) {
$.ajax({
url:"ajax-admin.php",
type:"POST",
data:{"get_option":val},
dataType:"JSON",
success:function(data){
$('#fname').val((data[0].firstname));
$('#mname').val((data[0].middlename));
$('#lname').val((data[0].lastname));
}
});
}

how to append form set on select dropdow

i don't know where to start. i have a dropdown list and onchange it will pass 2 values. from it i need to append form set base on setting (select query from database from the value i pass)
index.php
<div class="form-group">
<label for="add-name" class="col-sm-4 control-label">Staff</label>
<div class="col-sm-8">
<?php
$sqlEmpSelect = "select * from payroll_emp order by emp_name";
$qryEmpSelect = sqlsrv_query( $conn, $sqlEmpSelect );
?>
<select name="dpEmp" id="dpEmp" class="form-control">
<option>Sila Pilih</option>
<?php while( $rowAllow = sqlsrv_fetch_array( $qryEmpSelect, SQLSRV_FETCH_ASSOC) ) { ?>
<option value="<?=$rowAllow['emp_id']?>"><?=$rowAllow['emp_name']?></option>
<?php
}
sqlsrv_free_stmt($qryEmpSelect);
?>
</select>
</div>
</div>
<div class="form-group">
<label for="add-template" class="col-sm-4 control-label">Template</label>
<div class="col-sm-8">
<select name="dpTemp" id="dpTemp" class="form-control">
<option value="">Sila Pilih</option>
<?php
$sqlTemp = sprintf("select * from payroll_template order by template_name");
$qryTemp = mysql_query($sqlTemp) or die(mysql_error());
while ($resTemp = mysql_fetch_assoc($qryTemp)) {
?>
<option value="<?php echo $resTemp['template_id']; ?>"/><?php echo $resTemp['template_name']; ?></option>
<?php } ?>
</select>
</div>
</div>
<div id="div-data" name="div-data"></div>
here the jquery i try to use it..
$('#div-data').hide();
$('#dpTemp').change(function(){
var empvalue = $('#dpEmp').val();
var tempvalue = $('#dpTemp').val();
if(tempvalue != ""){
$.ajax({
type:'get',
url:'kew8/htmlforms.php',
data:'empid=' + empvalue + '&tempid=' + tempvalue,
success:function(html){
$('#div-data').html(html);
}
});
} else{
$('#div-data').hide();
}
});
here i try to call the forms set..
htmlforms.php
<?php
$sqlselect = "bla bla bla";
$result = "";
if ($result[0]['seeting1'] == 1) {
?>
<div class="form-group">
<label for="add-template" class="col-sm-4 control-label">Template</label>
<div class="col-sm-8">
<select name="dpTemp" id="dpTemp" class="form-control">
<option value="">Sila Pilih</option>
</select>
</div>
</div>
<div class="form-group">
<label for="add-desc" class="col-sm-4 control-label">Keterangan</label>
<div class="col-sm-8">
<textarea id="txtDesc" name="txtDesc" class="form-control"></textarea>
</div>
</div>
<?php
}
if ($result[0]['seeting2'] == 2) {
?>
<div class="form-group">
<label for="add-template" class="col-sm-4 control-label">Template</label>
<div class="col-sm-8">
<select name="dpTemp" id="dpTemp" class="form-control">
<option value="">Sila Pilih</option>
</select>
</div>
</div>
<div class="form-group">
<label for="add-desc" class="col-sm-4 control-label">Keterangan</label>
<div class="col-sm-8">
<textarea id="txtDesc" name="txtDesc" class="form-control"></textarea>
</div>
</div>
<?php } ?>
and more setting

Unable to get value of default option in select2 jquery

I am using the same form for adding new product and editing product with id. When we add a new product, its categories are selected from the select2 dropdown. But when we edit the already existing product, I want to display its category as default option. I tried with initSelection but in this way only text is displayed and its value is undefined. My form HTML code is:
<form class="form-horizontal form-label-left" novalidate>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" >Category
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select class="select2_single form-control col-md-7 col-xs-12" ></select>
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" >Name <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="name" class="form-control col-md-7 col-xs-12" value="<?php echo $name; ?>" placeholder="Product Name" required="required" type="text">
</div>
</div>
</form>
and my jquery script is
<script>
$(document).ready(function () {
categories();
});
function categories() {
$.ajax({
'url' : 'category/data',
'success' : function(data){
var j = $.parseJSON(data);
var select2_ary = [];
$.each(j, function(k, v) {
select2_ary.push({
id : v['id'],
text : v['category']
});
});
var id = "<?php echo $id; ?>";
if(id != '') {
$(".select2_single").select2({
placeholder: "Select a Category",
initSelection: function(element, callback) {
callback({val: '3223', text: '<?php echo $id; ?>' });
},
data : select2_ary
});
}
else {
$(".select2_single").select2({
placeholder: "Select a Category",
data : select2_ary
});
}
}
});
}
</script>
I'm not sure about about the select2 version but its not the latest one.
When I submit this form, I am not getting the value of select2 default option. Please help!
you can try this:
$(".select2_single").select2({ placeholder: "Select a Category", data : select2_ary }).select2("val",'3223'); });

Categories