Calculate from 3 inputfield in dynamic form yii2 - yii2

In my dynamic form I need to calculate data from from 3 fields(qty,rate,discount) and pass it to unitdiscount. The formula will be - unitdiscount = ((qty*rate*discount)/100) . I already have onchange event(getValue) on inputfield qty and rate as I pass (qty*rate) to value. When I'm adding the new onchange(getUnitdiscount), the calcluted field value is not passed. In stead, I get unitdiscount. Also, not sure how can I calculate from 3 inputdields.
I calculated value with the help of Pass calculated data in a texbox not from any model in dynamic form yii2
My present code looks like -
_form
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, "[{$i}]rate")->label(false)->textInput(['maxlength' => true,'onchange' => 'getValue($(this))', 'onkeyup' => 'getValue($(this))','onchange' => 'getUnitdiscount($(this))', 'onkeyup' => 'getUnitdiscount($(this))','placeholder' => 'Rate']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, "[{$i}]qty")->label(false)->textInput(['maxlength' => true,'onchange' => 'getTotal($(this))', 'onkeyup' => 'getTotal($(this))','onchange' => 'getValue($(this))', 'onkeyup' => 'getValue($(this))','onchange' => 'getUnitdiscount($(this))', 'onkeyup' => 'getUnitdiscount($(this))','placeholder' => 'Qty']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, "[{$i}]free")->label(false)->textInput(['maxlength' => true,'onchange' => 'getTotal($(this))', 'onkeyup' => 'getTotal($(this))','placeholder' => 'Free']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, "[{$i}]discount")->label(false)->textInput(['maxlength' => true,'placeholder' => 'Discount']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 ">
<input type="text" class="form-control" id="productsales-<?= $i ?>-value">
</div>
<input type="text" class="form-control" id="productsales-<?= $i ?>-unitdiscount">
JS Function
<?php
/* start getting the product value */
$script = <<< JS
function getValue(item) {
var index = item.attr("id").replace(/[^0-9.]/g, "");
var total = current = next = 0;
var id = item.attr("id");
var myString = id.split("-").pop();
if(myString == "qty") {
fetch = index.concat("-rate");
} else {
fetch = index.concat("-qty");
}
temp = $("#productsales-"+fetch+"").val();
if(!isNaN(temp) && temp.length != 0) {
next = temp;
}
current = item.val();
if(isNaN(current) || current.length == 0) {
current = 0;
}
if(!isNaN(current) && !isNaN(next)) {
total =parseFloat((parseFloat(current) * parseFloat(next))).toFixed(2);
}
valueField = "productsales-".concat(index).concat("-value");
$("#"+valueField+"").val(total);
}
JS;
$this->registerJs($script, View::POS_END);
/* end getting the product value */
?>
<?php
/* start getting the product Unit discount */
$script = <<< JS
function getUnitdiscount(item) {
var index = item.attr("id").replace(/[^0-9.]/g, "");
var total = current = next = 0;
var id = item.attr("id");
var myString = id.split("-").pop();
if(myString == "qty") {
fetch = index.concat("-rate");
} else {
fetch = index.concat("-qty");
}
temp = $("#productsales-"+fetch+"").val();
if(!isNaN(temp) && temp.length != 0) {
next = temp;
}
current = item.val();
if(isNaN(current) || current.length == 0) {
current = 0;
}
if(!isNaN(current) && !isNaN(next)) {
total =parseFloat((parseFloat(current) * parseFloat(next))).toFixed(2);
}
unitdiscountField = "productsales-".concat(index).concat("-unitdiscount");
$("#"+unitdiscountField+"").val(total);
}
JS;
$this->registerJs($script, View::POS_END);
/* end getting the product Unit discount */
?>
Update -
I've changed the inptfields and put onchange(getUnitdiscount) in front of onchange(getUnitdiscount). Now I'm atleast getting ouput in both textbox unitdiscount and value.
I've tried with the following javascript which not giving correct result.
<?php
/* start getting the total udisc */
$script = <<< JS
function getUdisc(item) {
var index = item.attr("id").replace(/[^0-9.]/g, "");
var total = current = next = previous =0;
var id = item.attr("id");
var myString = id.split("-").pop();
if(myString == "qty") {
fetch2 = index.concat("-discount");
fetch1 = index.concat("-rate");
} else if(myString == "discount") {
fetch3 = index.concat("-qty");
fetch1 = index.concat("-rate");
} else {
fetch2 = index.concat("-discount");
fetch3 = index.concat("-qty");
}
temp = $("#productsales-"+fetch1+"").val();
if(!isNaN(temp) && temp.length != 0) {
next = temp;
}
current = item.val();
if(isNaN(current) || current.length == 0) {
current = 0;
}
previous = item.val();
if(isNaN(previous) || previous.length == 0) {
previous = 0;
}
if(!isNaN(current) && !isNaN(next) && !isNaN(previous)) {
total = parseFloat(current) + parseFloat(next) + parseFloat(previous);
}
udiscField = "productsales-".concat(index).concat("-udisc");
$("#"+udiscField+"").val(total);
}
JS;
$this->registerJs($script, View::POS_END);
/* end getting the total udisc */
?>
By this javascript if I put rate = 50.50, qty = 100, discount = 10 it should give result 160.50 but it's giving 70.50. (I've taken a simple formula as unitdiscount = rate + qty + discount to test if I am getting the values correctly, then I can change the formula to a complex one.)

Try this way :
function getUdisc(item) {
var index = item.attr("id").replace(/[^0-9.]/g, "");
var total = current = next = previous = 0;
var id = item.attr("id");
var myString = id.split("-").pop();
if (myString == "qty") {
fetch1 = index.concat("-discount");
fetch2 = index.concat("-rate");
} else if (myString == "discount") {
fetch1 = index.concat("-qty");
fetch2 = index.concat("-rate");
} else {
fetch1 = index.concat("-discount");
fetch2 = index.concat("-qty");
}
temp1 = $("#productsales-"+fetch1+"").val();
temp2 = $("#productsales-"+fetch2+"").val();
if (!isNaN(temp1) && temp1.length != 0) {
next = temp1;
}
if (isNaN(temp2) || temp2.length == 0) {
previous = temp2;
}
current = item.val();
if (isNaN(current) || current.length == 0) {
current = 0;
}
if (!isNaN(current) && !isNaN(next) && !isNaN(previous)) {
total = (parseFloat(current) + parseFloat(next) + parseFloat(previous)).toFixed(2);
}
udiscField = "productsales-".concat(index).concat("-udisc");
$("#"+udiscField+"").val(total);
}

Related

Grouping the tables with dropdown box

I am a beginner in google app script. So right now I am doing a project where users can sign in and can view their payment history. So for now it is just showing from 2020 until 2021. So I want your guys help on creating a dropdown box which states (eg : 2020 , 2021 ) so maybe if the user clicks 2020 then they can see the payment history of 2020 only. I really need your guys help in this thing. I have attached the link to my google app script and a image to explain myself better. Thank you guys.
https://script.google.com/d/1DdRKqUX__-ZITUgTZanQ_A7hUL1kcc0TZOeFmn58wYsX_o_7cqNExnYo/edit?usp=sharing - Link to my appscript
First image
Second Image
Here is a sample code you can refer with:
WebAppLogin.html (modifications)
<script>
function GetRecords() {
var spin = "<span class=\"spinner-border spinner-border-sm\" role=\"status\" aria-hidden=\"true\"></span>";
spin += " Loading...";
document.getElementById("LoginButton").innerHTML = spin;
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
google.script.run.withSuccessHandler(function(output) {
console.log(output);
var username = output[1];
var name = output[2];
if(output[0] == 'TRUE') {
document.getElementById("errorMessage").innerHTML = "";
document.getElementById("currentUser").value = username;
google.script.run.withSuccessHandler(displayTable).GetRecords(username,"None");
} else if(output[0] == 'FALSE') {
document.getElementById("firstLastName").innerHTML = "";
document.getElementById("currentUser").value = "";
document.getElementById("myFilter").innerHTML = "";
document.getElementById("errorMessage").innerHTML = "Failed to Login";
document.getElementById("LoginButton").innerHTML = "Login";
}
}).checkLogin(username, password);
}
function filter(){
var filterStr = document.getElementById("filterYear").value;
var user = document.getElementById("currentUser").value;
google.script.run.withSuccessHandler(displayTable).GetRecords(user,filterStr);
}
function displayTable(result) {
var ar = result.data;
var filterString = result.filter;
var username = document.getElementById("currentUser").value;
if(ar.length > 0) {
var displayTable = '<table class=\"table\" id=\"mainTable\" >';
displayTable += "<tr>";
displayTable += "<th>Month</th>";
displayTable += "<th>House Number</th>";
displayTable += "<th>Street</th>";
displayTable += "<th>Payment Status</th>";
displayTable += "</tr>";
ar.forEach(function(item, index) {
displayTable += "<tr>";
displayTable += "<td>"+item[0]+"</td>";
displayTable += "<td>"+item[1]+"</td>";
displayTable += "<td>"+item[2]+"</td>";
displayTable += "<td>"+item[3]+"</td>";
displayTable += "</tr>";
});
displayTable += "</table>";
} else {
var displayTable = "<span style=\"font-weight: bold\" >No Records Found</span>";
}
var filter = '';
if(filterString.length > 0) {
filter += '<label for="years" style="font-size: 20px">Years</label><br><select class="form-control form-control-sm" id="filterYear" name="years" required><option value="" selected>Choose...</option>';
filterString.forEach(str => {
filter += '<option value="'+str+'">'+str+'</option>';
});
filter += '</select><button class="btn btn-primary" type="button" id="FilterButton" onclick="filter()" >Submit</button>';
}
//var filter = '<label for="years" style="font-size: 20px">Years</label><br><select class="form-control form-control-sm" id="filterYear" name="years" required><option value="" selected>Choose...</option><option value="2020">2020</option><option value="2021">2021</option></select><button class="btn btn-primary" type="button" id="FilterButton" onclick="filter()" >Submit</button>';
document.getElementById("digitalgoods-030521182921-1").style.display = "block";
document.getElementById("displayRecords").innerHTML = displayTable;
document.getElementById("firstLastName").innerHTML = "USER: " + name;
document.getElementById("myFilter").innerHTML = filter;
document.getElementById("LoginButton").innerHTML = "Login";
document.getElementById("username").value = '';
document.getElementById("password").value = '';
}
</script>
<div>
<h2 id="firstLastName">
</h2>
</div>
<input type="hidden" id="currentUser" value=""/>
<div id ="myFilter" class="form-group">
</div>
</div>
<div id="displayRecords" style="padding: 10px;" >
</div>
Modifications done:
Include empty form-group class
Include hidden input to hold current logged-in user
Create a reusable function displayTable()
Create an html content for the drop-down filter. See variable filter.
Include another argument when calling GetRecords(username, filter)
Create a new function filter()
During initial log-in, filter will be set to "None". filter will be set depending on the option selected
Code.gs (modifications)
function GetRecords(username,filter) {
var filteredDataRangeValues = GetUsernameAssociatedProperties(username);
var resultArray = GetPaymentRecords(filteredDataRangeValues,filter);
var resultFilter = getYears();
result = {
data: resultArray,
filter: resultFilter
};
return result;
}
function getYears() {
var ss= SpreadsheetApp.openByUrl(url);
var yearSheet = ss.getSheetByName("Configuration");
var getLastRow = yearSheet.getLastRow();
var return_array = [];
for(var i = 2; i <= getLastRow; i++)
{
if(return_array.indexOf(yearSheet.getRange(i, 2).getDisplayValue()) === -1) {
return_array.push(yearSheet.getRange(i, 2).getDisplayValue());
}
}
return return_array;
}
function GetPaymentRecords(userProperties,filter) {
var transpose = m => m[0].map((_, i) => m.map(x => x[i]));
var resultArray = [];
var ss = SpreadsheetApp.openByUrl(url);
var displaySheet = ss.getSheetByName(streetSheetName);
var addressValues = displaySheet.getRange("B:C").getValues();
var paidMonthValues = displaySheet.getRange("G:AD").getValues();
//Logger.log(addressValues);
//Logger.log(transpose(paidMonthValues));
userProperties.forEach((v, i) => {
var userHouseNumber = v[1];
var userStreet = v[2];
var column = addressValues.reduce(function callbackFn(accumulator, currentValue, index, array) {
if (currentValue[0] == userHouseNumber && currentValue[1] == userStreet) {
return index
} else {
return accumulator
}
}, '');
//Logger.log(column);
Logger.log(filter)
Logger.log(paidMonthValues);
if(filter=="None"){
var result = transpose(paidMonthValues).map(function callbackFn(element, index, array) {
return [element[0], userHouseNumber, userStreet, element[column] || '']
});
}else{
var result = transpose(paidMonthValues).map(function callbackFn(element, index, array) {
if(element[0].includes(filter))return [element[0], userHouseNumber, userStreet, element[column] || '']
});
}
resultArray = resultArray.concat(result);
//Logger.log(resultArray);
})
//Remove null elements
resultArray = resultArray.filter(element=>{
Logger.log(element!=null)
return element != null;
});
return resultArray;
}
Modifications done:
Modified GetRecords() and GetPaymentRecords() to include filter option
Add removal of null elements in the resultArray. (Null elements may exist when filter option was used due to the map() used)
Output:
(After user logged-in)
(After user selects a filter)
(UPDATE):
The following modifications where done to create a drop-box based on the list of years available in the configuration sheet.
WebAppLogin.html
displayTable() was modified that will accept an object as its parameter which contains an array data and an array of filter strings.
displayTable() was modified to update the drop-down options based on the filter strings available
Code.gs
getYears() was added that will read the sheet "Configuration" to get the filter string values
GetRecords() was modified to return an object which contains an array of record data and an array of filter strings.

Calculate 3 fields and display the result in the 4th in yii2 dynamic form

I'm trying to calculate sum of 3 fields - si_mrp + si_discp + si_taxpc in yii2 dynamic form. This is the same solution of Calculate from 3 inputfield in dynamic form yii2. But the present solution is not working.
form fields
<?= $form->field($modelsProductsales, "[{$i}]si_mrp")->label(false)->textInput(['maxlength' => true,'onchange' => 'getUdisc($(this))', 'onkeyup' => 'getUdisc($(this))','class' => 'mrp','placeholder' => 'MRP']) ?>
<?= $form->field($modelsProductsales, "[{$i}]si_discp")->label(false)->textInput(['maxlength' => true,'onchange' => 'getUdisc($(this))', 'onkeyup' => 'getUdisc($(this))','class' => 'discp','placeholder' => 'Disc %']) ?>
<?= $form->field($modelsProductsales, "[{$i}]si_taxpc")->label(false)->textInput(['maxlength' => true,'onchange' => 'getUdisc($(this))', 'onkeyup' => 'getUdisc($(this))','class' => 'taxpc','placeholder' => 'Tax %']) ?>
<?= $form->field($modelsProductsales, "[{$i}]si_rate")->label(false)->textInput(['maxlength' => true,'class' => 'rate','placeholder' => 'Rate']) ?>
javascript code -
<?php
/* start getting the rate */
$script = <<< JS
function getUdisc(item) {
var index = item.attr("id").replace(/[^0-9.]/g, "");
var total = current = next = previous = 0;
var id = item.attr("id");
var myString = id.split("-").pop();
if (myString == "si_mrp") {
fetch1 = index.concat("-si_discp");
fetch2 = index.concat("-si_taxpc");
} else if (myString == "si_discp") {
fetch1 = index.concat("-si_mrp");
fetch2 = index.concat("-si_taxpc");
} else {
fetch1 = index.concat("-si_discp");
fetch2 = index.concat("-si_mrp");
}
temp1 = $("#sellitem-"+fetch1+"").val();
temp2 = $("#sellitem-"+fetch2+"").val();
if (!isNaN(temp1) && temp1.length != 0) {
next = temp1;
}
if (isNaN(temp2) || temp2.length == 0) {
previous = temp2;
}
current = item.val();
if (isNaN(current) || current.length == 0) {
current = 0;
}
if (!isNaN(current) && !isNaN(next) && !isNaN(previous)) {
total = (parseFloat(current) + parseFloat(next) + parseFloat(previous)).toFixed(2);
}
udiscField = "sellitem-".concat(index).concat("-si_rate");
$("#"+udiscField+"").val(total);
}
JS;
$this->registerJs($script, View::POS_END);
/* end getting the rate */
?>
Error
The si_mrp field seems to be not adding up
But, When I do the following calculation
total = (parseFloat(10.5) + parseFloat(11.5) + parseFloat(12.5)).toFixed(2);
I get the correct result.
Again I can see, Either si_mrp + si_discp or si_discp + si_taxpc happening but not si_mrp + si_discp + si_taxpc
Not sure what is wrong with this code. Please help.
Update this :
if (isNaN(temp2) || temp2.length == 0) {
previous = temp2;
}
to
if(!isNaN(temp2) && temp2.length != 0) {
previous = temp2;
}

Pass calculated data in a texbox not from any model in dynamic form yii2

I'm trying to get the product of two textboxes (qty, rate) from model productsales to a 3rd textbox (value) withing dynamic form yii2. It is exactly same as Yii2-dynamicforms and javascript, except, the 3rd textbox doesn't belong to any model.
_form
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, "[{$i}]qty")->label(false)->textInput(['maxlength' => true,'onchange' => 'getTotal($(this))', 'onkeyup' => 'getTotal($(this))','onchange' => 'getValue($(this))', 'onkeyup' => 'getValue($(this))','placeholder' => 'Qty']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, "[{$i}]free")->label(false)->textInput(['maxlength' => true,'onchange' => 'getTotal($(this))', 'onkeyup' => 'getTotal($(this))','placeholder' => 'Free']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 ">
<input type="text" class="form-control" id="value">
</div>
JS function -
<?php
/* start getting the product value */
$script = <<< JS
function getValue(item) {
var index = item.attr("id").replace(/[^0-9.]/g, "");
var total = current = next = 0;
var id = item.attr("id");
var myString = id.split("-").pop();
if(myString == "qty") {
fetch = index.concat("-rate");
} else {
fetch = index.concat("-qty");
}
temp = $("#productsales-"+fetch+"").val();
if(!isNaN(temp) && temp.length != 0) {
next = temp;
}
current = item.val();
if(isNaN(current) || current.length == 0) {
current = 0;
}
if(!isNaN(current) && !isNaN(next)) {
total = parseInt(current) * parseInt(next);
}
vALUE = "productsales-".concat(index).concat("-value");
$("#"+vALUE+"").val(value);
}
JS;
$this->registerJs($script, View::POS_END);
/* end getting the product value */
?>
This is not giving any output. Please let me know if any additional piece of code needed.
_form.php
<input type="text" class="form-control" id="productsales-<?= $i ?>-value">
JS
if(!isNaN(current) && !isNaN(next)) {
total = parseInt(current) * parseInt(next);
}
valueField = "productsales-".concat(index).concat("-value");
$("#"+valueField+"").val(total);
<?= $form->field($model, 'quantity')->textInput(['id' => "quantity"]) ?>
<?= $form->field($model, 'subtotal')->textInput(['id' => "price"]) ?>
<input type="text" id="total">
<?php
$script = <<<EOD
$(function() {
$('#quantity').keyup(function() {
updateTotal();
});
$('#price').keyup(function() {
updateTotal();
});
var updateTotal = function() {
var quantity = parseInt($('#quantity').val());
var price = parseInt($('#price').val());
var total = quantity * price;
$('#total').val(total);
};
});
EOD;
$this->registerJs($script);
?>

yii2 active record transaction still commit when the validation failed

I'm using Yii2 active record transaction, but the validation failed in the middle of the codes, it still commit the transaction.
Please advice.
public function actionCreate()
{
$model = new PoAgen();
$seq = Sequence::FindOne(['seq_id' => 'INV/AG', 'seq_name' => (int)date('ymd')]);
if(is_null($seq))
{
$_seq = new Sequence();
$_seq->seq_id = 'INV/AG';
$_seq->seq_name = (int)date('ymd');
$_seq->value = 0;
$_seq->save();
$model->trx_id = $_seq->seq_id . '/' . $_seq->seq_name . str_pad($_seq->value+1, 3, "0", STR_PAD_LEFT);
}
else {
$seq->value += 1;
$model->trx_id = $seq->seq_id . '/' . $seq->seq_name . str_pad($seq->value, 3, "0", STR_PAD_LEFT);
$seq->update();
}
$model->pot_cong = 0;
$model->pot_basah_kg = 0;
$model->pot_tangkai = 0;
$model->ppn = 0;
$model->pot_pinjaman = 0;
$model->pot_panjar = 0;
$model->ongkos_angkut = 0;
$model->pot_angkut = 0;
$model->is_transfer = true;
$model->buy_date = date('Y-m-d');
if ($model->load(Yii::$app->request->post())) {
$transaction = Yii::$app->db->beginTransaction();
try
{
$model->created_by = Yii::$app->user->identity->id;
$model->created_time = date('Y-m-d H:i:s');
$saldo = Saldo::findOne(1);
$kas = Kas::findOne(1000); // pembelian agen pks
$agen = Agen::findOne($model->agen_id);
if(!$model->is_transfer)
{
if($kas->code == 'D')
{
$saldo->balance -= $model->total_bayar;
}
else
{
$saldo->balance += $model->total_bayar;
}
$saldo->update();
}
$model->save();
// posting to kasbook
$kasbook = new KasBook();
$kasbook->kasbook_id = uniqid();
$kasbook->kas_date = $model->buy_date;
$kasbook->ref_trxid = $model->trx_id;
$kasbook->kas_id = $kas->kas_id;
$kasbook->code = $kas->code;
$kasbook->total = $model->total_bayar;
$kasbook->balance = $saldo->balance;
$kasbook->received_by = '['. $model->agen_id . '] ' . $agen->agen_name;
$kasbook->remark = 'Berat Bersih : ' . $model->r_bersih . ', Harga : ' . $model->price;
$kasbook->vehicle_id = $model->vehicle_id;
$kasbook->is_transfer = $model->is_transfer;
$kasbook->created_by = Yii::$app->user->identity->id;
$kasbook->created_time = date('Y-m-d H:i:s');
$kasbook->save();
if($model->ppn != 0)
{
$kb_1 = new KasBook();
$kb_1->kasbook_id = uniqid();
$kb_1->kas_date = $model->buy_date;
$kb_1->ref_trxid = $model->trx_id;
$kb_1->kas_id = 2000;
$kb_1->code = 'K';
$kb_1->total = $model->ppn;
$kb_1->balance = $saldo->balance;
$kb_1->remark = 'Ppn Pembelian Agen';
$kb_1->vehicle_id = $model->vehicle_id;
$kb_1->is_transfer = true;
$kb_1->created_by = Yii::$app->user->identity->id;
$kb_1->created_time = date('Y-m-d H:i:s');
$kb_1->save();
}
// kurang posting ke purchase order barang
$transaction->commit();
return $this->redirect(['view', 'id' => $model->trx_id]);
}
catch(Exception $e) {
$transaction->rollBack();
throw $e;
}
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
validation failed in if($model->ppn !=0) { ... }
because there is no any primary key in the master table, but the transaction still do commit and the model inserted to database.
Please advice. Thanks before.
save() method does not throw exceptions but returns boolean so you need to throw it explicitly. Replace every
...->save();
in try-catch with
if (! ...->save()) {
throw new \yii\db\Exception('Error while saving ... model!');
// or use general \Exception()
}
so now it can be caught.
You can do this by :
$flag=true;
if($model->ppn != 0)
{
$kb_1 = new KasBook();
$kb_1->kasbook_id = uniqid();
$kb_1->kas_date = $model->buy_date;
$kb_1->ref_trxid = $model->trx_id;
$kb_1->kas_id = 2000;
$kb_1->code = 'K';
$kb_1->total = $model->ppn;
$kb_1->balance = $saldo->balance;
$kb_1->remark = 'Ppn Pembelian Agen';
$kb_1->vehicle_id = $model->vehicle_id;
$kb_1->is_transfer = true;
$kb_1->created_by = Yii::$app->user->identity->id;
$kb_1->created_time = date('Y-m-d H:i:s');
if(!$kb_1->save())
{
$flag=false;
}
}
if($flag)
$transaction->commit();
else
$transaction->rollback();

image does not display during drag and drop in hosted site

I have a drag and drop function which seems fine displaying images during localhost but when I try it in a hosted site the problem occurs. It also seems to emit this error image corrupt or truncated in firebug, but when I download it and try it again in the localhost it seems fine. Any idea what the problem may be?
<?php
session_start();
?>
<?php
require "menu.php";
?>
<!DOCTYPE HTML>
<html>
<title>imageload</title>
<head>
<style type="text/css">
#div1 {width:255px;height:285px;border:1px solid #aaaaaa;z-index:1;}
</style>
<script>
function dress()
{
document.getElementById("shirt1").style.visibility = "visible";
document.getElementById("shirt2").style.visibility = "visible";
document.getElementById("shirt3").style.visibility = "visible";
}
function braces()
{
document.getElementById("brace1").style.visibility = "visible";
document.getElementById("brace2").style.visibility = "visible";
document.getElementById("brace3").style.visibility = "visible";
}
function shoes()
{
document.getElementById("shoe1").style.visibility = "visible";
document.getElementById("shoe2").style.visibility = "visible";
document.getElementById("shoe3").style.visibility = "visible";
}
var x = "waa";
var y = "waa";
var z = "waa";
var shoe = "waa";
var shirt = "waa";
var brace = "waa";
function AddCart()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("DivAddDrag").innerHTML=xmlhttp.responseText;
}
}
var formData = new FormData();
var q1 = document.getElementById("quantity1").value;
var q2 = document.getElementById("quantity2").value;
var q3 = document.getElementById("quantity3").value;
formData.append("shoe", shoe);
formData.append("shirt", shirt);
formData.append("brace", brace);
formData.append("q1", q1);
formData.append("q2", q2);
formData.append("q3", q3);
xmlhttp.open("POST","adddrag.php");
xmlhttp.send(formData);
}
function allowDrop(ev)
{
if(x=="shirt1" || x == "shirt2" || x =="shirt3")
{
document.getElementById(x).style.visibility = "hidden";
}
if(x=="shirt1" && y=="waa" && z=="waa")
{
// is this what you are referring to?
document.getElementById('image').src = "dragged/d1.jpg";
}
if(x=="shirt2" && y=="waa" && z=="waa")
{
document.getElementById('image').src = "dragged/d2.jpg";
}
if(x=="shirt3" && y=="waa" && z=="waa")
{
document.getElementById('image').src = "dragged/d3.jpg";
}
if(y=="brace1" || y == "brace2" || y =="brace3")
{
document.getElementById(y).style.visibility = "hidden";
}
if(z=="shoe1" || z == "shoe2" || z =="shoe3")
{
document.getElementById(z).style.visibility = "hidden";
}
if(x=="shirt1")
{
shirt = "upload/d1.jpg";
}
if(x=="shirt2")
{
shirt = "upload/d2.jpg";
}
if(x=="shirt3")
{
shirt = "upload/d3.jpg";
}
if(y=="brace1")
{
brace = "upload/b1.jpg";
}
if(y=="brace2")
{
brace = "upload/b2.jpg";
}
if(y=="brace3")
{
brace = "upload/b3.jpg";
}
if(z=="shoe1")
{
shoe = "upload/s1.jpg";
}
if(z=="shoe2")
{
shoe = "upload/s2.jpg";
}
if(z=="shoe3")
{
shoe = "upload/s3.jpg";
}
ev.preventDefault();
}
function drag(ev)
{
if(ev.target.id == "shirt1" || ev.target.id == "shirt2" || ev.target.id == "shirt3")
{
x = ev.target.id;
}
if(ev.target.id == "brace1" || ev.target.id == "brace2" || ev.target.id == "brace3")
{
y = ev.target.id;
}
if(ev.target.id == "shoe1" || ev.target.id == "shoe2" || ev.target.id == "shoe3")
{
z = ev.target.id;
}
ev.dataTransfer.setData("Text",ev.target.id);
}
function drop(ev)
{
ev.preventDefault();
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
</script>
</head>
<body>
<?php
require "header.php";
?>
<div id = "wrapper" style="background-color:white; width:1013px; margin-left: auto;margin-right: auto;height:auto;z-index:1100;" >
<?php
if ($_SESSION['users_userName'] == "user")
{
?>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)" style="position:relative;top:120px;left:730px;border: 5px solid #032472;height:500px;">
<img id ="image" src = "draggable/d1.jpg" style = "height:500px;width:255px;position:relative;bottom:30px;"/></div>
<?php
echo '<div style = "position:relative;left:50px;bottom:400px;">';
$sql = "select * from draggable Limit 6,3";
$data = mysql_query($sql,$con);
$y = 1;
while($row = mysql_fetch_array($data))
{
$id="shirt".$y;
echo '<img src="'.$row['Drag'].'" id = "'.$id.'" name="shirt" width="85px" height="85px" draggable="true" ondragstart="drag(event)" onClick="next()" style = "visibility:hidden;"/>';
$y=$y+1;
}
echo '<label> Quantity : </label>';
echo '<input type = "text" id = "quantity1" />';
echo '</br>';
$sql = "select * from draggable Limit 3";
$data = mysql_query($sql,$con);
$y = 1;
while($row = mysql_fetch_array($data))
{
$id="brace".$y;
echo '<img src="'.$row['Drag'].'" id = "'.$id.'" name="brace" width="85px" height="85px" draggable="true" ondragstart="drag(event)" onClick="next()" style = "visibility:hidden;"/>';
$y=$y+1;
}
echo '<label> Quantity : </label>';
echo '<input type = "text" id = "quantity2" />';
echo'</br>';
$sql = "select * from draggable Limit 3,3";
$data = mysql_query($sql,$con);
$y = 1;
while($row = mysql_fetch_array($data))
{
$id="shoe".$y;
echo '<img src="'.$row['Drag'].'" id = "'.$id.'" name="shoe" width="85px" height="85px" draggable="true" ondragstart="drag(event)" onClick="next()" style = "visibility:hidden;"/>';
$y=$y+1;
}
echo '<label> Quantity : </label>';
echo '<input type = "text" id = "quantity3" />';
echo'</br>';
echo '</div>';
?>
<button style = "position:relative;bottom:250px;left:30px;" onClick="AddCart()"> Add to Cart</button>
<div style ="position:relative;left:150px;bottom:750px;z-index:1300;">
<button id = "dressclick" onclick = "dress()"> Dress </button>
<button id = "shoeclick" onclick = "shoes()"> Shoes </button>
<button id = "braceclick" onclick = "braces()"> Bracelet </button>
</div>
<div id ="DivAddDrag" style ="position:relative;height:200px;width:500px;left:150px;bottom:400px;border:3px solid black"></div>
<?php
}
?>
<div class="push4">
</div>
</div>
<div id="footer4" style="margin-left:auto;margin-right:auto;width:1013px;"><?php require "footer.php";?></div>
</body>
</html>
It sounds like the link to your image is hard-coded for your local environment. You may need to make your path root-relative so its link is independent of deployment environment. What server OS are you running? Where is your code for us to see?