Page 1 of 1
Problem with reading JSON
Posted: Sat Feb 24, 2018 1:41 am
by NirvanaNirvan
I'm trying to build a form page that for example by choosing a product from a drop-down menu, the product abbreviation is read automatically from database and is filled in a menu underneath of drop-down menu (this is something similar to what is done in registration form when by choosing a country, the zones are updated in the zone menu). It seems that the code is working but it doesn't show anything in menu bar!!. The ajax part in .tpl file is as following. Any help?
Code: Select all
<script type="text/javascript"><!--
$('select[name=\'product_id\']').on('change', function() {
$.ajax({
url: 'index.php?route=information/product/profunc&product_id=' + this.value,
dataType: 'json',
beforeSend: function() {
$('select[name=\'product_id\']').after(' <i class="fa fa-circle-o-notch fa-spin"></i>');
},
complete: function() {
$('.fa-spin').remove();
},
success: function(json) {
html = json['code'];
$('input [name=\'productcode\']').html(html);
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
});
$('select[name=\'product_id\']').trigger('change');
//--></script>
And the "profunc" function is :
Code: Select all
public function profunc() {
$json = array();
$this->load->model('localisation/product');
$product_info = $this->model_localisation_product->getPro($this->request->get['product_id']);
if ($product_info) {
$json = array(
'product_id' => $product_info ['product_info'],
'reserved1_id' => $product_info ['reserved1_id'],
'name' => $product_info ['name'],
'code' => $product_info ['code'],
'status' => $product_info ['status']
);
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 1:52 am
by straightlight
No OC version posted, partial solution has been posted. Model file for getPro is missing. Also check your mySQL fields noticing if they do match with the arrays before encoding the JSON array.
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 3:22 am
by NirvanaNirvan
Thanks for your prompt answer. I checked the mySQL field and it seems OK! Personally I think the problem is with this part of the code but I can't understand where:
Code: Select all
success: function(json) {
html = json['code'];
$('input [name=\'productcode\']').html(html);
},
and here is the model file for the getPro function:
Code: Select all
<?php
class ModelLocalisationProduct extends Model {
public function getPro($product_id) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product WHERE product_id = '" . (int)$product_id . "' AND status = '1'");
return $query->row;
}
}
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 3:31 am
by thekrotek
Try to debug it using alert() and JSON.stringify functions, see, what is returned on your AJAX request.
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 3:39 am
by NirvanaNirvan
thekrotek wrote: ↑Sat Feb 24, 2018 3:31 am
Try to debug it using alert() and JSON.stringify functions, see, what is returned on your AJAX request.
Thanks a lot thekrotek! I'm not really good in using JSON and ajax. May you please let me know how and where to use alert() and JSON.stringify functions?
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 3:51 am
by thekrotek
Put the string below before html = json['code'] (BTW, you don't need this string at all) in "success" part of your AJAX function:
alert(JSON.stringify(json));
Remember this debugging technique, it lets you to print a nice looking array in JS alert popup.
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 4:09 am
by NirvanaNirvan
thekrotek wrote: ↑Sat Feb 24, 2018 3:51 am
Put the string below before html = json['code'] (BTW, you don't need this string at all) in "success" part of your AJAX function:
alert(JSON.stringify(json));
Remember this debugging technique, it lets you to print a nice looking array in JS alert popup.
Thanks again, I think I'm getting something! I put it there and now, for example for a product, I'm getting this: {"product_id":"2","reserved1_id":"101","name":"mouse1","code":"MSE","status":"1"}
But I still don't understand why it doesn't show MSE in the productcode filed!!
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 4:19 am
by thekrotek
$('input [name=\'productcode\']').val(json['code']);
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 4:25 am
by straightlight
In your controller,
replace:
Code: Select all
public function profunc() {
$json = array();
$this->load->model('localisation/product');
$product_info = $this->model_localisation_product->getPro($this->request->get['product_id']);
if ($product_info) {
$json = array(
'product_id' => $product_info ['product_info'],
'reserved1_id' => $product_info ['reserved1_id'],
'name' => $product_info ['name'],
'code' => $product_info ['code'],
'status' => $product_info ['status']
);
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
with:
Code: Select all
public function profunc() {
$json = array();
if (!empty($this->request->get['product_id'])) {
$this->load->model('catalog/product');
$product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);
if ($product_info) {
$json = array(
'product_id' => (int)$product_info['product_id'],
'reserved1_id' => (int)$product_info['reserved1_id'],
'name' => strip_tags(html_entity_decode($product_info['name'], ENT_QUOTES, 'UTF-8')),
'code' => strip_tags(html_entity_decode($product_info['code'], ENT_QUOTES, 'UTF-8')),
'status' => (int)$product_info['status']
);
}
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
Then, from your jQuery code, replace:
Code: Select all
success: function(json) {
html = json['code'];
$('input [name=\'productcode\']').html(html);
},
with:
Code: Select all
success: function(json) {
$('input [name=\'productcode\']').val(json['code']);
},
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 4:50 am
by NirvanaNirvan
Thanks straightlight.I really appreciate you guys help. I used your code (I changed some parts because I've created the model in localisation folder) but still no answer. With an "alert(JSON.stringify(json)); " I get the same answer either with my code or yours.
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 5:00 am
by straightlight
Is your product code returning to a select object or to an input object? Please post your HTML portion as well regarding productcode. Right now, we only have partial solutions.
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 5:02 am
by straightlight
Also, ensure to include the token key and value in your jQuery URL if this solution is being used from the admin.
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 5:38 am
by NirvanaNirvan
straightlight wrote: ↑Sat Feb 24, 2018 5:02 am
Also, ensure to include the token key and value in your jQuery URL if this solution is being used from the admin.
no, I'm not working on the admin part. It seems that everything is working except that the java code is not returning the value to the php code

Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 5:40 am
by straightlight
Then, what is your HTML source code containing the productcode?
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 5:56 am
by NirvanaNirvan
straightlight wrote: ↑Sat Feb 24, 2018 5:40 am
Then, what is your HTML source code containing the productcode?
here it is:
Code: Select all
<div class="form-group">
<label class="col-sm-2 control-label" for="input-productcode"><?php echo $entry_productcode; ?></label>
<div class="col-sm-5">
<input type="text" name="productcode" value="<?php echo $productcode; ?>" id="input-productcode" class="form-control" readonly/>
<?php if ($error_productcode) { ?>
<div class="text-danger"><?php echo $error_productcode; ?></div>
<?php } ?>
</div>
</div>
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 6:00 am
by straightlight
<input type="text" name="productcode" value="<?php echo $productcode; ?>" id="input-productcode" class="form-control" readonly/>
Where does this ID come from?
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 6:16 am
by NirvanaNirvan
straightlight wrote: ↑Sat Feb 24, 2018 6:00 am
<input type="text" name="productcode" value="<?php echo $productcode; ?>" id="input-productcode" class="form-control" readonly/>
Where does this ID come from?
It comes from a php file in controller:
Code: Select all
if (isset($this->request->get['productcode'])) {
$data['productcode'] = $this->request->get['productcode'];
} else {
$data['productcode'] = '';
}
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 6:17 am
by straightlight
What I am asking is the input-productcode ID specifically, where does that come from?
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 6:24 am
by NirvanaNirvan
It just appears here. Actually I'm just mimicking what opencart has done to create a form!
Re: Problem with reading JSON
Posted: Sat Feb 24, 2018 6:54 am
by straightlight
A good start. Although, it would be great to provide the beginning through the end of the process you did in order to replicate that content.