0

As seen in the title I am looking for a solution to above question. I have the following button:

<input onclick="setRefreshTimer();" type="button" value="10 Seconds">

And below the function. This one sets a certain time to the refresh timer (in the example 10 seconds) so that the default value of 30 seconds is overwritten.

function setRefreshTimer() {
  Yii::log("I am not certain if I was here!");
  CHtml::refresh(Yii::app()->config->set('DASHBOARD_RELOAD', 10));
  return CHtml::refresh(Yii::app()->config->get('DASHBOARD_RELOAD'), $url);
}

What I need now is a solution on how to call the function when the button is clicked. The code above is what I have tried so far but it does not seem to work correctly and I can't seem to find the reason for this.

Any help, advice and/or hint on how to solve/improve this issue is very welcomed.

niac
  • 33
  • 2
  • 17
Malgosh
  • 340
  • 1
  • 11
  • PHP is executed **server-side**, your browser (**client-side**) cannot execute PHP code (that's why you use a web server). Your input is actually trying to call a JavaScript function. You might be looking for AJAX requests and an API endpoint. – AymDev Nov 27 '20 at 10:33
  • This answer from another question might be helpful: https://stackoverflow.com/questions/5968280/how-to-run-a-php-function-from-an-html-form – Baris Ulgen Nov 27 '20 at 10:35
  • Makes sense to me, now that you mention it. I need to make this work in another fashion then. – Malgosh Nov 27 '20 at 10:35
  • @baris Thanks. I will check it out immediately. – Malgosh Nov 27 '20 at 10:36
  • Does this answer your question? [How do I pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-do-i-pass-variables-and-data-from-php-to-javascript) – AymDev Nov 27 '20 at 10:37
  • Does this answer your question? [How to run a PHP function from an HTML form?](https://stackoverflow.com/questions/5968280/how-to-run-a-php-function-from-an-html-form) – ariefbayu Nov 27 '20 at 10:38
  • Thanks for the help, lads. Really helped me out here. But now I have another question that is kind of related with this one. But first, would I need to edit the question above or do I need to open up a new question? Just want to make sure I follow the customs right. – Malgosh Dec 01 '20 at 09:10

1 Answers1

0

OK guys. Thanks a lot at all for the amazing links you send me. And sorry for the long absence. But thanks to you I managed to make the call using Ajax and it worked just fine. This is how I did it.

First, the function within DashboardController.php that will change the refresh timer of my dashboards to 10 seconds.

public function actionSetRefreshTimer() {
    $url = Yii::app()->request->url;

    Yii::log("I am not certain if I was here!");
    if (isset($_GET['DASHBOARD_RELOAD']) !== 10) {
      Yii::log("And it went into the if!");
      CHtml::refresh(Yii::app()->config->set('DASHBOARD_RELOAD', 10));
    }

And now the ajax call that got the job done.

<button id="tenSeconds">10 Seconds</button>

  <script>
$("#tenSeconds").click(function() { 
  console.log("The click worked just fine");
  $.ajax({
    url: '/path/to/my/function/SetRefreshTimer',
    method: 'POST',
  }); 
});
</script>
Malgosh
  • 340
  • 1
  • 11