Tell me how in opencart2 to accept the data in the model, I have a form

<form action="" method="post"> <table class="table table-bordered table-striped"> <thead class="thin-border-bottom"> <tr> <th style="width: 10%;"> <i class="ace-icon fa fa-caret-right blue"></i><?php echo $text_id ;?> </th> <th> <i class="ace-icon fa fa-caret-right blue"></i><?php echo $text_name_category ;?> </th> <th class="hidden-480"> <i class="ace-icon fa fa-caret-right blue"></i><?php echo $text_status_category ;?> </th> <th style="width: 8%"> <i class="ace-icon fa fa-caret-right blue"></i><?php echo "Изменить" ;?> </th> </tr> </thead> <?php foreach ($category as $category){ ?> <tbody> <tr> <td><?php echo $category['category_id']?></td> <input type="hidden" name="category_id" id="category_id" value="<?php echo $category['category_id'];?>"> <td><?php echo $category['name'];?></td> <?php if($category['status'] == 1){ ?> <td class="hidden-480"> <span class="label label-info arrowed-right arrowed-in"><?php echo $text_on ;?></span> </td> <?php } else { ?> <td class="hidden-480"> <span class="label label-info arrowed-right arrowed-in"><?php echo $text_off ;?></span> </td> <?php } ?> <td> <center><button class="btn btn-xs btn-danger" id="<?php echo $category['category_id'];?>" type="button" data-toggle="modal" data-target="#myEdit" onclick="give(this.id)" > <i class="ace-icon fa fa-edit bigger-120"></i> <span class="bigger-110"></span> </button></center> </td> </tr> </tbody> <?php } ?> </table> </form> 

so i get the category id

 <script> function give(val) { var obj = document.getElementById('block'); $.ajax({ type: 'POST', url: '/index.php?route=product/category', data: val, success: function (data) { console.log(val); } }); } 

in the console, when I click a button, they receive id 85, 82, etc. I cannot understand how to write the received data; when I did print_r ($ _ POST) in the controller, nothing happens, and I do not know what comes in the post-e, tell me how it can be done.

  • Try data: 'id='+val - vp_arth
  • Then $ _POST ['id'] should come, and how can I see this? - Sender1050
  • Can you explain in more detail - Sender1050
  • Do you work with opencart? - Kirill Korushkin
  • yes with opencart? ......... - Sender1050

1 answer 1

try either in php to add the result to a file, or when you make an ajax request in chrome (for example) there is an inspector - in the networking tab you can view all requests and answers including XHR.

  • Yes, I get the Form data id: 82 when I click on the button, in the model I do $id = isset($this->request->post['id']); $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category WHERE category_id = '" .$id. "'"); return $query->row; $id = isset($this->request->post['id']); $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category WHERE category_id = '" .$id. "'"); return $query->row; and the check fails i can't understand why? - Sender1050
  • $ id = isset ($ this-> request-> post ['id']) <- here $ id will be either true or false. The SQL query will look something like this: SELECT * FROM prefix_category WHERE category_id = 'true' - Stalker
  • But how to make the value come, the output will be displayed only if the necessary id comes there? - Sender1050
  • @ Roma Kapioshka you Ajax pull the controller, not the model. - Kirill Korushkin
  • Something does not come out in the $this->model_product_category->getCategoryId(isset($this->request->post['id'])); and in the model `$ id = isset ($ this-> request-> post ['id'])? $ this-> request-> post ['id']: null; $ sql = "SELECT * FROM". DB_PREFIX. "category"; if ($ id) {$ sql. = "WHERE region_id = '". $ id. "'"; } $ query = $ this-> db-> query ($ sql); return $ query-> rows; `and error - Sender1050