如果PHP中的条件为true,如何自动显示按钮? [英] How to auto display button if condition in PHP is true?

查看:209
本文介绍了如果PHP中的条件为true,如何自动显示按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果PHP中的IF条件为true,我想自动显示(不刷新页面)按钮.

I want to auto display (without refreshing the page) button if, IF condition in PHP is true.

我尝试过:

<?php if($diffex >= "90"){  ?>
      <button class="btn btn-large btn-primary" type="submit" style="float:right;" name="btn-resend">
         Resend OTP
      </button>
<?php }  ?>

但是上面的代码需要刷新页面,我希望不刷新页面.

But the above code needs the page to be refreshed and I want it without refreshing it.

我的$diffex的计算公式如下:

<?php
$eotd="";
$otd="";
if(isset($_GET['od'])){
    $eotd = $_GET['od'];
    $otd = base64_decode($eotd);
}
date_default_timezone_set('Asia/Calcutta');
$cdate = date('Y-m-d H:i:s ', time());
$scdate = strtotime($cdate);
$dscdate = base64_encode($cdate);
$deotd = strtotime($otd);
$diffex = $scdate - $deotd;
?>

实际上,如果the current timeURL中的时间大90s

Actually, I need this for displaying Resend Option if the current time is greater than the time in URL by 90s

推荐答案

正如您已经指出的那样,在PHP的服务器端无法做到这一点.每当需要显示或隐藏按钮时,您都需要在客户端运行JavaScript来更新DOM.

As you've already noted, there is no way to do this in server-side in PHP. You'll need JavaScript running on the client side to update the DOM whenever the button should be displayed or hidden.

在最简单的级别上,按照您的要求进行操作,您可能最终会将AJAX请求发送到PHP脚本,该脚本将响应JSON中的客户端JavaScript.有了这些数据后,您可以从那里更新视图.

At the simplest level, to do what you're asking, you'll probably end up sending AJAX requests to a PHP script that will respond to your client-side JavaScript in JSON. Once you have that data, you can update the view from there.

示例:

check_diffex.php

<?php

header('Content-Type: application/json');

// You would calculate a real value here

echo json_encode([
  'diffex' => 101
]);


JavaScript

var checkState = function(){
  jQuery.ajax({
    url: '/check_diffex.php'
  }).done(function(data){
    var button = jQuery("#myButton");
    if(data.diffex >= 90) {
      button.show();
    } else {
      button.hide();
    }
  });

}

checkState();
setInterval(checkState, 10);

带有诸如本机JavaScript和jQuery之类的棘手部分是决定如何构建代码来检查diffex >= 90与您的应用程序的其余部分.即使是最简单的如果这样,那么那个"检查,很多快速而肮脏的实现都会迅速变得令人头疼.

The tricky part with something like native JavaScript and jQuery is deciding how to structure your code that checks that diffex >= 90 with the rest of your application. A lot of quick and dirty implementations will quickly turn into a headache, even for the simplest of "if this, then that" checks.

这些天来,您要执行的操作中最易于访问且可维护的方法是采用单页应用程序(SPA)框架,例如:

These days, the most accessible and maintainable way to do what you're asking is to adopt a Single Page Application (SPA) framework, such as:

  • Vue.js
  • Angular
  • React

我强烈建议您看一下Vue,因为您可以将它集成到现有项目中,而不必重写整个前端.

I highly recommend you take a look at Vue, as you can integrate it into existing projects without having to rewrite the entire front-end.

这篇关于如果PHP中的条件为true,如何自动显示按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆