PHP : How to put and pass variable in modal URL

PHP : How to put and pass variable in modal URL

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

Stefan Bogdanescu

Founder & Senior Architect

PHP : How to Put and Pass Variable in Modal URL

You are encountering a fundamental mismatch between how your browser handles navigation and how a modal works. In your current implementation, you are constructing a URL that includes a hash fragment (#myModal). When the user clicks this link, the browser attempts to navigate to main_user.php. This triggers a full page load or an AJAX request depending on your routing setup.

The critical issue is that a standard hyperlink click does not automatically populate a modal's hidden inputs. The URL main_user.php?user_id=123#myModal serves two distinct purposes: the query parameter ?user_id=123 changes the server-side context, while the fragment #myModal instructs the browser to scroll to an element with that ID. It does not inject data into the modal itself.

When your form submits via <form action="action/doEditUserStatus.php" method="post">, the server receives a POST request. Unless you have explicitly populated hidden inputs within the modal with the user_id before the form submission, the target script will see an empty or null value for that parameter. The data exists in the URL only until the page reloads, and even then, it is not automatically transferred into the form fields of a subsequent modal instance if the modal content is loaded dynamically.

To resolve this, you must bridge the gap between the link click and the form submission using JavaScript. There are two primary architectural approaches to handle this pattern effectively: passing the ID as a hidden input or utilizing a JavaScript object to pre-populate the modal fields.

Passing via Hidden Input

The most robust method involves creating a mechanism to store the user_id temporarily when the button is clicked and then retrieving it when the form submits.

First, ensure your modal contains a hidden input field capable of holding the ID:

<div id="myModal" class="modal">
    <div class="modal-content">
        <!-- Other content -->
        <form class="form-login" action="action/doEditUserStatus.php" method="post">
            <input type="hidden" name="user_id" id="hidden_user_id" required>
            
            <div class="changestatus">
                <p>Banning or Activing User Status</p>
            </div>

            <div class="btn-group" data-toggle="buttons">
                <label class="btn btn-success active">
                    <input type="radio" name="options" value="1" autocomplete="off" checked> Active
                </label>
                <label class="btn btn-primary">
                    <input type="radio" name="options" value="2" autocomplete="off"> Banned
                </label>
            </div>

            <div class="modal-footer">
                <button data-dismiss="modal" class="btn btn-default" type="button">Cancel</button>
                <button class="btn btn-theme" name="modalSubmit" type="submit">Submit</button>
            </div>
        </form>
    </div>
</div>

Next, modify your button to use a JavaScript event listener instead of relying on the href attribute for navigation. This prevents the page from reloading and allows you to inject the data into the hidden field before showing the modal:

<a href="#" 
   class="btn btn-warning" 
   onclick="openModal(<?php echo $user['user_id']; ?>)">
    <span class="glyphicon glyphicon-edit" aria-hidden="true"></span>&nbsp;Change Status
</a>

Implement the JavaScript logic to handle the ID injection and modal display. This ensures the user_id is available in the POST request when the form is submitted:

function openModal(userId) {
    // Inject the user ID into the hidden input
    document.getElementById('hidden_user_id').value = userId;
    
    // Show the modal
    var myModal = new bootstrap.Modal(document.getElementById('myModal'));
    myModal.show();
}

Handling Dynamic Content

If you are using a framework like Bootstrap 3 or older versions, you might need to adjust the initialization slightly. The core principle remains the same: do not rely on the URL fragment to populate form fields. Use the query parameter ?user_id=... only if your backend logic explicitly reads it upon initial page load and writes it into the hidden input (e.g., using PHP's $_GET['user_id'] in the modal template).

However, relying solely on the URL fragment (#myModal) is ineffective for data transfer. If you must stick to a direct link approach without JavaScript manipulation of the button click, you would need to use AJAX to fetch the modal content specifically for that user ID before displaying it. This adds complexity regarding caching and state management.

The hidden input method described above is generally preferred because it decouples the visual trigger from the data transfer mechanism, making your code easier to maintain and debug. It ensures that action/doEditUserStatus.php receives the correct identifier regardless of how the user arrived at the modal.

Our network