0

I have a js file and a index file which are located in the same folder. This is my form. I have already register the script already too.

$this->registerJs($this->render('script.js'), \yii\web\VIEW::POS_READY);

<?php $form = ActiveForm::begin(); ?>
<div class="booth">
    <video id="video" width="400" height="300"></video>
    <a href="#" id="capture" class="button">Take Photo</a>
    <canvas id="canvas" width="400" height="300"></canvas>
</div>

<div class="form-group">
       <?= Html::button('Save',['class'=>'btn btn-primary','onclick'=>'saveimage();']) ?>
</div>

 <?php ActiveForm::end(); ?>

This is the function inside the js file.

function saveimage()
{
    var csrfToken = $('meta[name="csrf-token"]').attr("content");

    console.log(123);

    })*/
}

When I clicked my button is show error Uncaught Reference Error: saveimage is not defined? Its there anyway to call the function?

I tried 'onclick'=> 'js:saveimage()' too, but still not working.

2
  • Does the solution propose worked for you? If yes consider upvoting the answer, and if no tell us what other issues you are facing. Commented Jul 6, 2017 at 11:00
  • Also I would suggest you to go through the following link: stackoverflow.com/help/someone-answers Commented Jul 6, 2017 at 11:01

1 Answer 1

1

Try:

$this->registerJs(
    "function saveimage()
     {
        var csrfToken = $('meta[name="csrf-token"]').attr("content");

        console.log(123);

     }",
    \yii\web\VIEW::POS_READY,
    'my-button-handler'
);

Or alternatively, If you want to use a separate js file you can use below syntax:

$this->registerJsFile(
    '@web/js/script.js',
    ['depends' => [\yii\web\JqueryAsset::className()]]
);

When working with the yii\web\View object you can dynamically register frontend scripts. There are two dedicated methods for this:

1) registerJs() for inline scripts

2) registerJsFile() for external scripts

For more details refer to the link.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.