Creating a javascript alert with php that has a php variable inside?

后端 未结 4 1902
忘了有多久
忘了有多久 2021-02-08 13:14

I\'m making a form that is supposed to create a javascript alert when some fields aren\'t filled out or filled out properly. I want to be able to take the error messages I\'

相关标签:
4条回答
  • 2021-02-08 13:31
    <?php
     echo "<script type='text/javascript'>alert('{$_SESSION["success"]}');</script>";
     unset($_SESSION["success"]);
    ?>
    

    Use this code it would work correctly

    0 讨论(0)
  • 2021-02-08 13:35

    You can use function follow this:

    function died($error) {
        echo '<script> alert("'.$error.'")</script>';
        die();
    }
    
    0 讨论(0)
  • 2021-02-08 13:39

    Display variable php in alert javascript

       <?php 
              function died($error) { ?>
    
                <script>alert("<?php echo $error; ?>")</script>
    
        <?php   die(); 
              } ?>
    
    0 讨论(0)
  • 2021-02-08 13:41

    You only forgot quotations that are required for the JavaScript alert.

    If you passed 'hello' to the function, your current code would create alert as:

    alert(hello)
    

    instead of doing:

    alert("hello")
    

    Therefore, change your line to the following (two double quotes are added before and after concatenating $error):

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

    and you can use your function:

    died('error on whatever');
    
    0 讨论(0)
提交回复
热议问题