Load modal after form submit

Load modal after form submit

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

Stefan Bogdanescu

Founder & Senior Architect

Why Your show() Method Fails on a Form Submit

You've likely encountered this scenario: a form submits successfully, validation passes, and your logic executes without error, yet the confirmation modal never appears. In the specific case of triggering a Bootstrap modal via PHP output, the issue almost always lies in how the browser interprets the script tag you are echoing.

The core problem isn't that the show() method doesn't exist; it's that the HTML entity decoding and escaping logic often breaks when mixing server-side rendering with client-side event listeners. When you echo a string like <script>$('#thankyouModal').modal('show')</script> directly from PHP, the browser expects to receive a complete, valid script block. If any part of the surrounding HTML is escaped or if the script tag isn't parsed in isolation, the JavaScript parser will fail before it even attempts to run your command.

Escaping Characters and Script Tags

In many server-side languages, including PHP, you must be careful about how you output HTML strings. The original code snippet demonstrates a common pitfall:

echo "&lt;script&gt;$('#thankyouModal').modal('show')&lt;/script&gt;";

Here, the < and > characters have been escaped to &lt; and &gt;. While this is safe for displaying text, it is destructive for executing code. The browser sees a literal string containing the letters "s", "c", "r", "i", "p", "t" rather than an actual opening script tag. Consequently, the JavaScript engine inside the browser never registers the event listener or executes the method call.

The solution requires echoing the raw HTML tags so the browser's parser can recognize them as structural elements:

echo "<script>$('#thankyouModal').modal('show');</script>";

By removing the HTML entities, you allow the client-side jQuery (or Bootstrap) library to parse the command correctly. The script tag becomes a container for executable logic, not a static text string.

Execution Context and Timing

Beyond escaping, there is a timing consideration inherent in server-side rendering. When a form submits via POST, the browser requests a new page from the server. The server processes the request, sends back an HTML response, and only then does the browser render that content.

This means your modal logic runs after the DOM has been freshly built by the browser. Unlike AJAX calls where you can append data to an existing DOM element and then trigger modals within a success callback, a standard form submit replaces the entire document tree. As long as the Bootstrap modal HTML is present in the initial page load (which it should be), the $('#thankyouModal') selector will find the element immediately upon script execution.

However, if you are using a framework like Laravel or Blade, remember that server-side variables cannot directly output raw JavaScript code without proper escaping directives to prevent XSS vulnerabilities. In PHP native files, direct echoing is acceptable for this specific use case, provided the content is trusted and generated by your own logic.

A More Robust Approach

While fixing the escaping issue solves the immediate problem, relying on echo inside a form handler is brittle. If you ever need to pass dynamic data to the modal—such as a personalized thank-you message—you would have to escape that string for HTML output inside the script tag, which quickly becomes messy:

// Messy and prone to errors
echo "<script>$('#thankyouModal').modal('show', {message: '" . addslashes($subject) . "');}</script>";

A cleaner architecture involves separating concerns. Keep your form handling logic focused on data processing (validation, sanitization, sending emails) and return a JSON response or redirect to a specific view that renders the modal. Alternatively, use a framework's built-in session flash messages. These are rendered by the view layer in a controlled manner, ensuring that HTML entities are handled correctly by the templating engine rather than manually escaped string concatenation.

For immediate fixes on legacy code, simply ensure your script tag is echoed as raw text:

echo "<script>$('#thankyouModal').modal('show');</script>";

This restores the expected behavior where the browser parses the tag, executes the jQuery method, and displays the confirmation dialog to the user.

Our network