send ajax variable to yii2

Hi guys!

I need your help, please.

I have this code js, where there are a ajax variable “idpadre” that i need to pass to variable php “variablePHP”, but i can’t:





$(document).ready(function() {

    

		$("#idElement").change(function(event)

		{

			var idpadre = "value";

			$("#pidhijo").load('<?= Url::to(['combobox', 'variablePHP' => "idpadre"]) ?>');


		});      

	});


    

My code php is:




<?php 


$variablePHP= $_GET['variablePHP'];

echo $variablePHP;

?> 



An important point to consider, is that this code work!, if i send in the previous url one constant, for example “1” ($("#pidhijo").load(’<?= Url::to([‘combobox’, ‘idpadre’ => 1]) ?>’);), but i need send a variable.

In other forum, i find this for yii 1:




$this->registerJs(

    'jQuery(document).ready(function($){

        $(".member").on("change",function(){

            var id = $(this).attr("id");

            // alert(id);

            var n = $(this).val();

            // alert(n);

$.post("'.\Yii::$app->getUrlManager()->createUrl(['death/stl_set_relation']).'?id="+id+"&relation="+n)


        });

     });'

);



But i don’t know how suit to my need, in yii2 by method get

appreciate any help

Regards!




$url = \yii\helpers\Url::to(['death/stl_set_relation']);

$this->registerJs(

    'jQuery(document).ready(function($){

        $(".member").on("change",function(){

            var id = $(this).attr("id");

            // alert(id);

            var n = $(this).val();

            // alert(n);

            $.post(

                 "'.$url.'",

                 { id : id, relation: n },

                 function(data) {

                       console.log("post finished!");

                 }

            );

        });

     });'

);



Thanks friend!

I will review and tell you how I was.

Regards!

Hi Fabrizio,

Not work :(.

could I replace this code?




$("#idpadre").change(function(event)

		{

var idpadre = $(this).find(':selected').val();

$("#pidhijo").load('<?= Url::to(['combobox', 'buscar' => "hijos",'idpadre' => 1]) ?>');


}



for something like this?




$("#idpadre").change(function(event)

		{

var idpadre = $(this).find(':selected').val();

$("#pidhijo").load("'.<?= Url::to(['combobox']) ?>.'",{buscar: hijos, idpadre: idpadre });

}



I need change the constant "1" for the variable "idpadre" .

sorry for the disturbances.

Thanks for your help!