Pls. help me with this code :) the alert box is not appearing and it goes directly to homebody_admin.php
I want the homebody_admin.php to appear after I click the ok button on alert box pls help.
Your help is highly appreaciated! thanks in advance :)

if($password!= mysql_result($result, 0))  
{  
    echo ("<SCRIPT LANGUAGE='JavaScript'>
        window.alert('You entered an incorrect password!')
        </SCRIPT>");
    //echo "You entered an incorrect password"; 
    header("Location:homebody_admin.php"); 
}  

Dani AI

Generated

Short diagnosis (for future readers): the PHP in the first post echoes JavaScript and then issues an HTTP redirect. The browser receives the Location header and navigates away before any client-side script runs, so the alert never appears. called this out correctly. Fixing the JS syntax (thanks ) won't change that flow, and echoing output before header() can also produce "headers already sent" warnings if output buffering is off.

A practical, robust solution is to use a session "flash" message: set the message on the server, redirect immediately, and let the destination page display the alert (or a nicer inline message). That keeps the redirect server-side and the UI rendering client-side where it belongs.

Example (login logic — set the flash and redirect):

<?php
session_start();
// on failed login
$_SESSION['error'] = 'Incorrect password';
header('Location: homebody_admin.php');
exit;

Example (homebody_admin.php — display once and clear):

<?php
session_start();
if (!empty($_SESSION['error'])) {
    $msg = $_SESSION['error'];
    unset($_SESSION['error']);
    echo '<script> alert(' . json_encode($msg) . '); </script>';
}

Notes and best practices: always call exit after header(); call session_start() before sending headers; use json_encode when embedding PHP strings into JavaScript to avoid XSS or quoting issues; prefer a styled inline message or toast instead of alert() for better UX; keep auth error messages generic for security. This approach also avoids the timing issues that caused the alert to be skipped in the original code.

Recommended Answers

All 6 Replies

Try using a semicolon after the window.alert() or additional attributes to the script-tag like type.

It's not appearing, because you're rendering content on the current page, then immediately redirecting the user to a different page, before they see the content.

<script type="text/javascript">
if(confirm('You entered an incorrect password'))
    window.location = 'homebody_admin.php';
</script>

Thank you so much for your help! yey! :)

echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('You entered an incorrect password!')
</SCRIPT>");

Try this.

echo( " <script language='JavaScript'>  
             alert('You entered an incorrect password!');
        </script>
      "

I hope your problem is solved. If it is, help others know the thread is solved.

SOLVED!!! THANK YOU VERY MUCH!!! ARIGATOU GOZAIMASU!!! GRACIAS!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.