Nicholas Corin
Nicholas Corin

Reputation: 2404

Issues with calling a JavaScript function from PHP

I have created a form to upload a a newsletter into the database. I'm using the iFrame method to post the date without the page refresh and I'm displaying a nice jQuery dialog for the loading.

I am now having issues with closing that dialog when the upload is complete. All the tutorials I have read online say that I must just echo out the code like this:

<?php
  echo "<script type='text/javascript>uploadComplete();</script>";
?>

Now the JavaScript does run when I do something stupid to test like just echo out an alert, but when I try and call any function or just go straight to the jQuery to close the dialog it just says that the function is not defined. I can post my code if it is necessary, but it's pretty standard and I didn't think I would need to in this kind of example.

Any help would be appreciated! Thanks in advance.

Upvotes: 1

Views: 250

Answers (3)

Nicholas Corin
Nicholas Corin

Reputation: 2404

I eventually solved this by using the following code:

<?php
  echo "<script type='text/javascript>top.uploadComplete();</script>";
?>

The "top" keyword just solved all of my problems. I hope this helps whoever has the same issue I had!

Upvotes: 0

mgraph
mgraph

Reputation: 15338

you forgot ' in the end of 'text/javascript'

echo "<script type='text/javascript'>uploadComplete();</script>";

NB : the type attribute is entirely unnecessary if the script is JavaScript

Upvotes: 5

Nikhil D
Nikhil D

Reputation: 2509

using just php

echo '<script type="text/javascript">'
   , 'jsfunction();'
   , '</script>';

escaping from php mode to direct output mode

<?php
// some php stuff
?>
<script type="text/javascript">
    jsFunction();
</script>

Upvotes: 1

Related Questions