0

I am trying to do a javscript where the div contents changes after a certain amount of time to include the next flash animation but javascript is not accepting my code. I've tried to escape <?php include 'flash-2.php'; ?> with this <?php include \'flash-2.php\'; ?>

<script type="text/javascript">
setInterval(function()  
        {
        var res = "<?php include \'flash-2.php\'; ?>";
        document.getElementById("swfdiv").innerHTML = res;
        }, 
        3000);
</script>
1
  • 2
    How can you use Server-side include at client-side? Commented Mar 19, 2015 at 10:19

4 Answers 4

2

PHP code is executed BEFORE the page is rendered to the user. Always. No exceptions. By the time your JS runs, it will do nothing.

If you want to include something in your page after it's already loaded, you need to look at an asynchronous technology, like AJAX.

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

Comments

1

Ajax is your best bet:

<script type="text/javascript">
setInterval(function()  
{
        $.ajax( "example.php" )
    .done(function(res) {
            document.getElementById("swfdiv").innerHTML = res;
    })

}, 
 3000);
</script>

Comments

1

If you want to show output of you PHP file in a DIV, then call your php file through AJAX and then update the innerHTML of the DIV.

For example if you use jQuery then you can do like,

$.get( "flash-2.php", function( data ) {
  $("#swfdiv").html(data);    
});

Hope it helps, thanks.

Comments

0

use ajax like this

 <script type="text/javascript">
 setInterval(function()  
 {
    $.ajax({
        type: "POST",
        url: flash-2.php,
        success: function(res){
            if(res)
            {
                document.getElementById("swfdiv").innerHTML = res;
            }
        }
    }), 
    3000);
</script> 

Comments

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.