1

I need ajax to run a script of both php and javascript in the background.

$.ajax("j_update.php");

And then: j_update.php:

//-> Get an XML file
$xmldata = simplexml_load_file('datafile.xml');

//->Look through
foreach($xmldata->user as $user) {

    echo "
        <script type='text/javascript'>
        animateOther(".$user->ip.", ".$user->left.", ".$user->top.");
        spectrumOther(".$user->ip.");
        </script>";

}

But (as I expected) this is impossible, the javascript doesn't get executed. I'm not sure what the correct way around it would be though... Any idea?

Thanks

2 Answers 2

3

Instead of $.ajax("j_update.php");, use $.getScript("j_update.php"); and erase the <script> open/close tags in the php file. Also remember to add header("Content-type: application/javascript"); at the very top of your php script.

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

Comments

0

Use $.getScript() instead of $.ajax() to ensure the JavaScript runs.

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.