Creating a javascript alert with php in which there is a php variable inside?

I am making a form that should generate a javascript alert when some fields are not populated or populated properly. I want to be able to receive error messages that I entered in a php variable and display them in a javascript warning window.

The following code does not work:

function died($error) {
    echo '<script type="text/javascript"> alert('.$error.')</script>';
    die();
}

How to add the line contained in $errorbetween two lines of a "script" so that it correctly displays as a javascript warning?

Thank!

+4
source share
3 answers

, JavaScript.

'hello' , :

alert(hello)

:

alert("hello")

( $):

echo '<script type="text/javascript">alert("'.$error.'");</script>';

:

died('error on whatever');
+11

php javascript

   <?php 
          function died($error) { ?>

            <script>alert("<?php echo $error; ?>")</script>

    <?php   die(); 
          } ?>
+2

You can use the function:

function died($error) {
    echo '<script> alert("'.$error.'")</script>';
    die();
}
+1
source

Source: https://habr.com/ru/post/1538334/


All Articles