0

What is the easiest way to run Javascript code inside PHP, preferably without using external libraries? Specifically, I need the innerHTML of a div to change depending on some calculations performed using the user's form inputs that were captured earlier with $_POST in a separate form. How can I get the following JS code to run inside the following PHP code? (My most basic attempt was using echo command, but it's throwing up errors)

HTML:

<div id="output">
                
</div><!--#output -->

PHP (desired JS included):

if (count($valid_locations)<$num_of_locations){
      echo '<script>const output = document.querySelector("#output");</script>;';
      echo '<script>output.innerHTML = "<div class="alert-danger"> Trip unable to be created. Please adjust the budget/number of travelers/duration etc.</div>;";</script>';
}else{
      createTrip($trips);
}

Right now, the IF condition is True and the code does run, but it echos ";" for some reason. I have verified that the condition is met and it runs by adding in a simple echo "this runs";, which did appear upon refresh. I just don't understand why it's outputting to the default echo space and the script isnt running to change the innerHTML of the output div. Is this even possible to do with PHP? If so, what's the best way? (doesn't need to be secure, won't be used publicly). I've heard of a few things such as ajax but it seems so complicated. Any explanations on stuff like that specific to this scenario would be greatly appreciated.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • - Also when it outputs ";", the console also includes the error: Uncaught SyntaxError: Unexpected identifier –  Jan 13 '22 at 12:43
  • Whats the point of the java tag here? – f1sh Jan 13 '22 at 12:43
  • 1
    http://javascriptisnotjava.com/ – OH GOD SPIDERS Jan 13 '22 at 12:44
  • Sorry, I thought they were similar, I'm a young new programmer just trying to get some projects done. –  Jan 13 '22 at 12:44
  • Any reason why you don't populate the `output` directly with `PHP` but modify it's contents with `JS`? – DarkBee Jan 13 '22 at 12:48
  • That's kind of exactly what I'd love to do, I just thought running JS inside it would be the best way to do this. Why, is there a way to update the "output" div HTML with PHP? –  Jan 13 '22 at 12:49
  • Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Alon Eitan Jan 13 '22 at 13:01

1 Answers1

0

Following up on the comments, you don't need JS to achieve what you want. You could just use PHP

<?php
    $output = null;
    if (!empty($_POST)) {
        //...
        //... do stuff
        //... 
       if (count($valid_locations)<$num_of_locations){
            $output = 'Error occured - bla bla bla';
       }
    }
?>
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <?php if (isset($output)) {?>
        <div id="output"><?= $output; ?></div>
        <?php } ?> 
    </body>
</html>
DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • Absolute legend. Thank you. Found this question everywhere and people are overcomplicating it, but this simple rearrangement (even though I literally have the same kind of thing in the rest of the code lol), just doesn't occur to me at first. Huge help, thanks again :) For anyone new like me wondering on their specific situation: Use PHP to change the variable to whatever you want it to be, and use PHP within the HTML div itself to capture the variable. –  Jan 13 '22 at 13:02