如何从PHP触发Javascript中的警报

PHP newbie here...

I am trying to trigger an alert in JavaScript from PHP, but it doesn't seem to work. :

if(isset($_POST['save-text'])){

        // get form data, making sure it is valid
        $submit_date = date('Y-m-d H:i:s');
        $content = mysql_real_escape_string(htmlspecialchars($_POST['editor']));

        $new_url = 'example.com/' . $key;
        echo "<script>alert('$new_url');</script>";

        // build query
        mysql_query("INSERT INTO `source`(`id`, `key`, `submit_date`, `ip`, `content`) VALUES (null, '$key','$submit_date','$ip','$content')")
        or die(mysql_error());


    }

You can't insert HTML and JavaScript just anywhere in the page; make sure you're outputting in a location where the <script> tag is valid.

your quotes were wrong:

echo "<script>alert('$new_url');</script>";

should be:

echo "<script>alert('" . $new_url . "');</script>";