Using if(isset($_POST['submit'])) to not display echo when script is open is not working

Using if(isset($_POST['submit'])) to not display echo when script is open is not working

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

Stefan Bogdanescu

Founder & Senior Architect

The Hidden Side Effect of isset() on Form Submission

The core issue in your script stems from a misunderstanding of how HTML forms and server-side variables interact upon submission. You want to hide the form when data is submitted, but you are checking for the wrong variable. In your current implementation, you are wrapping the entire output block—including the echoed search results—inside if(isset($_POST['submit'])).

This logic fails because of how the HTTP request cycle works. When a user clicks the "Submit" button, the browser sends a POST request containing the form data and the value of the submit button itself. If you check isset($_POST['submit']), the condition evaluates to true only when that specific input is present in the request.

However, there is a critical flaw in your HTML structure relative to this logic. Look closely at the placement of your PHP block:

<form action="exam_interface.php" method="post" name="sessionform">
    <!-- Form fields here -->
    <input type="submit" value="Submit" />
</form>

<?php
// ... Database logic and output ...
?>

The form is defined with method="post". This means that when the user clicks "Submit," the browser sends a POST request. The $_POST superglobal array receives all submitted data, including $_POST['submit'].

Your code checks if(isset($_POST['submit'])) and then outputs the results. On the very first load (GET request), $_POST['submit'] does not exist, so the PHP block executes nothing, and the form displays correctly. This is good.

On submission (POST request), $_POST['submit'] does exist. Your condition becomes true. The code runs, queries the database, and echoes the results. So far, this seems to work as intended: show results on submit, hide them on load.

Wait—why does your specific report say it's not working?

The problem lies in the interaction between the form definition and the PHP logic inside the same file. If you are seeing no output when submitting, it is highly likely that the form action or the variable name matching is slightly off in your actual environment, or there is a whitespace issue. But more commonly in this specific pattern, developers forget that isset() returns false for keys that exist but have empty values.

In your code:

$grade = isset ($_POST['grade']) ? $_POST['grade'] : "";

This handles empty inputs well. However, the check for the button relies on the existence of the key. If the form submission fails silently (e.g., due to a redirect loop or browser caching), the $_POST array might be empty entirely.

The real solution to "not displaying echo when script is open" while showing it on submit is usually to separate concerns: use the request method to determine what to show, rather than just checking for the existence of the button.

A more robust approach uses isset($_SERVER['REQUEST_METHOD']) or checks if a specific action parameter exists that you set explicitly, rather than relying on the submit button's presence which is technically part of the form data but semantically distinct from "action taken."

Here is how you can refactor the logic to ensure the form disappears and results appear reliably:

<?php
// Define a flag for submission. 
// We check if 'submit' exists in POST, but we also need to ensure we are on a POST request.
if (isset($_POST['submit']) && $_SERVER['REQUEST_METHOD'] === 'POST') {
    
    // Your existing variable extraction and sanitization goes here
    $sessionid = isset($_POST['sessionid']) ? $_POST['sessionid'] : "";
    // ... rest of variables

    // Run your query logic
    // Note: You are using deprecated mysql_* functions. 
    // These have been removed in PHP 7.0+. Use mysqli or PDO instead.
    
    // Execute query...
    
    // Output results
    echo "<p>Your Search...</p>";
    // ... table output ...

} else {
    // Display the form only when it's a GET request or no submit button was sent
    ?>
    <form action="" method="post">
        <!-- Your form fields -->
        <input type="submit" value="Submit" name="submit" />
    </form>
    <?php
}
?>

The key takeaway is ensuring that the logic explicitly guards against executing the database query and output block when only a GET request arrives. Additionally, your source code uses mysql_* functions which are deprecated and removed in modern PHP versions. Migrating to PDO or MySQLi is not just a best practice; it is mandatory for security and compatibility.

If you find yourself needing conditional rendering based on user actions repeatedly, consider passing an action variable via the URL (e.g., ?action=search) rather than relying solely on the presence of the submit button in the POST array. This makes your logic cleaner and less prone to edge cases where form fields are empty or buttons are styled differently.

For modern Laravel projects, this pattern is handled by controllers returning views based on state, but in vanilla PHP, strict separation of the "display form" block and the "display results" block using if/else is the standard solution.

Our network