0
let lvlcount = 0;

  function loadmore(){

    if (lvlcount == 0){
      document.getElementById("d").innerHTML = lvlcount;
      lvlcount =+ 1;
    }else if (lvlcount == 1){
      document.getElementById("d").innerHTML = '<?php require("./parts/levels/lvl-456.php"); ?>';
      lvlcount += 2;
    }
  }

How can I run PHP Code in Javascript? I want to add Code when someone clicks on the button, the Code is saved in an PHP File.

m2v
  • 7
  • 1
  • Rather than ask how to go about implementing what you think is the solution to your problem, perhaps you'd have better luck asking about your problem. As other users have pointed out, there's a flaw in your mental model of the process you've imagined. Look-up "X/Y problem" for more on the concept. – enhzflep Sep 19 '20 at 06:12

2 Answers2

1

You can't do that. PHP runs on server side and JavaScript runs of client side. Imagine what would happen if the malicious user on client site get access to your database.

gpl
  • 1,380
  • 2
  • 8
  • 24
0

Better write your JS code from PHP before to send it to the client :

<?php
$output = '
  let lvlcount = 0

  function loadmore(){

    if (lvlcount == 0){
      document.getElementById("d").innerHTML = lvlcount
      lvlcount =+ 1
    } else if (lvlcount == 1) {
      document.getElementById("d").innerHTML = ';

    $output .= require("./parts/levels/lvl-456.php");

    $output .= 'lvlcount += 2
    }
  }';
  // Send it...
  echo $output;

Check for the syntax... lol... Or... If you have to "require" some other script than lvl-456.php, you have to send an XHR (Ajax call) from the client side JS.

Note that ";" are no more required in JS...

Jean-Luc Aubert
  • 620
  • 5
  • 19