0

I have JavaScript code I added it in a php variable But I want to implement it first and then take the information Example

$nam = "<script>
document.write(5 + 6);
</script>";

I want to execute the code and show the results in another variable to put in the database

INSERT INTO pro SET nam='$nam'

and I tried to use but failed

$nam = echo "<script>
document.write(5 + 6);
</script>";
  • 2
    You can't execute JavaScript inside PHP. – Spoody Apr 15 '18 at 20:00
  • What exactly are you trying to do and why? JavaScript executes in the browser, on the client, long after the PHP has finished executing. Your `$nam` variable is just a string, nothing more. And if you're just trying to execute `5 + 6` in PHP then, well, you don't really need JavaScript for that. PHP can do math. – David Apr 15 '18 at 20:03

1 Answers1

0

PHP is a server-side language. Javascript is a client-side language.

You can not and should not perform Javascript through text inside a PHP variable.

You can create a Javascript file that calculates 5+6 and sends the result to a PHP page through Ajax.

I suggest you read more about Ajax in the link I attached.

  • But remember, if you don't need this code to be rendered on the client browser then maybe it would be better to run it in PHP.
Niv Apo
  • 983
  • 1
  • 9
  • 18