Yii2 reload a div after call action in controller


i would like change only a div-content after i called a action-function in my controller.

in my view:

<div id="test">hello world!</div>
<a class="btn" href="/orders/sendverify?id=m27MMnrn7XgFMLwWxdMnY8Ai6sEsnpACjaC3HQdyy-ThMFBsruCX3JGP8SLBtnFa"><i class="fa-solid fa-envelope"></i> send mail</a>

in my controller:

public function actionSendverify($id) {

   // do something like load a model and set verified to true

   // after set verified true returns new status
   return 'mail sended...';

Is there a solution how i can show the return status in the div with id=test? Or must i do that with an javascript and ajax?

Thank you for help.

If you only want to change the content of one div without reloading the page you need an Ajax request and Javascript.
Otherwise you’ll have to reload the entire page but I would rather suggest the js way

Thank you for you answer. I thought so. Does Yii have a way to do this without an external JS file?

So far I’ve been writing Javascript functions which call a controller function via Ajax. But I don’t think it’s so good if you can later view the javascript functions by downloading the files.

The question is: what is the difference for you if Yii2 does this request or if you write it.
In the end people can always download both script files and see what happens.

As soon as your request is done everyone can see it in their Console - no matter what.
There is no need to try to cover your JS. You can minify + uglyfy it tho but there is no reliable way to cover it from people who really want to see it. Furthermore as said, the Browser Console can still provide all the information…

For example just in case you never worked with it. When you submit a Post here:
You can see all the form-submit data

As well as the target
" Request URL: Yii Framework Forum - Latest posts"
As well as the entire response

With the request call stack

So it’s easy to find out what happened and how to use it. Even without your JS.
You can even copy your Ajax call, paste it in software like postman and re-send it as often as you wish with manipulated POST data

Thank you for your comprehensive answer.

I was hoping Yii might have a different approach.

You may be able to accomplish this using Pjax, yii\widgets\Pjax | API Documentation for Yii 2.0 | Yii PHP Framework.

Thank you very much @snathan. This is exactly what i needed :grinning: