HTML+PHP表单提交后显示空白页面问题求助
Let's walk through why your contact form is showing a blank page when submitted, and fix it up properly:
1. Critical Mismatch in Form Field Name
Your HTML uses name="mail" for the email input, but your PHP code checks for isset($_POST['email']) — this condition will never be true, so all the email-sending logic gets skipped entirely, leaving you with a blank page.
Fix:
Update the PHP condition to check for the correct field name:
if(isset($_POST['mail']) && !empty($_POST['mail'])){
2. Invalid Email Header Format
Your email headers use plain line breaks instead of the required \r\n (RFC-compliant line endings for email headers). Also, there's a typo: X=Mailer should be X-Mailer.
Fix:
Rewrite the header line with proper formatting:
$header = "From:info@diegodiasp.com\r\n" ."Reply-To:".$email."\r\n" ."X-Mailer:PHP/".phpversion();
3. Enable Error Reporting for Debugging (Critical for Live Sites)
Live servers often disable error display by default, which hides issues that cause blank pages. Add these lines at the top of your contactform.php to see what's going wrong:
error_reporting(E_ALL); ini_set('display_errors', 1);
Once you've fixed the issues, you can remove these lines (or set display_errors to 0 for production).
4. Improved Security & Readability
- Replace
addslashes()withhtmlspecialchars()to prevent XSS risks when handling user input:$name = htmlspecialchars($_POST['name']); $email = htmlspecialchars($_POST['mail']); $message = htmlspecialchars($_POST['message']); - Use
PHP_EOLfor line breaks in the email body (cross-platform compatible):$body = "Name: ".$name.PHP_EOL ."Email: ".$email.PHP_EOL ."Message: ".$message;
Full Corrected PHP Code
<?php error_reporting(E_ALL); ini_set('display_errors', 1); if(isset($_POST['mail']) && !empty($_POST['mail'])){ $name = htmlspecialchars($_POST['name']); $email = htmlspecialchars($_POST['mail']); $message = htmlspecialchars($_POST['message']); $to = "info@diegodiasp.com"; $subject = "Contact - Diego Dias Front End"; $body = "Name: ".$name.PHP_EOL ."Email: ".$email.PHP_EOL ."Message: ".$message; $header = "From:info@diegodiasp.com\r\n" ."Reply-To:".$email."\r\n" ."X-Mailer:PHP/".phpversion(); if(mail($to,$subject,$body,$header)){ echo("Email successfully sent!"); }else{ echo("Email not sent!"); } } ?>
Additional Notes
- If the mail still fails to send, check your hosting provider's mail configuration — some shared hosts require you to use a verified sender email that matches your domain.
- Consider adding form validation on the frontend (e.g., required attributes, email format checks) to catch issues before submission:
<input class="form-control" name="name" placeholder="Your Name" required> <input class="form-control" name="mail" placeholder="Your Email" type="email" required>
内容的提问来源于stack exchange,提问作者Diego Dias




