表单提交报错Cannot POST /get.php及PHP代码直接返回问题求助
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
htdocsfolder. On Windows, this is usuallyC:\xampp\htdocs; on Mac, it’s/Applications/XAMPP/htdocs. Create a subfolder (likecontact-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.phpin thecontact-formsubfolder, go tohttp://localhost/contact-form/send-email.phpin 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
methodandactionattributes: Make sure your HTML form hasmethod="post"(since your PHP checks for$_POST['submit']) and theactionpoints to the correct path of your PHP file. For example:
If your form and PHP logic are in the same file, set<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>action=""oraction="<?php echo $_SERVER['PHP_SELF']; ?>"instead. - Verify your submit button has the right
name: Your PHP usesisset($_POST['submit'])to trigger form handling, so your submit button must havename="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:
Option 1: Use PHPMailer (Recommended)
PHPMailer is a popular library that makes sending emails easier and more reliable. Here’s how to use it:
- Download PHPMailer, extract it into your project folder (e.g.,
contact-form/phpmailer/). - 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 selectphp.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
- First, test if PHP is working: Create a file named
test.phpwith<?php phpinfo(); ?>inside, access it viahttp://localhost/contact-form/test.php—you should see a PHP info page if everything’s set up right. - Then, test your form submission without email code first: Add a simple
echo "Form submitted successfully!";inside theisset($_POST['submit'])block to confirm the form reaches the PHP file. - Once that works, add the email logic.
内容的提问来源于stack exchange,提问作者Justin Peterson

