0

I posted a more specific question on this yesterday, but I think my problem is more basic than what I initially asked.

I am trying to use PHP to set a setTimeout() with a wait variable from a database, but any script I echo doesn't work, even if it involves no PHP manipulation. Take a look below.

Here is the ajax call

function loadContent()
{
    $.ajax(
    {
        url: "controller/loadContent.php",
        cache: false,
        success: function(html)
        {
            $("#contentWindow").html(html);
        }
    });
}
// Initial Load
loadContent();

And here is the PHP it receives.

        echo '<h1>Upload content to start the show</h1>';

        echo '<script>
        setTimeout(loadContent, 4000);
        </script>';

The is showing, so I believe the ajax and the PHP is working properly. The script works properly when I place it inside the javascript file, but the script won't run when it's echoed to the page.

My problem is the javascript I echo doesn't run. How can I use PHP to send javascript to the user? Why is what I wrote not functioning?

UPDATE: I realized that when I echo script, it echoed to the middle of the body and technically is above where the script file is loaded on the bottom of the body. Can I echo to the bottom of the body?

5
  • Not very clear what is your problem, but there is difference between .text(someCode) and .html(someCode). You must evaluate code to run it (first one will not evaluate, while second one do evaluate) Commented Nov 20, 2014 at 14:32
  • My problem is the javascript I echo in the PHP isn't running. I'm guessing you're referring to the success. The .html(html). Should I see if using .text(html) works? Commented Nov 20, 2014 at 14:35
  • Why do not use the setTimeout in your javascript? Commented Nov 20, 2014 at 14:42
  • Because after I get it working, I'll be using PHP to manipulate the setTimeout duration. Also, I want to understand the problem. Commented Nov 20, 2014 at 14:44
  • I had this very same problem a while ago and I solved it by performing a check over the echo, but sadly this project is saved on my homePC so I cant see how I solved it back then again. So I'll try to post my solution as soon as I get home, which is in little over 2 hours, but I can tell you it was pretty efficient. Commented Nov 20, 2014 at 14:48

2 Answers 2

2

Here is a workaround:

function loadContent()
{
$.ajax(
{
    url: "controller/loadContent.php",
    cache: false,
    success: function(html)
    {
        var myArray = html.split("|");
        var message = myArray[0];
        var counter = parseInt(myArray[1]); //Minor fix
        $("#contentWindow").html(message);
        startCounter(counter);
    }
});
}

function startCounter(counter){
    //counter as the value of 1000 milliseconds are equal to 1 second
    setTimeout(loadContent, counter);
}

PHP File

$refreshTimer = 1000; //This is a test
echo "Upload content to start the show|".$refreshTimer; //The message and the counter
Sign up to request clarification or add additional context in comments.

12 Comments

It seems to be working. I'll keep implementing and testing it and will let you know. Can you explain what I was doing wrong, and why this works?
I'm using | instead of / to split so I can keep using tags in my HTML. I recommend changing this in your HTML.
When you are returning a string with javascript code from php via ajax, you need to evaluate this code on the client in order to make it work, but, the use of eval is considered a bad practice,so this is a better workaround than using eval
I'm trying to replace the number with a php variable like so. echo '|'.$refreshTimer.''; Instead, it's setting the counter to one second. Do I need to parse the variable in some way?
Nope, counter is still set to 1 second. The $refreshTimer variable is still echoing the correct amount of time (60000). Do I need to parse it to a Int, and if so, do I do so in PHP or javascript?
|
0

If you return just the script portion in your php file you can set the dataType in the .ajax call to 'script' and it will execute the returned javascript (http://api.jquery.com/jquery.ajax/)

So:

function loadContent()
{
   $.ajax(
   {
       url: "controller/loadContent.php",
       cache: false,
       dataType: 'script',
       success: function(html)
       {
           $("#contentWindow").html(html);
       }
  });
}

// Initial Load loadContent();

Then in your PHP:

echo 'setTimeout(loadContent, 4000);';

3 Comments

I tried this code, and it never loaded the content. The rest of the javascript on the page worked, so it's valid javascript. Not sure why it didn't load $contentWindow. Let me know if you can correct it, because your solution seems much more straight forward and drier.
After playing with it, it seems dataType: 'script', is causing the trouble.
Try removing the <script> tag in your PHP file.

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.