You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关闭现有Modal窗口后打开新弹窗:脚本添加位置咨询

How to Open a New Modal After Closing an Existing One (With PHP Redirect)

Hey there! Let's work through how to get that new modal to pop up right after closing your existing enquiry modal, especially with your current PHP redirect setup. Here's a straightforward breakdown:

1. Keep Your Backend Logic (Use Session Status as a Trigger)

Your existing PHP redirect with the success status is exactly what we need to signal the frontend to act. Keep this code in your controller—it’ll pass the success message to the next page:

return redirect('enquiries')->with('status', 'Enquiry added successfully');

2. Add Frontend Script to the Redirect Target Page

Since the redirect reloads the enquiries page, we’ll check for that session status when the page loads. Paste this script into your enquiries view file (like enquiries.blade.php)—put it at the bottom of the page or in a dedicated scripts section:

<script>
document.addEventListener('DOMContentLoaded', function() {
  // Pull the success status from the PHP session
  const successMessage = "<?php echo session('status') ?? ''; ?>";
  
  if (successMessage) {
    // First, close the existing enquiry modal if it’s still open
    const existingModal = document.getElementById('enquiryModal'); // Replace with your modal’s actual ID
    if (existingModal) {
      // If using Bootstrap modals, use this to close it:
      bootstrap.Modal.getInstance(existingModal)?.hide();
      // If you have a custom close function, use that instead:
      // closeEnquiryModal();
    }

    // Add a small delay to ensure the old modal fully closes (smoother user experience)
    setTimeout(() => {
      // Call your new modal’s trigger function here
      openYourNewModal(); // Swap this with your actual new modal function name
      
      // Optional: Pass the success message to the new modal if needed
      // openYourNewModal(successMessage);
    }, 300);

    // Clear the session status so it doesn’t re-trigger on page refresh
    <?php session()->forget('status'); ?>
  }
});
</script>

3. Alternative: Handle Frontend Modal Close Events (For Non-Redirect Flows)

If you ever need to open a new modal when the user manually closes the enquiry modal (without a backend redirect), you can listen for the modal’s close event. For example, with Bootstrap modals:

const enquiryModal = document.getElementById('enquiryModal');
enquiryModal.addEventListener('hidden.bs.modal', function() {
  // Open your new modal immediately after the old one closes
  openYourNewModal();
});

But since your current flow uses a PHP redirect, the first method is the perfect fit for your scenario.


内容的提问来源于stack exchange,提问作者Sid Menon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:46:20