too long

This question already has an answer here:

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>";
</div>

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.