Jump to content

Edit History

andtrx

andtrx

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!





 

andtrx

andtrx

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 so after I select the City, I populate the select 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,		
			));


            /*  THE DUDE ! how pass TPL to AJAX request ???*/
           return json_encode($this->context->smarty->assign(array(
				'city_list' => $cities,		
			)));
        
        }

	}

I am out of ideas.. :)

Thanks!





 

×
×
  • Create New...