WAMP环境下PHP表单GET方法异常,点击提交下载PHP文件
Hey, let's work through this issue step by step. When your welcome_get.php file downloads instead of running after form submission, it means your WAMP server isn't configured to parse PHP code—your browser is treating it like a regular static file instead. Here's how to fix this, plus a quick tweak to your form code that's causing another hidden issue:
1. 确认WAMP服务正常运行
- Open your WAMP manager (the icon in your system tray). Make sure the icon is green—if it's orange, that means Apache or MySQL isn't running.
- If it's not green, right-click the icon, hover over
Apacheand selectStart Service, then do the same forMySQLif needed. Wait for the icon to turn green before moving on.
2. 确保Apache加载了PHP模块
- Right-click the WAMP icon, go to
Apache > Modules. - Look for an entry like
phpX.X.X(replace X.X.X with your installed PHP version, e.g.,php8.2.0). Make sure this option is checked. - If it wasn't checked, click to select it, then restart Apache via
Apache > Restart Service.
3. 验证Apache配置文件中的PHP关联设置
- From the WAMP icon, select
Apache > httpd.confto open the main Apache config file. - Search for these lines (they might be scattered in the file):
LoadModule php_module "${INSTALL_DIR}/bin/php/phpX.X.X/php8apache2_4.dll" AddHandler application/x-httpd-php .php PHPIniDir "${INSTALL_DIR}/bin/php/phpX.X.X" - Ensure none of these lines start with a
#(which would comment them out). If they do, remove the#at the start of each line. - Save the file, then restart Apache again.
4. 检查文件位置和访问方式
- Make sure both your HTML form file and
welcome_get.phpare saved in WAMP's root directory: this is usuallywamp64/www(for 64-bit WAMP) orwamp/www. - Always access your site via
http://localhost/your-file-name.html(replaceyour-file-namewith your actual HTML file name) instead of double-clicking the local file directly.
Bonus: Fix Your Form Code (It Won't Pass Data Right Now!)
Your current HTML form has two small issues that will prevent PHP from getting the email address, even after fixing the WAMP problem:
- The email input doesn't have a
nameattribute—PHP uses thename(notid) to retrieve form data. - You don't need to close
<input>tags (they're self-closing in HTML).
Here's the corrected form code:
<form action="welcome_get.php" method="get"> <section class="contact bg-primary" id="contact"> <div class="container"> <h2>Abonnez vous par mail!</h2> <input type="email" id="email" name="email" size="50" placeholder="Entrez votre adresse e-mail"> </div> <div class="mail-submit"> <input type="submit" id="submit" value="Je m'abonne"> </div> </section> </form>
And here's a more robust version of your PHP code (to avoid errors if no email is submitted):
<html> <body> <?php if (isset($_GET["email"])) { // Use htmlspecialchars to prevent XSS attacks echo "Your email address is: " . htmlspecialchars($_GET["email"]); } else { echo "No email address submitted!"; } ?> </body> </html>
Once you've gone through all these steps, reload your form page via localhost, submit the email, and your PHP file should execute correctly instead of downloading.
内容的提问来源于stack exchange,提问作者FrenchyRomain

