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>
7
  • Possible duplicate of How to call a JavaScript function from PHP? Commented Apr 12, 2017 at 11:45
  • @YuvalPruss i was try <script type="text/javascript"> jsFunction(); </script> and not work. Commented Apr 12, 2017 at 11:46
  • 1
    can u write a clean code man @RobertGlavaš don't know why you changed the edit back to your unreadable code Commented Apr 12, 2017 at 11:51
  • @Masivuye Cokile ah, sorry for that we are do edit in same time. Commented Apr 12, 2017 at 11:52
  • If you want to call the openmsgpF function when the page is loaded you have to put it in window.onload(); Is it this what you want? Commented Apr 12, 2017 at 11:56

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Hello, thanks!! Clearly my problem was in order... Thanks for helping!
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>

2 Comments

But i need call that function only when i want, not all time.
I edited the answer with how to conditionally show the message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.