phphtmlyiiyii1.x

How to call a PHP function through HTML input tag (Yii1.1)


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.


Solution

  • 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>