Post by NirvanaNirvan » Sat Feb 24, 2018 1:41 am

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));
	}

Newbie

Posts

Joined
Fri Sep 01, 2017 7:04 am

Post by straightlight » Sat Feb 24, 2018 1:52 am

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.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by NirvanaNirvan » Sat Feb 24, 2018 3:22 am

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;
	}
}

Newbie

Posts

Joined
Fri Sep 01, 2017 7:04 am

Post by thekrotek » 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.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by NirvanaNirvan » Sat Feb 24, 2018 3:39 am

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?

Newbie

Posts

Joined
Fri Sep 01, 2017 7:04 am

Post by thekrotek » 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.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by NirvanaNirvan » Sat Feb 24, 2018 4:09 am

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!!

Newbie

Posts

Joined
Fri Sep 01, 2017 7:04 am

Post by thekrotek » Sat Feb 24, 2018 4:19 am

$('input [name=\'productcode\']').val(json['code']);

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by straightlight » Sat Feb 24, 2018 4:25 am

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']); 
		},


Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by NirvanaNirvan » Sat Feb 24, 2018 4:50 am

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.

Newbie

Posts

Joined
Fri Sep 01, 2017 7:04 am

Post by straightlight » Sat Feb 24, 2018 5:00 am

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.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by straightlight » 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.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by NirvanaNirvan » Sat Feb 24, 2018 5:38 am

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 ???

Newbie

Posts

Joined
Fri Sep 01, 2017 7:04 am

Post by straightlight » Sat Feb 24, 2018 5:40 am

Then, what is your HTML source code containing the productcode?

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by NirvanaNirvan » Sat Feb 24, 2018 5:56 am

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>

Newbie

Posts

Joined
Fri Sep 01, 2017 7:04 am

Post by straightlight » 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?

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by NirvanaNirvan » Sat Feb 24, 2018 6:16 am

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'] = '';
		}

Newbie

Posts

Joined
Fri Sep 01, 2017 7:04 am

Post by straightlight » Sat Feb 24, 2018 6:17 am

What I am asking is the input-productcode ID specifically, where does that come from?

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by NirvanaNirvan » Sat Feb 24, 2018 6:24 am

It just appears here. Actually I'm just mimicking what opencart has done to create a form!

Newbie

Posts

Joined
Fri Sep 01, 2017 7:04 am

Post by straightlight » Sat Feb 24, 2018 6:54 am

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.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 15 guests