Jump to content

Ajax Request in AddressController.php


Recommended Posts

Hello, 

In the address.tpl I have a select input that has a dropdown with a list of Cities. When I select on change the City, I need to populate the select under it with smaller cities. 
I Created in the database my list of smaller cities.

So what I am trying to do is send an ajax request to the AddressController.php.
After I select the City, I populate the select under it with the smaller cities.


I dont know what exactly how to create the ajax request and how to get it in my AdminController.php
This is what I tried:


In my address.tpl:

<script>
    {literal}

        var uriAddress = 'index.php?controller=AddressController';

        $('#id_state').change( function() {
           $(this).find(":selected").each(function () {                              
                city = $(this).text();                   
            });

            $.ajax({
                url : uriAddress,
                type : 'POST',
                async: true,
                cache: false,
                dataType : "json",
                data: {
                    city: city,
                    action: "TestMyAjax",
                },
                success : function (result) {
                    console.log(result);
                }
            });
        });

    {/literal}
</script>



In my AddressController.php

	public function initContent()
	{
		parent::initContent();

        $this->ajax = true; // if I enable this it throws me a blank page

		$this->setTemplate(_PS_THEME_DIR_.'address.tpl');
	}

	public function displayAjax()
	{
		if (count($this->errors))
		{
			$return = array(
				'hasError' => !empty($this->errors),
				'errors' => $this->errors
			);
			die(Tools::jsonEncode($return));
		}

        if(Tools::getValue('method')=='myMethod')
        {
			$cities = array();
			$sql = 'SELECT judet, localitate FROM sdn_fancourier_cities WHERE judet = "alba"';
			if ($results = Db::getInstance()->ExecuteS($sql))
			foreach ($results as $resultCity) {
				array_push($cities, $resultCity);
			}
			
			$this->context->smarty->assign(array(
				'city_list' => $cities,		
			));

           return json_encode($this->context->smarty->assign(array(
				'city_list' => $cities,		
			)));
        
        }

	}

I am out of ideas.. :)

Thanks!





 

Edited by andtrx (see edit history)
Link to comment
Share on other sites

Hello:

All ajax request coulb be processed with his own method on AdminController:

public function ajaxProcessFilterClients()
{
  $result = array();

  die(Tools::jsonEncode($result));
}

You must specify which AdminController to call and action to execute:

$.ajax({
        url: 'index.php',
        type: 'POST',
        data: 'controller=AdminControllerYours&action=filterClients&ajax=1&token=' + token,
        dataType: 'json',
        cache: false,
        async: false,
        success: function (jsonData) {
            if (jsonData) {
                alert(jsonData);
            }
        }
    });

Regards

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...