Why is the button in yii2 works only at second click?

In the View there is a button:
<?php $newForm = ActiveForm::begin(); ?>
 <div class="form-group">
 <?= Html::submitButton('Clear',['class'=-->'btn btn-danger', 'name' => 'submit', 'value' => 'clear_unique_users_table']) ?>
</div>
 <?php ActiveForm::end(); ?>


The handler in the controller:
if(Yii::$app->request->post('submit')==='clear_unique_users_table'){
if($uniqueUsersModel->clearTable()){
 return $this->refresh();
}
 }

The action takes place only after the second click on the button.
What am I doing wrong?
Thank you.
June 14th 19 at 20:27
2 answers
June 14th 19 at 20:29
Solution
Need to look for js that intercepts the click on the button. Apparently You wrote somewhere in the handler a clique with very General selector, like:
$('button').on('click',finction(e){
e.preventDefault();
//some code
});

Or intercept a form submission or anything like that. In any case, if the query is not the case in js, not in php and certainly not in the controller.
in this project net basic, there js never wrote
turns out for every button and need to write a js handler? - kimberly.Predovic commented on June 14th 19 at 20:32
in this project net basic, there js never wrote

but as a type template is associated with the presence of js?

turns out for every button and need to write a js handler?

no, you have to find js that interferes with the normal operation - Henderson_Beat commented on June 14th 19 at 20:35
I mean that the design is clean, basic, launched, created a model of the table and wrote a method for its purification, no extra js wrote
thank you for your reply, I will look for in js - kimberly.Predovic commented on June 14th 19 at 20:38
if nothing works, then you may want to look for problems in the browser (settings, extensions, viruses) - Henderson_Beat commented on June 14th 19 at 20:41
Yes, the point is that checked in a few browsers
if the buttons are handled via the form associated with the model - everything is OK, but if the view to write only the button, without a model, and in the handler check it through the name - this garbage happens - kimberly.Predovic commented on June 14th 19 at 20:44
You're saying that js is not added, now that there is a handler. You decide - Henderson_Beat commented on June 14th 19 at 20:47
I probably made a mistake in terminology, for me the handler is the logic that is executed after clicking the button, in this case, it clears the table. - kimberly.Predovic commented on June 14th 19 at 20:50
June 14th 19 at 20:31
Hello.
--
To mine the problem in dual terms, two if doing. I'm in a similar situation is encountered. You have to write just:
public function actionCleartable() 
{
 $ok = Yii::$app->request->post('submit');
 if(isset($ok)) {
$uniqueUsersModel->clearTable();
 return $this->refresh();
 } else {
 return $this->render('cleartable');
}
}

That is, in the good old nineties, when there was php 4 ))) We wrote, if you enter the name click submit, then is this or that, if not pressed, then nothing happens.
The same approach will work, if you don't want to use a ActiveForm

<?= Html::beginForm(['order/update', 'id' =--> $id], 'post', ['enctype' => 'multipart/form-data']) ?>
<?= Html::submitButton('Send', ['class' =--> 'submit', 'name' => 'submit']) ?>
<?= Html::endForm() ?>


You can make these forms in one page at least a dozen, and the work, which name conforms to the rules prescribed in the controller, I search form shoved in the top menu without linking each page to a specific model, and there are no errors. Most importantly, if you push input, see example here

Find more questions by tags Yii