0

I want to create two submit button in my form. The first button (I use Html::submitButton) link to actionCreate in Controller redirect to index. I want to redirect the second button to its form but it doesn't work. Here is my second button:

<?= Html::a('<span class="glyphicon glyphicon-floppy-disk"></span>  Save', ['simpan'], ['class' => 'btn btn-primary']) ?>

and this is my actionSimpan in Controller

public function actionSimpan()
{
    $model = new Armada();

    if ($model->load(Yii::$app->request->post())) {
        // get the instance of the uploaded file 
        $imageName = $model->NAMA_ARMADA; 
        $model->photo = UploadedFile::getInstance($model,'photo');
        $model->photo->saveAs('uploads/'.$imageName.'.'.$model->photo->extension);

        //save the path in the column 
        $model->IMG_ARMADA = 'uploads/'.$imageName.'.'.$model->photo->extension; 
        $model->save();

        return $this->redirect(['index']);
    }

    return $this->render('create', [
        'model' => $model,
    ]);
}

I tried the Html::submitInput but it doesn't work. What should I do to make it work? Thank you!

unicornous
  • 19
  • 1
  • 4

4 Answers4

0

Button:

<?= Html::a('<span class="glyphicon glyphicon-floppy-disk"></span>  Save', ['simpan'], ['class' => 'btn btn-primary', 'id' => 'simpan-submit-btn']) ?>

JS:

<script>
$('#simpan-submit-btn').on('click', function(e) {
  e.preventDefault();
  var submitUrl = $(this).attr('href');

  var form = $('form').eq(0); // change selector if you need
  form.attr('action', submitUrl);
  form.submit();
});
</script>
SiZE
  • 2,217
  • 1
  • 13
  • 24
0

Please, make sure to place your submit button inside the form tags (using Html::submitButton).

If this is not the problem, check if the model is saving using:

if (!$model->save()) {
    var_dump($model->getErrors());
    die;
}
ThiagoYou
  • 308
  • 3
  • 12
0

You just need to add the data-method="post" to your anchor and it will submit the form automatically, no need to add any javascript. Change your anchor definition to the one below and you will see it start asking for the empty inputs of the form means triggers validation too and after validating it submits.

<?php

echo Html::a('<span class="glyphicon glyphicon-floppy-disk"></span>  Save', ['simpan'], [
    'class' => 'btn btn-primary',
    'data' => [
        'method' => 'post'
    ]
]);
?>
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
  • I am not able to get the name can you please see my [question](https://stackoverflow.com/q/66963449/6854117) related to it? – Moeez Apr 06 '21 at 06:12
0

The easiest way is pass the params to the submit link:

<?= Html::a(Yii::t('common', 'Export to XLSX'), Url::toRoute(['xlsx-export']), [
            'class' => ['btn', 'btn-info', 'btn-sm'],
            'data' => [
                'method' => 'get',
                'params' => \yii\helpers\ArrayHelper::toArray($modelSearch),
            ]
    ]); ?>
Correcter
  • 3,466
  • 1
  • 16
  • 14