PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 27, 2022

[FIXED] How to ajax request to custom page in prestashop 1.7

 October 27, 2022     ajax, php, prestashop, prestashop-1.7     No comments   

Issue

Am apology for my bad english. Am new to prestashop. Please anybody help. How to send AJAX request to custom php file in prestashop

//My js file
     $.ajax({
     url : baseUrl + "modules/<myModule>/ajaxfunc.php",
     type: "POST",
     cache: false,
     data : {form_data: 1 , action:'imageuploadAction'},

     beforeSend: function() {
     $('body').append('<div class="loading_popup">Loading...</div>');},        

    success: function(data){
    console.log(data);
       }
      });

// php file
// modules/<myModule>/ajaxfanc.php

   <?php
   include_once('../../config/config.inc.php');
   include_once('../../init.php');
   class ajaxfuncAjaxModuleFrontController extends ModuleFrontController
    {
    public function  imageuploadAction() {
     die('here');
    }
   }
   ?>

I didn't know its be correct or not. please guide me.


Solution

I have found the solution to get the proper Ajax request in prestashop 1.7

//In tpl file

<script>
  var url= {url entity='module' name='<myModuleName>' controller='<MyControllerName>' params = ['var1' => 1,'var2' => 2,action => 'MyControllerAction']}
</script>

//In Js file

$.ajax({
url : url,
type: "POST",
data : 'var3='3,
success : function(response){
  console.log(response);

} });

//In Controller Php file

<?php
  require_once(dirname(__FILE__).'../../../../config/config.inc.php');
  require_once(dirname(__FILE__).'../../../../init.php');
  class <MyModule><MyController>ModuleFrontController extends ModuleFrontController
    {
      public function initContent()
      {
       $this->ajax = true;
        parent::initContent();
      }
      // displayAjax for FrontEnd Invoke the ajax action
      // ajaxProcess for BackEnd Invoke the ajax action

       public function displayAjaxMyControllerAction()
        {
         $var1 = Tools::getValue('var1');
         $var2 = Tools::getValue('var2');
         $var3 = Tools::getValue('var3');

         header('Content-Type: application/json');
         die(Tools::jsonEncode(['var1'=> $var3]);
        }
      }


Answered By - Prabhakaran A
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing