Search code examples
javascriptphpalertlocation-href

how to get alert and location.href to both work in the same script


I have the following in a php controller:

echo '<script type="text/javascript">';
echo 'alert("Emails have been added to the database")';
echo 'location.href="https://www.sustainablewestonma.org/update.php"';
echo '</script>';

exit();

The alert by itself works and the location.href by itself works. When combined I just get a blank page and neither seems to be working


Solution

  • Add a ; at the end of the alert to separate the two lines:

    echo '<script language="javascript">';
    echo 'alert("Emails have been added to the database");';
    echo 'location.href="https://www.sustainablewestonma.org/update.php"';
    echo '</script>';
    
    exit();