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

表单提交报错Cannot POST /get.php及PHP代码直接返回问题求助

Troubleshooting Your PHP Form & XAMPP Issues

Hey there! Let's break down your problems step by step—this is super common when starting out with PHP and XAMPP, so don't stress. I'll walk you through each issue and fix them one by one.

1. Why Your PHP Code Is Showing Instead of Running

If your browser displays raw PHP code (like <?php if (isset($_POST['submit']))...), you're not accessing your files through XAMPP's web server. Here's how to fix that:

  • First, open your XAMPP Control Panel and start the Apache service (you’ll see a green "Running" indicator when it’s active).
  • Move all your form files (HTML and PHP) into XAMPP’s htdocs folder. On Windows, this is usually C:\xampp\htdocs; on Mac, it’s /Applications/XAMPP/htdocs. Create a subfolder (like contact-form) here to keep things organized.
  • Access your files using the local server URL, not by double-clicking the file. For example, if your PHP file is named send-email.php in the contact-form subfolder, go to http://localhost/contact-form/send-email.php in your browser.

2. Fixing the "Cannot POST" Error

This error happens when your form can’t find the PHP file it’s supposed to submit to, or the request method doesn’t match. Let’s check these:

  • Check your form’s method and action attributes: Make sure your HTML form has method="post" (since your PHP checks for $_POST['submit']) and the action points to the correct path of your PHP file. For example:
    <form method="post" action="send-email.php">
      <!-- Your form fields (name, email, message, etc.) go here -->
      <input type="submit" name="submit" value="Send Message">
    </form>
    
    If your form and PHP logic are in the same file, set action="" or action="<?php echo $_SERVER['PHP_SELF']; ?>" instead.
  • Verify your submit button has the right name: Your PHP uses isset($_POST['submit']) to trigger form handling, so your submit button must have name="submit" (like the example above—don’t skip this!).

3. Sending Form Content to an Email

The default mail() function in XAMPP often doesn’t work out of the box because the mail server isn’t configured. Here are two reliable ways to fix this:

PHPMailer is a popular library that makes sending emails easier and more reliable. Here’s how to use it:

  1. Download PHPMailer, extract it into your project folder (e.g., contact-form/phpmailer/).
  2. Use this sample code in your PHP file to handle the form and send the email:
    <?php
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    require 'phpmailer/src/Exception.php';
    require 'phpmailer/src/PHPMailer.php';
    require 'phpmailer/src/SMTP.php';
    
    if (isset($_POST['submit'])) {
        // Get form data (replace with your actual field names)
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
    
        $mail = new PHPMailer(true);
    
        try {
            // Server settings (update with your email provider's details)
            $mail->isSMTP();
            $mail->Host = 'smtp.your-email-provider.com'; // e.g., smtp.gmail.com for Gmail
            $mail->SMTPAuth = true;
            $mail->Username = 'your-email@example.com'; // Your email address
            $mail->Password = 'your-email-password'; // For Gmail, use an app password if 2FA is enabled
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
            $mail->Port = 465;
    
            // Recipients
            $mail->setFrom('your-email@example.com', 'Your Name');
            $mail->addAddress('recipient@example.com'); // The email you want to send submissions to
    
            // Email content
            $mail->isHTML(true);
            $mail->Subject = 'New Form Submission from ' . $name;
            $mail->Body = "Name: $name<br>Email: $email<br>Message: $message";
    
            $mail->send();
            echo 'Message has been sent successfully!';
        } catch (Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
        }
    }
    ?>
    

Option 2: Configure XAMPP's Mercury Mail

If you want to use the built-in mail server:

  • Start Mercury in the XAMPP Control Panel.
  • Open php.ini (click the "Config" button next to Apache in XAMPP, then select php.ini).
  • Find the [mail function] section and update these lines:
    SMTP=localhost
    smtp_port=25
    sendmail_from=your-email@example.com
    
  • Restart Apache, then you can use the mail() function. Note: This only works for local emails or servers that accept unauthenticated mail, so it’s less reliable for external recipients.

Quick Testing Checklist

  1. First, test if PHP is working: Create a file named test.php with <?php phpinfo(); ?> inside, access it via http://localhost/contact-form/test.php—you should see a PHP info page if everything’s set up right.
  2. Then, test your form submission without email code first: Add a simple echo "Form submitted successfully!"; inside the isset($_POST['submit']) block to confirm the form reaches the PHP file.
  3. Once that works, add the email logic.

内容的提问来源于stack exchange,提问作者Justin Peterson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:39:26