Page 1 of 1

UNDEFINED INDEX: PRODUCT_CATEGORY - ERROR BY TRYING TO $ _REQUEST IN CONTROLLER ADDPRODUCT.PHP

Posted: Sat Jun 22, 2019 10:33 pm
by joaodias117
Good morning!

I'm new at programming and opencart, so I'm not very familiar with the site.

I'm creating a combobox of categories and subcategories on the addproduct page, where when selecting a category, another combo is generated with its subcategories.

For this I created a script by pulling the selected value and I will use it in the Controller to parameterize the array of the second combo (subcategories).

However, when I try to perform a $ _REQUEST ['product_category'] on the Controller, it says that this index is undefined.

I know there are some functions like "$ this-> load-> view -> ..." but I do not know how to use them in this case.

Could someone help me with the communication between view and controller and to make this request?

The codes are as follows:

VIEW

HTML:

Code: Select all

  <form method="POST" action="addproduct.php">
		<select name="product-category" id="product-category" size="10" class="form-control" onclick="myFunction()">
			
					<option value="">Escolha a Categoria</option>
			
			<?php foreach($product_categories as $product_category){ ?>
		
					<option value="<?php echo $product_category['category_id'] ?>" ><?php echo $product_category['name'] ?></option>
		
			<?php } ?>

		</select>


		<select name="product-subcategory1" id="product-subcategory1" size="10" class="form-control" onclick="myFunction1()">
		
					<option value="">Escolha a Subcategoria</option>
		
		<?php foreach($product_subcategories1 as $product_subcategory1){ ?>

					<option value="<?php echo $product_subcategory1['category_id'] ?>"><?php echo $product_subcategory1['name'] ?></option>
					
					
				<?php } ?>

		</select>
		</form>
SCRIPT:

Code: Select all

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
		<script type="text/javascript">
		  google.load("jquery", "1.4.2");
		</script>
		
		<script type="text/javascript">
		$(function(){
			$('#product-category').change(function(){
				if( $(this).val() ) {
					$('#product-subcategory1').hide();
					$.getJSON('addproduct.php?search=',{category_id: $(this).val(), ajax: 'true'}, function(j){
						var options = '<option value="">Escolha Subcategoria</option>';	
						for (var i = 58; i < j.length; i++) {
							options += '<option value="' + j[i].category_id + '">' + j[i].name + '</option>';
						}	
						$('#product-subcategory1').html(options).show();
					});
				} else {
					$('#product-subcategory1').html('<option value="">– Escolha Subcategoria –</option>');
				}
			});
		});
		</script>

CONTROLLER

PHP:

Code: Select all

@include_once('C:/xampp2/htdocs/alloeetestes/catalog/view/theme/journal2/template/account/customerpartner/addproduct.tpl');
		
		$id_categoria = $_REQUEST['product_category'];

Thank you in advance for your help!

Re: UNDEFINED INDEX: PRODUCT_CATEGORY - ERROR BY TRYING TO $ _REQUEST IN CONTROLLER ADDPRODUCT.PHP

Posted: Sat Jun 22, 2019 11:45 pm
by straightlight
No OC version posted. Forum rules. However, simply replicate the country and zone feature with your idea since you're looking to load categories and sub-categories. Since you claim on being a beginner to the task, either create a service request in the Commercial Support section of the forum, or drop me a PM, to complete this task as a custom job.

In addition, you can also look at this free marketplace extension to have a better idea: https://www.opencart.com/index.php?rout ... n_id=36704