0

After one of my PHP Scripts, I am running this code

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

However, no alert is shown. Is this a limitation with how the DOM is loaded, and if so, how do I work around it?

nathanfranke
  • 775
  • 1
  • 9
  • 19
  • Possible duplicate of [Calling a JavaScript function returned from an Ajax response](https://stackoverflow.com/questions/510779/calling-a-javascript-function-returned-from-an-ajax-response) – Mike Mar 01 '18 at 05:25
  • 1
    You can't just insert a ` – Mike Mar 01 '18 at 05:29
  • Thanks for this, can you go more in depth about how to run .js files in PHP? – nathanfranke Mar 01 '18 at 05:47
  • I mean regular external javascript files. They are loaded by the browser and not parsed by PHP. You just link to them from your HTML (``). – Mike Mar 01 '18 at 06:00
  • `echo ('');` does not do anything... :( It simply adds that tag to the HTML and doesn't execute (script.js: `console.log("It worked!");`) – nathanfranke Mar 01 '18 at 16:07
  • Forget about PHP returning scripts and put the script in your HTML file. The AJAX call should simply return some sort of basic data. For example, if you want to get a book from your database and dynamically insert it into your page, you would have PHP return a JSON object in the AJAX response and based on the values contained in that object (e.g. title, excerpt, price, etc), update your DOM. But all the javascript necessary to make that change has already been loaded directly by your original page. – Mike Mar 01 '18 at 17:15

3 Answers3

0

Try to use javascript in this way:

window.top.window.yourFunctionName();

in your case, it would be like:

echo ('<script type="text/javascript">window.top.window.alert("hi");</script>');
slon
  • 1,002
  • 1
  • 8
  • 12
  • Sorry, this doesn't seem to work for me. Adding an echo for "hello" displays properly, so it must be a problem of the ` – nathanfranke Mar 01 '18 at 04:59
0

The issue here is that you have a typo. It should be $_FILES not $FILES Also you are not using the move_uploaded_file() function properly. The syntax is as follows

move_uploaded_file($tmp_name, "$uploads_dir/$name");

Where

$tmp_name = $_FILES["file"]["tmp_name"];

$uploads_dir = 'Location Folder of where the file should be stored';

$name = $_FILES["file"]["name"];

Also i strongly advise you add some validations for this upload module. As it is, malicious files such as trojans etc can easily be uploaded.

Rotimi
  • 4,783
  • 4
  • 18
  • 27
  • I fixed the typos, they are just a side effect of me being new to PHP. I can, however, be guarenteed that the code is running, because adding normal HTML (Not JavaScript) to the echo command will display properly – nathanfranke Mar 01 '18 at 04:57
0

As @mike suggested, the problem is that simply appending a script to the DOM usually doesn't execute it. The solution is to change the logic of the code.

Either the PHP code should redirect the browser to another page, or the JavaScript AJAX should have an event listener for the upload completion.

nathanfranke
  • 775
  • 1
  • 9
  • 19