简体   繁体   中英

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. Here is the function which determines whether the form has been submitted and populates $_SESSION['prompt'] with data.

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.

<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? Are you using that data later?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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