Numberbox jeasyui not showing value from variable - javascript

I have set value to a variable, but when I want to write down the value into numberbox, nothing is happen. I am working with datagrid jQuery Easyui here is my code
function update(){
var pesan='';
var dg =$(dgUpholdstery);
var id_cotation = '<?php echo $id_cotation ?>';
$.map(dg.datagrid('getChecked'), function(row){
var index = dg.datagrid('getRowIndex', row);
var harga=0;
var mat_waste='0';
var kode='';
harga = row.brg_harga;
mat_waste = row.mat_waste;
kode = row.material;
$.ajax({
type : 'POST',
url : "<?php echo site_url(); ?>/cotation/harga_cotation",
data : "kode="+kode+"&type=upholstery",
dataType : 'json',
cache : false,
success : function(data){
var $response=$(data);
if (harga!=$response[0]['harga']){
harga = $response[0]['harga'];
}
if (mat_waste!=$response[0]['mat_waste']){
mat_waste = $response[0]['mat_waste'];
}
}
});
dg.datagrid('beginEdit',index);
var ed_mat_waste = dg.datagrid('getEditor', {index:index, field:'mat_waste'});
var ed_brg_harga = dg.datagrid('getEditor', {index:index, field:'brg_harga'});
// this is part is not working
$(ed_mat_waste.target).numberbox('setValue',mat_waste);
//if I using the code bellow that I comment, it's work fine
//$(ed_mat_waste.target).numberbox('setValue','35');
cotation_uphold(index);
dg.datagrid('endEdit',index);
});
}
when I test to alert the variable, it's show the value
if I write direct value, it's working but if I put the variable, it's do nothing

I solved it.
here is the code
function update(){
var pesan='';
var dg =$(dgUpholdstery);
var id_cotation = '<?php echo $id_cotation ?>';
$.map(dg.datagrid('getChecked'), function(row){
var index = dg.datagrid('getRowIndex', row);
var harga=0;
var mat_waste=0;
var kode='';
harga = row.brg_harga;
mat_waste = row.mat_waste;
kode = row.material;
$.ajax({
type : 'POST',
url : "<?php echo site_url(); ?>/cotation/harga_cotation",
data : "kode="+kode+"&type=upholstery",
dataType : 'json',
cache : false,
success : function(data){
var $response=$(data);
if (harga!=$response[0]['harga'] && mat_waste!=$response[0]['mat_waste']){
dg.datagrid('beginEdit',index);
var ed_mat_waste = dg.datagrid('getEditor', {index:index, field:'mat_waste'});
var ed_brg_harga = dg.datagrid('getEditor', {index:index, field:'brg_harga'});
$(ed_brg_harga.target).numberbox('setValue',$response[0]['harga']);
$(ed_mat_waste.target).numberbox('setValue',$response[0]['mat_waste']);
cotation_uphold(index);
dg.datagrid('endEdit',index);
}
}
});
});
}

Related

ajax no funciton due e.preventdefault error and no update DB

few days ago I change my PHP version from 5.4 to 5.5 due to some code needed for render QR code. And today i found error in this function:
$(function changeStav() {
$("#selPismoSet<?php echo $id;?>").change(function() {
var del_id = $(this).attr("id");
del_id = del_id.replace("selPismoSet","");
var id = del_id;
var val = $('#selPismoSet<?php echo $id;?> option:selected').val();
$.ajax({
type: "GET",
url: "actions.php?action=changeStav",
data: 'id=' + id+ '&stav=' + val,
success: function(data){
e.preventDefault();
$('#content, #ok<?php echo $id;?>').html(data);
console.log();
}
});
return false;
});
});
I got this error:
I dont know where is the problem. Its possible that error is due to change PHP version?
You are missing an argument in your function. e is not defined
$(function changeStav() {
$("#selPismoSet<?php echo $id;?>").change(function(e) {
var del_id = $(this).attr("id");
del_id = del_id.replace("selPismoSet","");
var id = del_id;
var val = $('#selPismoSet<?php echo $id;?> option:selected').val();
$.ajax({
type: "GET",
url: "actions.php?action=changeStav",
data: 'id=' + id+ '&stav=' + val,
success: function(data){
e.preventDefault();
$('#content, #ok<?php echo $id;?>').html(data);
console.log();
}
});
return false;
});
});

Need Help In Script

My script code like :
function changePrice(id) {
var url = '<?php echo $base_url ?>home/getprice/';
$.ajax({
url: url,
type: 'post',
data: 'id='+id,
success: function(msg) {
alert(msg);
/*
"regular_price": "800",
"discount_price": 720
*/
}
});
}
I want to both regular price and discount price on separate variable.How??
If you are getting the response as "regular_price": "800", "discount_price": 720 then make it valid JSON, parse it and get the properties.
var obj = JSON.parse('{' + msg + '}');
// valid json -^-----------^-
// get object properties
var regular = data.regular_price;
var discount = data.discount_price;
UPDATE : If response data is valid JSON format then set dataType: 'json' option.
$.ajax({
url: url,
type: 'post',
data: 'id='+id,
// set response datatype as json
dataType:'json',
success: function(msg) {
// get properties
var regular = msg.regular_price;
var discount = msg.discount_price;
}
});
Or parse it directly if the response is a string.
$.ajax({
url: url,
type: 'post',
data: 'id='+id,
success: function(msg) {
// parse the string
var data = JSON.parse(msg);
// get properties
var regular = data.regular_price;
var discount = data.discount_price;
}
});
Thanx to all..Here are the solution :
<script>
function changePrice(id)
{
var url = '<?php echo $base_url ?>home/getprice/';
$.ajax({
url:url,
type:'post',
data:'id='+id,
dataType:'json',
success:function(msg)
{
var regular = msg.regular_price;
var discount = msg.discount_price;
}
});
}
</script>
My Function :
$new = array (
"regular_price" => $result->price,
"discount_price" => $price
);
$newarray = json_encode($new, JSON_PRETTY_PRINT);
print_r($newarray);
Try this:
In server side ajax call:
$respose['regular_price'] = 120;
$respose['discount_price'] = 100;
echo json_encode($response);
In JS: Considering msg is an json object
var data = JSON.parse(msg);
var regular = data.regular_price;
var discount = data.discount_price;

Unable to change image onChange function on ajax success return

Hi I'm new to this javavascript/ajax. I am trying to create a dropdown that dynamically changes the images by the different options as shown in this Fiddle here but the change function does not seem to be working.
I made sure that I am able to get the data from pictureList but the image source did not change successfully as the fiddle.
$('#selectVariant').change(function () {
var sku = $('#selectVariant :selected').val();
var sessionId="<?php echo $sessionId; ?>";
var dataString='sku='+ sku +'&sessionId='+sessionId;
$.ajax({
type:"post",
url: "<?php echo $base_url; ?>ajax-helper/search_variant.php",
data:dataString,
cache:false,
dataType: "JSON",
success: function(data){
var pictureList = {};
//example of my data list
//var pictureList = {'Apple SKU2': "http://tos-staging-web-server-s3.s3.amazonaws.com/9/catalogue/apples_in_season.png",
//'Pear1': "http://tos-staging-web-server-s3.s3.amazonaws.com/9/catalogue/pears.png"
//};
$.each(data.productVariantImages,function(i, productVariantImages){
pictureList[data.sku] = this.imagePath;
});
console.log(pictureList);
$('.content img').attr({"src":[pictureList[this.value]]});
}
});
return false;
});
However, when I do test it outside the ajax post, it is able to run.
Instance of this is change in ajax success function scope.
In this line $('.content img').attr({"src":[pictureList[this.value]]}); this
is not the instance of selectVariant element.
The usual practice for this is declare a variable that and use that variable in other scope. try the below code.
$('#selectVariant').change(function () {
var sku = $('#selectVariant :selected').val();
var sessionId="<?php echo $sessionId; ?>";
var dataString='sku='+ sku +'&sessionId='+sessionId;
var that = this;
$.ajax({
type:"post",
url: "<?php echo $base_url; ?>ajax-helper/search_variant.php",
data:dataString,
cache:false,
dataType: "JSON",
success: function(data){
var pictureList = {};
//example of my data list
//var pictureList = {'Apple SKU2': "http://tos-staging-web-server-s3.s3.amazonaws.com/9/catalogue/apples_in_season.png",
//'Pear1': "http://tos-staging-web-server-s3.s3.amazonaws.com/9/catalogue/pears.png"
//};
$.each(data.productVariantImages,function(i, productVariantImages){
pictureList[data.sku] = this.imagePath;
});
console.log(pictureList);
$('.content img').attr({"src":[pictureList[that.value]]});
}
});
return false;
});

Passing JavaScript variable to PHP using POST

I read somewhere that you don't need a form or hidden value to pass data when using ajax. If I could even get an example I would be grateful.
index.js:
function collectData(r) {
//gets the rows index
var i = r.parentNode.parentNode.rowIndex;
//selects the row picked
var sliceRow = document.getElementById('Sona').rows[i];
//have access to indiviual cells in the row
var sliceCell = sliceRow.cells;
var song = (sliceCell[0].innerHTML);
var artist = (sliceCell[1].innerHTML);
$.post("myLibrary.php", { postsong: song, postartist: artist });
}
PHP file :
if (isset($_POST)) {
$song = $_POST['postsong'];
echo $song;
}
$.ajax({
url : example.com',
contentType : 'application/x-www-form-urlencoded',
type : 'post',
data : {
i : r.parentNode.parentNode.rowIndex,
sliceRow : document.getElementById('Sona').rows[i],
sliceCell : sliceRow.cells,
song : (sliceCell[0].innerHTML),
artist : (sliceCell[1].innerHTML),
},
success:function(data) {
// blabla
},
});
<?php
if(isset($_POST)) {
print_r($_POST);
}

Passing array and other data through ajax

This is my javascript function....
But my php controller getting all values but not the array not sure why ? Need help.....
Thanks in advance :)
function submitForm(){
var id = $('#id').val();
var supplier_id = $('#supplier_id').val();
var description = $('#description').val();
var numofpro = $('#numofpro').val();
var product_id = new Array();
for(var i=0;i<numofpro;i++){
product_id[i] = $('#product_id'+(i+1)).val();
}
var payment_mode = $('#payment_mode').val();
//description = description.replace(new RegExp('\r?\n','g'), '<br />');
$.ajax({
url: "<?= base_url(); ?>edit/save_purchase", //The url where the server req would we made.
data: "id="+id+"&supplier_id="+supplier_id+"&description="+description+"&product_id="+product_id+"&payment_mode="+payment_mode,
dataType: "html", //Return data type (what we expect).
beforeSend:function(){
//alert("asds");
},
success: function(data) {
//alert("Edited");
alert(data);
}
});
}
because you pass a string, not an array :) try it:
$.ajax({
url: "<?= base_url(); ?>edit/save_purchase",
type: "POST"
data: {id : id, supplier_id : supplier_id, description : description, product_id : product_id, payment_mode : payment_mode},
dataType: "json",
beforeSend:function(){
//alert("asds");
},
success: function(data) {
//alert("Edited");
alert(data);
}
});
in you php use :
$arr = json_decode($_POST);
//some php code
//some $result;
// if $result arr then
echo json_encode($result);
// else
echo json_encode(array('data' => $result))
hope this helps...
You need to first turn that array into a string in JS. Before sending it, do this:
JSON.stringify(product_id);
Once you receive it in PHP, you need to decode it.
$decoded = json_decode($_POST['product_id'])

Categories

Resources