It is necessary to substitute the value of this.id for the id key.
I feel that I was naughty, but I do not know how to do it right.

 <script type="text/javascript"> ... if (confirm("Вы уверены?")) window.location = '<?=$this->createUrl('controller/action', ['id' => '<script type="text/javascript">this.id</script>']); ?>'; ... </script> 
  • this.id a server side value? - Grundy
  • on the client. This is the id of the item I clicked on - Jeque
  • one
    then - in any way, since php is executed on the server, and javacript is performed on the client. At most, you can take advice from the answer and collect some template, save it to a string, and insert the desired value into the client - Grundy

2 answers 2

If you need to do an action for example for a few buttons, for example deletion, you get the following code

 <div class="delete" data-url-to-delete="<?=$this->createUrl('controller/action', array($id)); ?>">Удалить</div> <script type="text/javascript"> document.querySelectorAll(".delete").forEach(function(el){ el.addEventListener("click", function(e){ var urlToDelete = el.getAttribute("data-url-to-delete"); if (confirm("Вы уверены?")) window.location = urlToDelete; }); }); </script> 

Or, when you click on the button, pass the ID, in the handler, generate a URI and redirect to the URI

  • And where is the ID given by the author to form the link here? - VenZell
  • @VenZell corrected - added ID - Mihanik71
  • This is the Yii method of forming the url, in which you need to transfer an array with parameters. Therefore, imposing them on the method will not work - Jeque
  • @ Mihanik71 why do you answer questions in which you don’t understand what a person needs? We should also ask to specify in the commentary to the question - Alexey Shimansky
  • I need to pass this.id to createUrl like this: <?=$this->createUrl('controller/action', ['id' => this.id]); ?> <?=$this->createUrl('controller/action', ['id' => this.id]); ?> . But I can’t write this because this.id is a js code, and everything else is a php code. - Jeque

I realized that js in php not substituted, so I made ajax request to the controller/action , and passed id to it