-3

I have to execute javascript that's written inside a .php file. I do understand that javascript is client side and php is server side language and I'm not supposed to be doing this but that's the project guidelines and hence I have to do it.

Issue: Whenever I load my php file from localhost( I'm using Apache on Mac), the javascript functions do not work. I know the code is correct as it works fine when the same js code is within an html file. I have tried writing js code in <script>.....</script> and <?php echo" <script>...</script>"?> but neither seem to work and I'm not permitted to use JQuery either. Entire code has to be within one php file. I have searched throughout stack overflow and all I have seen so far is to use <script>.....</script> and <?php echo" <script>...</script>"?> and they're not working. The html and css in the same file work properly.

Any help on how to solve is much appreciated. Thank you!

Navaneeth
  • 9
  • 5
  • "Does not work" is not a useful problem description. Look at the rendered source, javascript console, etc. – mario Mar 01 '19 at 01:22
  • As stated you might have an error elsewhere in your code that we have no clue what it is because you did not post your full code. My example will run PHP then Javascript within the same file, I even tested it myself. – ABC Mar 01 '19 at 01:23
  • What is the source code? See [mcve] and also [ask]. What does it mean: *'I have to execute javascript that's written inside a .php file'*? Be more verbose what is actually intended. What are the actual headers? Show the the raw results from the developer tools in your browser or perform a request via *curl* or *wget* from the command line. – Pinke Helga Mar 01 '19 at 01:26
  • I couldn't disclose the code because it's a school assignment, otherwise I would have and I'm just starting out with this so, I have no idea how to do those things to get the raw output and such. I have been as verbose as I could be. The problem was solved when I used a new file and copied the code from the original to it. Still doesn't work in the original file though. – Navaneeth Mar 01 '19 at 19:24

1 Answers1

0

Think this is what you are looking for: How to call a JavaScript function from PHP?

PHP file is server side yes, so is Javascript depending on what you are doing with it. PHP can absolutely execute a Javascript script within its main .php file. You just can not include anywhere in your program like before.

Below is an example to run a Javascript script that simply alerts the browser to the string "pong". Notice how the PHP statement is still executed.

We could be more helpful if you posted the full code and were very efficient and detailed explaining what you needed to be answered.

Copy and paste this code into a PHP file by itself, so you can test without any errors your full code might contain: index.php

<?php
    echo 'Ping' ?>
<script>alert('Pong');</script>


<?php
    // PHP code goes inside here
?>
<script>
   // Javacript code goes inside here
</script>
ABC
  • 2,068
  • 1
  • 10
  • 21
  • He didn't escape it to direct output mode. – ABC Mar 01 '19 at 01:14
  • I forgot to add ; – ABC Mar 01 '19 at 01:17
  • @Navaneeth my example works, copy and paste it within a .php file by itself to test. Because you might have errors elsewhere. – ABC Mar 01 '19 at 01:22
  • @Raymond I tried as you had suggested. Made a new file and it worked! So, I copied all the javascript, html and php code from the file it didn't work to the new file and it works. Weird thing is it still doesn't work in the original file. Thank you for your help though. – Navaneeth Mar 01 '19 at 19:21