Hello,

I read an article on "how to prevent users from submitting a form twice." at the url

I was able to freeze the web page but was not able to unfreeze the web page. I am using PHP with javascript. Nothing happens when my code executes the "unfreeze" js function. I tested this js function and it only worked after I disabled the 2 php header() functions below. The JS unfreeze function just stops working with the php header functions enabled. There were no errors. Only the web page still freezes.

I hope someone can give me advice to help me solve this issue. Thanks in advance.

Below is my code in short version:

<?php
ob_start();

$hearingform = '
<html>
<head>

<style type="text/css">
.....
</style>

<div align="center" id="FreezePane" class="FreezePaneOff">
<div id="InnerFreezePane" class="InnerFreezePane"> </div>
</div>

<script type="text/javascript">

function FreezeScreen(msg) {
scroll(0,0);
var outerPane = document.getElementById("FreezePane");
var innerPane = document.getElementById("InnerFreezePane");
if (outerPane) outerPane.className = "FreezePaneOn";
if (innerPane) innerPane.innerHTML = msg;
}

function unFreezeScreen() {
var outerPane = document.getElementById("FreezePane");
outerPane.className = "FreezePaneOff";
}';

//---------------------------------------------------------
// some code to pull data from the database here...
//---------------------------------------------------------

// unfreeze the webpage
echo '<script type="text/javascript">unFreezeScreen();</script>';

/*send data to an excel file*/
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$filename");

echo $data;

exit;
} //END post IF

print $hearingform;

ob_flush();
?> 

Dani AI

Generated

The behavior seen here is expected: the script that returns the CSV uses Content-Disposition: attachment, so the browser treats the entire HTTP response as a downloadable file and does not render or execute any HTML/JavaScript contained in that same response. That explains why unFreezeScreen() “works” only when the header calls are removed. As suspected, this is the form-handling response (not the original form page); and if the submission were via AJAX (as asked) the approach changes.

Practical fixes that keep the UI responsive and prevent double-submit:

  • Post/Redirect/Get (PRG): process the POST, then header('Location: /done.php?file=...') and exit. The redirected page can run unFreezeScreen() and present a download link or auto-start the download with a separate request. This both prevents duplicate posts and separates the HTML/JS from the CSV bytes.

  • Download in a separate browsing context: have the form submit to a download endpoint in a new tab/window or to a hidden iframe so the parent page remains active and JS can run. Example patterns:

<form action="export.php" method="post" target="_blank">...</form>

or

<form action="export.php" method="post" target="download_iframe">...</form>
<iframe name="download_iframe" style="display:none"></iframe>
  • AJAX flow: POST via XHR/fetch, receive a JSON response containing a secure download URL or token, call unFreezeScreen() on success, then trigger the download (set window.location or create & click a temporary anchor). To detect the download start reliably, many implementations use a short-lived cookie set by the download response and poll for it from JS.

Troubleshooting: verify the response headers in the browser Network tab (Content-Type/Disposition), check PHP for “headers already sent” warnings (order of header() vs output matters unless using buffering), and avoid mixing HTML/JS and an attachment in the same HTTP response. The simplest robust fix is to separate the UI (confirmation page) from the file download.

Recommended Answers

All 2 Replies

It's not clear, is this a script that composes the page with the form on it or a script that's executed in response to the form being submitted?

Much of the code indicates the one and the rest indicates the other.

are you submitting the form via ajax? bring in the code bro

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.