Calling a javaScript function in a php echo dosnt work [duplicate]

I have a website where, under certain conditions, a PHP file is executed. Within this PHP function, an if statement includes an echo that is supposed to call a JavaScript function. However, instead of executing the JavaScript, it simply displays the code as text. I’ve also tried using a simple alert, but it still doesn’t work. Interestingly, when I access the PHP file directly, without invoking it through the HTML page via a button, it works perfectly fine. What could be causing this issue?

here is some code:

call in html:

<form id="form" action="assets/php/file.php" method="POST" onsubmit="submitEditForm(event)">

the echo in the php file:

echo '<script type="text/javascript">', 'alert("hello world!");', '</script>';

result: (the text that is displayed on the website through the echo):

<script type="text/javascript">alert("hello world!");</script>

I hope we can find the problem or, alternatively, a way to implement it.your text