dongtuji0992 2017-02-02 09:11
浏览 34

我如何在PHP __construct中执行Javascript函数

Well, i want to call a function like alert() (javascript) into the class construct PHP, when the POST variable is no define. but i don't no if it's posible.

class Su{

    private $nomb;
    private $dir;
    private $tel;

    public function __CONSTRUCT($mode){
        try {
            if(isset($_POST['nomb'])) {
                ?>
                <script language="javascript">alert("Sorry, this field can't be empty ")</script>
                <?php
                throw new Exception('?view=sucursal&mode=' . $mode . '&error=define');
            }
        } catch(Exception $error) {
            header('location: '.$error->getMessage());
            exit;
        }
        $this->nomb = strtoupper($_POST['nomb']);
        $this->dir = strtoupper($_POST['dir']);
        $this->tel = strtoupper($_POST['tel']);
    }

    public function __GET($var){ return $this->$var; }
    public function __SET($var, $valor){ return $this->$var = $valor; }

}
  • 写回答

1条回答 默认 最新

  • drd2551 2017-02-02 13:11
    关注

    What you are asking for, is not what's good for you. You ought to think differently about how to achieve your goal.

    php executes a script in less than 1 millisecond. It sits on the server, and only works when a client makes a request.

    Javascript is different. It works on the computer of the client; it constantly waits for events (mouse clicks, keys pressed, timers, ...).

    You ought to use javascript for event handling. There can be no reason for PHP to dynamically generate javascript code (in your case the alert). No, you make sure that alert is already in the javascript code, in some function. Then you let javascript call that function.

    What you can do, is get a result from the server, and according to that result you let javascript decide to call that (or another) javascript function. This is what AJAX is about.

    Do you know and use jQuery?

    Here is an example. Let's say "ajax.php" prints the hour of the day.

    <div id="welcome"></div>
    <script>
      $.ajax({
        url: 'ajax.php',
        success: function(output) {
          var hour = Number(output);
          if(hour < 11) {
            good_morning();
          }
          if(hour >= 11 && hour < 14) {
            good_day();
          }
          if(hour >= 14 && hour < 18) {
            good_afternoon();
          }
          // etcetera  ...
        }
      });
      function good_morning() {
        $('#welcome').html('Good morning, what a loverly day ...');
      }
      function good_day() {
        $('#welcome').html('Good Day, ...');
      }
      // etcetera  ...
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)