使用VS Code运行connect.php时HTTP 405错误的解决方法
解决XAMPP注册表单提交HTTP 405错误
问题描述
使用XAMPP + phpMyAdmin创建注册表单,提交时返回:
此页面无法正常工作。如果问题持续,请联系网站管理员。HTTP错误405
系统:Mac
可能的原因及解决方案
1. 必须通过HTTP协议访问页面
直接双击打开HTML文件会使用file://协议,这种方式无法触发PHP的POST请求处理,必然导致405错误。
- 将HTML和PHP文件放在XAMPP的
htdocs目录下(比如/Applications/XAMPP/htdocs/bloom/) - 在浏览器中通过
localhost/bloom/register.html访问表单,而非直接打开本地文件。
2. 验证文件路径与权限
- 确认
connect.php和HTML文件在同一目录下,表单的action="connect.php"路径正确。 - 给项目文件夹设置正确权限:
打开终端执行命令:
或者右键项目文件夹→显示简介→共享与权限,将权限设置为所有用户可读可执行。sudo chmod -R 755 /Applications/XAMPP/htdocs/你的项目文件夹名
3. 检查Apache服务与PHP运行状态
- 打开XAMPP控制面板,确保Apache服务已启动。
- 直接访问
localhost/bloom/connect.php,若页面显示PHP相关错误(如Undefined index),说明PHP能正常运行;若显示404,说明文件路径错误。
4. 明确表单按钮类型
给注册按钮添加type="submit"属性,避免浏览器默认行为异常:
<button type="submit">Register</button>
5. 检查Apache配置是否限制POST方法
打开XAMPP的Apache配置文件(/Applications/XAMPP/etc/httpd.conf),搜索是否存在限制POST方法的规则(如<LimitExcept GET>),若有需确保POST方法被允许。
你的代码参考
HTML代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Sign Up - Bloom of Psyche</title> <link rel="stylesheet" type="text/css" href="register-form.css"> </head> <body> <section> <div class="formbox"> <div class="form-value"> <form method="post" action="connect.php"> <h2>SIGN UP</h2> <div class="inputbox"> <input type="text" id="first_name" name="firstName" required> <label for="">First Name</label> </div> <div class="inputbox"> <input type="text" id="last_name" name="lastName" required> <label for="">Last Name</label> </div> <div class="inputbox"> <input type="email" id="email_address" name="email" required> <label for="">Email</label> </div> <div class="inputbox"> <input type="password" id="user_password" name="password" required> <label for="">Password</label> </div> <button type="submit">Register</button> <div class="login"> <p>Already have an account? <a href="login-form.html">Log in</a></p> </div> </form> </div> </div> </section> <script type="module" src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.esm.js"></script> <script nomodule src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.js"></script> </body> </html>
PHP代码
<?php $firstName = $_POST['firstName']; $lastName = $_POST['lastName']; $email = $_POST['email']; $password = $_POST['password']; $conn = new mysqli('localhost', 'root', '', 'bloomofpsyche_database'); if($conn->connect_error){ die('Connection Failed : '.$conn->connect_error); }else{ $stmt = $conn->prepare("insert into registration(firstName, lastName, email, password) values(?, ?, ?, ?)"); $stmt->bind_param("ssss", $firstName, $lastName, $email, $password); $stmt->execute(); echo "You have successfully registered!"; $stmt->close(); $conn->close(); } ?>
内容的提问来源于stack exchange,提问作者Shanaya
相关产品推荐
相关产品推荐

