Jump to content

module baseDir is not working


Recommended Posts

Hi,

I am doing a small module in Prestashop 1.6 . For that I want to load some custom js with ajax in the backend form.

So in my module code I just made the code for form 

 

 


public function getContent() {
    $this->_html .= $this->renderForm();
    $this->_html .= $this->__displayForm();
    return $this->_html;
 }

 

 in renderForm() function I have shown the form in the backend for my module.

 and in __displayForm() function I have taken the custom js codes and ajax part which will be loaded in the prestashop backend side.

 so the code goes like this

   


public function __displayForm()
  {
    $this->_html .=  '
      <script src="../modules/mymodule/js/custom.js" type="text/javascript" charset="utf-8"></script>
  }

  

  in custom.js I have my custom js and ajax file goes like this

  


    jQuery('.button').on('click', function() {
    $.ajax({
      type: "POST",
      url: baseDir + "/modules/mymodule/assets/php/selectvalue.php",
      cache: false,
      data: { selectedVal : SelectedValue },
            success: function(html) {
        $(".test").html(html);
      }
    });
  });

  

  Now when I am doing click on button it should work with ajax. But it is showing error like 


  Uncaught ReferenceError: baseDir is not defined 

  

  But if I am giving absolute path for url then its working without any issue. So can someone kindly tell me how to use baseDir here. Any help and suggestions will be really appreciable. Thanks

 

Link to comment
Share on other sites

As far as I can recall, baseDir is not assigned in the back office.

Get it in the php file and assign it after your script to js, like

 

$protocol_content.Tools::getHttpHost().__PS_BASE_URI__.(!Configuration::get('PS_REWRITING_SETTINGS') ? 'index.php' : '')

 

and then right before the script, inside another <script>, use var baseDir =...

Link to comment
Share on other sites

As far as I can recall, baseDir is not assigned in the back office.

Get it in the php file and assign it after your script to js, like

 

$protocol_content.Tools::getHttpHost().__PS_BASE_URI__.(!Configuration::get('PS_REWRITING_SETTINGS') ? 'index.php' : '')

 

and then right before the script, inside another <script>, use var baseDir =...

 

 

 

@Nemo1 thanks for the reply but I am confused here. For now I have made my code like this

 

 
public function __displayForm()
  {
    $protocol_content =  Tools::getHttpHost().__PS_BASE_URI__.(!Configuration::get('PS_REWRITING_SETTINGS') ? 'index.php' : '');
  
    $this->_html .=  '
      <script type="text/javascript">
        var baseDir = "<?php echo json_encode($protocol_content) ?>";
      </script>
      <script src="../modules/mymodule/js/custom.js" type="text/javascript" charset="utf-8"></script>
      <div class="test"></div>';
  }
  
  
  and inside custom.js when I am doing console.log(baseDir). It is showing <?php echo json_encode($protocol_content) ?> in console tab. Am I doing any wrong here?
Link to comment
Share on other sites

  • 11 months later...
×
×
  • Create New...