简体   繁体   中英

Javascript alert in php script

I wanted to popup an alert box. After that, the site would redirect to main page. But, it seems that it directly redirect to the mainpage without alerting anything.

if($registerquery)
    {
    ?>

    <script>alert('received!')</script>
<?php
    }


    header("Location: mainpage.php");
    exit();


    ?>

I wanted to do this to ensure users that the process of submission ended successfully. How can i alert something before the page redirect to mainpage and more importantly what causes this? I think the page should not have redirected before the alert box.(Before these codes, site registers what users submitted but not relevant i guess.)Thanks

You just can't do this. PHP is server-side, JS is client-side. Using a location header is server-side, so the browser never gets the JS.

Instead, try something more like:

if( $registerquery)
    echo "<script>alert('received!'); location.href='mainpage.php';</script>";

and remove the header bit altogether.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM