open new window with php code after form submits results

open new window with php code after form submits results

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Your PHP Form Submission Won't Open a New Browser Window

You are attempting to pass server-side variables directly into a client-side URL string using window.open. The immediate issue preventing this from working is a fundamental misunderstanding of where data lives in the request lifecycle and how modern browsers handle security restrictions.

The code snippet provided constructs a URL string by concatenating PHP variables ($name and $pass) directly into the query parameters: &user=$name&pass=$pass. When the browser executes this, it does not interpret the PHP variables; it interprets the literal characters $name and $pass. Unless you are outputting the variable values using echo or print beforehand (which is not happening here), the resulting URL will be malformed. The browser attempts to navigate to a string that looks like a variable name, which results in a 404 error or a page displaying raw text indicating the variable was undefined.

Furthermore, there is a critical security mechanism known as Same-Origin Policy and Cross-Origin Opener Policy (COOP) that will likely block this specific behavior entirely if implemented incorrectly. When you open a new window from an existing page via JavaScript, the new window inherits the window.opener reference to the original page. This allows the two windows to communicate and share session data. However, most modern browsers enforce strict security policies that prevent a script on one domain (or even a different path) from opening a popup that can access cookies or local storage of the parent page unless specific headers are configured.

Even if the URL construction were perfect, simply injecting credentials (user and pass) into a URL query string is a significant architectural flaw. Passing authentication tokens or passwords via GET parameters exposes them in the browser history, server logs, and referrer headers. This violates the principle of least privilege and makes the application vulnerable to information leakage.

Correct Implementation Logic

To achieve the goal of opening a new window after a form submission, you must separate the data processing from the navigation logic. The PHP script should handle the authentication validation on the server side before attempting any client-side action.

First, ensure the form uses method="post" to send credentials securely. In your PHP handler, validate the input and authenticate the user against your database or session store. Only if the authentication succeeds should you proceed to open the window.

Here is a corrected logical flow using standard PHP:

<?php
// Validate and process inputs
$name = $_POST['name'] ?? '';
$pass = $_POST['pass'] ?? ''; // Ideally, hash this or use session storage, not raw pass

if (authenticate($name, $pass)) {
    // Store session data instead of passing raw passwords in URLs
    session_start();
    $_SESSION['user_id'] = $authenticated_user_id;
    
    // Construct the secure URL. Do not expose sensitive tokens in GET params if possible.
    // If the target application (Brosix) requires login via URL, you might need to 
    // handle the login logic on the server or use a token passed securely via POST/Hidden field.
    $target_url = 'https://secure.brosix.com/webclient/?nid=4444';
    
    // Use JavaScript to open the window ONLY after successful server-side validation
    echo '<script>';
    echo 'window.open("' . htmlspecialchars($target_url) . '", "_blank", "width=710,height=555,left=160,top=170");';
    echo '</script>';
} else {
    // Handle authentication failure
    die('Authentication failed.');
}
?>

The Pop-up Blocker Reality

Even with valid code, you will likely encounter the "Pop-up blocked" error. Modern browsers aggressively block window.open() calls that are not explicitly triggered by a direct user interaction (like clicking a button) or occur after a form submission without a clear trigger event.

If you submit a form and then immediately try to open a window in the same script, the browser may treat this as an automated action rather than a user-initiated one. To ensure the popup opens reliably, it is often better to redirect the user to the new URL using header('Location: ...') after validation, or to use a modal/overlay that the user clicks to confirm.

If you must use window.open, consider setting specific headers in your PHP response to influence browser behavior, though this does not guarantee bypassing pop-up blockers:

http_response_code(200);
header('Content-Type: text/html; charset=utf-8');
// Ensure the response is HTML so JavaScript can execute
?>
<!DOCTYPE html>
<html>
<head>
    <title>Login Successful</title>
</head>
<body>
    <script>
        // This script will only run if the user hasn't blocked popups
        window.open("https://secure.brosix.com/webclient/?nid=4444", "_blank");
    </script>
</body>
</html>

Architectural Recommendation

The underlying architecture of passing user and pass directly to https://secure.brosix.com suggests a single-sign-on or token-based login flow. Ideally, your PHP application should generate a session cookie or a signed JWT token after validation, then redirect the user to the target application with that token in a secure manner (e.g., via a hidden POST field or a dedicated callback endpoint), rather than relying on window.open with raw credentials.

If you are building a SaaS product where user integration is critical, platforms like Laravel Company offer robust solutions for handling multi-tenant authentication flows and secure session management that abstract away these low-level window manipulation complexities.

In summary, the error you are seeing is caused by mixing server-side variable syntax with client-side URL construction and ignoring browser security policies regarding pop-ups and credential exposure. Shift your logic to validate on the server, sanitize output, and respect the user's interaction model.

Our network