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

Bootstrap提示消息失效:表单提交跳转至PHP页而非原地显示

Fixing Contact Form Message Display (No Page Redirect)

Hey there! Let’s get that contact form sorted so your success/error messages show up right in the .messages div on your original page, instead of jumping to contact.php. The core issue here is that your current form uses a standard synchronous submit, which forces a page reload. We’ll fix this with a little vanilla JavaScript (no extra libraries needed) to handle AJAX submission, and make tiny tweaks to your setup.

Step 1: Tweak the Form Tag

First, update your form’s opening tag to remove the action attribute (our JS will handle sending data to contact.php directly). Keep the id since we’ll target it in our script:

<form id="contact-form" method="post" role="form">

Step 2: Add AJAX Handling JavaScript

Paste this script right before the closing </body> tag on your request-interpreter.html page (or put it in an external JS file if you prefer):

document.addEventListener('DOMContentLoaded', function() {
  const form = document.getElementById('contact-form');
  const messagesContainer = document.querySelector('.messages');

  form.addEventListener('submit', function(e) {
    // Stop the default page reload when submitting the form
    e.preventDefault();

    // Collect all form data
    const formData = new FormData(form);

    // Send data to contact.php via AJAX
    fetch('contact.php', {
      method: 'POST',
      body: formData
    })
    .then(response => response.json())
    .then(data => {
      // Clear any old messages
      messagesContainer.innerHTML = '';

      // Create a styled message element
      const alertElement = document.createElement('div');
      alertElement.classList.add('alert', data.type === 'success' ? 'alert-success' : 'alert-danger');
      alertElement.textContent = data.message;

      // Add the message to the .messages div
      messagesContainer.appendChild(alertElement);

      // Optional: Reset the form if submission was successful
      if (data.type === 'success') {
        form.reset();
      }
    })
    .catch(error => {
      // Handle network or unexpected errors
      messagesContainer.innerHTML = '<div class="alert alert-danger">Oops, something went wrong. Please try again later.</div>';
      console.error('Submission error:', error);
    });
  });
});

This code waits for the page to load, listens for the form submit event, sends the data to your PHP script in the background, then displays the response message directly in your .messages container. I used Bootstrap-style alert classes here—adjust the class names if your site uses different styling!

Step 3: Verify Your PHP Code

Great news: Your existing contact.php already has built-in support for AJAX requests! The only thing to double-check is the $sendTo line. Right now it’s set to $_POST['email'], which sends the form submission email to the address the user entered. If you want the email sent to your own address instead, update that line to:

$sendTo = 'your-email@example.com'; // Replace with your actual email

Everything else in the PHP code is good to go.

Step 4: Optional Style for Messages

If you don’t already have styles for alert messages, add this CSS to make them look clean:

.alert {
  padding: 1rem;
  margin: 1rem 0;
  border-radius: 4px;
  border: 1px solid transparent;
}
.alert-success {
  background-color: #d4edda;
  color: #155724;
  border-color: #c3e6cb;
}
.alert-danger {
  background-color: #f8d7da;
  color: #721c24;
  border-color: #f5c6cb;
}

That’s all! Now when users submit the form, the page won’t reload, and the success or error message will appear right where you want it.

内容的提问来源于stack exchange,提问作者Christian Luneborg

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:49:15