简体   繁体   English

寻找更好的方式来显示/隐藏元素

[英]Looking for a better way to show/hide an element

I'd like to know if someone could please show me a better way to show/hide an element on my page. 我想知道是否有人可以向我展示一种更好的方式来显示/隐藏页面上的元素。 This is how I am currently doing it. 这就是我目前正在做的事情。

At the very top of my index.php file, I have the code for my application. 在index.php文件的最顶部,我有应用程序的代码。 Here is the function which determines whether the form has been submitted and populates $_SESSION['prompt'] with data. 这是确定表单是否已提交并用数据填充$ _SESSION ['prompt']的函数。

private function validateForm() {
    if (isset($_POST['submit'])) {
        $_SESSION['prompt'] = 'Form submitted';
    }
}

And here is the element which I would like to show/hide depending on whether the form has been submitted located towards the bottom of my index.php file. 这是我想显示/隐藏的元素,具体取决于是否已将表单提交到index.php文件的底部。

<p id="dialog">
    <?php
        if (!empty($_SESSION['prompt'])) {
            echo $_SESSION['prompt'];
        }
        else {
            echo '<script>document.getElementById("dialog").style.display="none";</script>';
        }
    ?>
</p>

It's hardly a great leap in creative logic to work that one out 要实现这一目标,在创造逻辑上几乎没有很大的飞跃

<?php
    if (!empty($_SESSION['prompt'])) {
        echo '<p id="dialog">'.$_SESSION['prompt'].'</p>';
    }
    else {
      // nothing
    }
?>

Why are you setting $_SESSION variables that you then use in the same file, as opposed to just variables? 为什么要设置$_SESSION变量,然后在同一文件中使用它,而不是仅使用变量? Are you using that data later? 以后要使用这些数据吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM