1

I am trying make some div for message when is showing just when i call him. I need to call javascript to change div style (style="display:block;") but i don't know how.

I try call with echo, but now work.

I am try add in HTML file <script type="text/javascript">openmsgpF();</script> and that not work too.

Some other way how can i call javascript funtion?

MY PHP FILE:

if (isset($_POST['newsletter'])){
    $message='Thanks for signig up on our newsletter!';
    echo '<script type="text/javascript">openmsgpF();</script>';
}

MY MESSAGE DIV:

<div id="message-p" style="display:none;background-color: green;color: rgb(255, 255, 255);text-align: center;position: fixed;top: 0px;left: 0px;z-index: 999999;width: 100%;    font-size: 25px; font-style: normal;letter-spacing: 1px;height: 100px;padding-top: 30px;letter-spacing: 1px;">
    <?php echo $message;?>
    <div id="close_msg" style="position: absolute;top: 0;right: 10px;">
        <a style="color: #c6c6c6;" href="javascript:void(0);" onclick="closemsgF()">X</a>
    </div>
</div>

MY SCRIPT:

 <script>
    function closemsgF()
    {
        document.getElementById("message-p").style.display = "none";
        document.getElementById("message-n").style.display = "none";
    }

    function openmsgpF()
    {
        document.getElementById("message-p").style.display = "block";
    }

    function openmsgnF()
    {
        document.getElementById("message-n").style.display = "block";
    }
</script>
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34

2 Answers2

2

You have an issue with the order in which your javascript is loading. I presume you are echoing

<script type="text/javascript">openmsgpF();</script>

before you have printed your HTML. In that scenario, the elements are not yet on the page.

Try outputting your code with this order:

<?php
$message = '';
if (isset($_POST['newsletter'])){
    $message='Thanks for signig up on our newsletter!';
}
?>
<div id="message-p" style="display:none;background-color: green;color: rgb(255, 255, 255);text-align: center;position: fixed;top: 0px;left: 0px;z-index: 999999;width: 100%;    font-size: 25px; font-style: normal;letter-spacing: 1px;height: 100px;padding-top: 30px;letter-spacing: 1px;">
    <?php echo $message;?>
    <div id="close_msg" style="position: absolute;top: 0;right: 10px;">
        <a style="color: #c6c6c6;" href="javascript:void(0);" onclick="closemsgF()">X</a>
    </div>
</div>
<script>
    function closemsgF()
    {
        document.getElementById("message-p").style.display = "none";
        document.getElementById("message-n").style.display = "none";
    }

    function openmsgpF()
    {
        document.getElementById("message-p").style.display = "block";
    }

    function openmsgnF()
    {
        document.getElementById("message-n").style.display = "block";
    }
</script>
<?php
if (isset($_POST['newsletter'])){
    echo '<script type="text/javascript">openmsgpF();</script>';
}
?>

https://jsfiddle.net/de6ha4nf/

Renaud C.
  • 535
  • 2
  • 14
0

Besides of putting the script tag before the end of the body tag add the call to openmsgpf inside the window.onload() event for it to trigger when the page finishes loading.

To display conditionally you surround the function with an if statement. can be to check if the html in the message div is empty, or to check if you have included (from the php code) a specific css class or attribute in the message div. There are many ways.

<script>
    function closemsgF()
    {
        document.getElementById("message-p").style.display = "none";
        document.getElementById("message-n").style.display = "none";
    }

    function openmsgpF()
    {
        document.getElementById("message-p").style.display = "block";
    }

    function openmsgnF()
    {
        document.getElementById("message-n").style.display = "block";
    }

    window.onload(function(){

        var showMessage = <whatever condition you evaluate>;

        if(true === showMessage){
            openmsgpF();
        }
    });
</script>
Juan
  • 5,525
  • 2
  • 15
  • 26