3

I am making changes in existing web page in yii2. I had this section of code:

Html::a('Confirm!',[
        'default/apply',
        'confirm' => 1,
        'id' => $data->id
    ],['class' => 'btn-primary','data-method' => 'post'])

I have moved this to a different container on the same page.

(I had to adjust slightly, changing $data->id into $projectInfo->id as earlier it was inside anonymous function within a widget and now inside a foreach loop. But this should not be relevant I suppose.)

Both before and after the change the same line is present in html (but in different part of the page):

<a class="btn-primary" href="/participant/default/apply/13/1" data-method="post">Confirm!</a>

But on execution http request is now sent as GET instead of POST.

BEFORE: "POST /participant/default/apply/13/1 HTTP/1.1"

NOW: "GET /participant/default/apply/13/1 HTTP/1.1"

I cannot figure out why this changed and how to get the code to work as POST in new location. This href execution must depend on some additional factor that I am not aware of.

Radek
  • 67
  • 1
  • 9

1 Answers1

4

You can send POST request using link thanks to JavaScript inside yii.js file that wraps it in form silently. If this JS is not loaded in assets link works in standard way which is sending GET requests.

Check if yii.js is loaded (usually through registering yii\web\YiiAsset directly or by dependency).

Bizley
  • 17,392
  • 5
  • 49
  • 59
  • Thanks! That gave me a hint although it did not solve my issue entirely. 1. I have a reference to yii.js just before the end of `body` in html: `` This reference is valid. Can I check anything else to make sure it is loaded? 2. There is more complexity in JS in my page that might be affecting how this `href` is handled. I am still unsure why `data-method` related code is not executed but I have got a valid answer for my question. – Radek Feb 07 '17 at 15:35