Passing jquery variable

I am having problems with the following jquery code

$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']) .'&name="+id) }); });' ); 

I want the ajax link to be like this http://192.168.1.4/~user/church/backend/web/death/stl_set_relation?id=20&name=1

but with my code, I cannot pass the id value correctly. that my code creates the following url

 http://192.168.1.4/~user/church/backend/web/death/stl_set_relation?id=%2Bid&name=20 

I tried this too

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

but it did not give me the desired result

How can I pass the id value correctly?

+6
source share
4 answers

I solved the problem using the following code

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

try how it can be, it will work.

 $.post("'.\Yii::$app->getUrlManager()->createUrl(['death/stl_set_relation','id'=>'"+id+"']) .'&name="+n); 
+2
source

You can do it differently using the yii\helpers\Url class.

For instance:

 $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::toRoute('death/stl_set_relation').'", { id: id, name: id }); }); });' ); 
+1
source

Try this line

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

Everything that you write in single quotes here will be printed, as in JS. You type the + sign as it is here. You are also using id inside a PHP scope, which should not be.

0
source

All Articles