user893970
user893970

Reputation: 899

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

Upvotes: 0

Views: 31520

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

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.

Upvotes: 5

Related Questions