PHP健身俱乐部计算器故障:会员自动切换为Gold问题求助
健身俱乐部计算器会员类型计算错误修复
问题根源
- 未读取用户选择的会员类型:代码初始化时
$membership = "gold";,但在POST请求处理逻辑中,从未从$_POST["membership"]获取用户实际选择的会员类型,导致计算时始终沿用初始值"gold"。 - 字符串大小写不匹配:HTML中Gold会员的
value属性是"Gold"(首字母大写),但PHP判断条件里用的是小写"gold",即便获取到值也会匹配失败,依然进入默认的Gold会员分支。 - Clear按钮功能错误:原Clear按钮为
type="submit",点击会提交表单而非清空内容。 - 函数调用语法错误:HTML中
onchange="reloadForm"缺少括号,无法正确触发表单提交函数。
修复步骤
- 获取用户选择的会员类型:在POST处理块开头添加代码,读取并处理用户选择的会员类型:
if (!empty($_POST["membership"])) { $membership = test_input($_POST["membership"]); }
- 统一字符串大小写:将HTML中Gold会员的
value改为小写"gold",与PHP判断条件保持一致:
<input type="radio" name="membership" onchange="reloadForm()" <?php if (isset($membership) && $membership=="gold") echo "checked";?> value="gold"> Gold
- 修复Clear按钮:将Clear按钮的
type从submit改为reset,实现清空表单功能:
<input type="reset" value="Clear">
- 修正函数调用:将所有
onchange="reloadForm"改为onchange="reloadForm()",确保函数正常执行。
修改后的完整代码
<?php $tax = 0; $total = 0; $membership = "gold"; $tennis = "no"; $racquetball = "no"; $golf = "no"; $child_care = "no"; $personal_trainer = "no"; $swimming_pool = "no"; function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } if ($_SERVER["REQUEST_METHOD"] == "POST") { if (!empty($_POST["tax"])) { $tax = test_input($_POST["tax"]); } // 新增:获取用户选择的会员类型 if (!empty($_POST["membership"])) { $membership = test_input($_POST["membership"]); } if ($membership == "basic") { $total = 80; } elseif ($membership == "platinum") { $total = 100; } else { $total = 120; } if (isset($_POST["tennis"])) { $tennis = "yes"; $total = $total + 15; } if (isset($_POST["racquetball"])) { $racquetball = "yes"; $total = $total + 20; } if (isset($_POST["golf"])) { $golf = "yes"; $total = $total + 25; } if (isset($_POST["child_care"])) { $child_care = "yes"; $total = $total + 15; } if (isset($_POST["personal_trainer"])) { $personal_trainer = "yes"; $total = $total + 20; } if (isset($_POST["swimming_pool"])) { $swimming_pool = "yes"; $total = $total + 25; } $total = $total + $total * $tax; $total = round($total, 2); } ?> <html> <head> <title>Health Club (PHP)</title> </head> <body style="padding: 30px"> <h2>Health Club (PHP)</h2> Franklin Covington <p> <form method="post" name="healthClubForm" id="healthClubForm" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <div style="float:left; width:130px; background-color:pink;"> <dl> <dt>Membership <dt><input type="radio" name="membership" onchange="reloadForm()" <?php if (isset($membership) && $membership=="basic") echo "checked";?> value="basic"> Basic <dt><input type="radio" name="membership" onchange="reloadForm()" <?php if (isset($membership) && $membership=="platinum") echo "checked";?> value="platinum"> Platinum <dt><input type="radio" name="membership" onchange="reloadForm()" <?php if (isset($membership) && $membership=="gold") echo "checked";?> value="gold"> Gold </dl> </div> <div style="float:left; width:180px; background-color:yellow;"> <dl> <dt>Additional Charges (1) <dt><input type="checkbox" onchange="reloadForm()" <?php if (isset($tennis) && $tennis=="yes") echo "checked";?> name="tennis"> Tennis <dt><input type="checkbox" onchange="reloadForm()" <?php if (isset($racquetball) && $racquetball=="yes") echo "checked";?> name="racquetball"> Racquetball <dt><input type="checkbox" onchange="reloadForm()" <?php if (isset($golf) && $golf=="yes") echo "checked";?> name="golf"> Golf </dl> </div> <div style="float:left; width:180px; background-color:red;"> <dl> <dt>Additional Charges (2) <dt><input type="checkbox" onchange="reloadForm()" <?php if (isset($child_care) && $child_care=="yes") echo "checked";?> name="child_care"> Child Care <dt><input type="checkbox" onchange="reloadForm()" <?php if (isset($personal_trainer) && $personal_trainer=="yes") echo "checked";?> name="personal_trainer"> Personal Trainer <dt><input type="checkbox" onchange="reloadForm()" <?php if (isset($swimming_pool) && $swimming_pool=="yes") echo "checked";?> name="swimming_pool"> Swimming Pool </dl> </div> <div style="clear:both"> </div> <div style="float:left; width:150px; background-color:lightcoral;"> <dl> <dt><input type="submit" value="Calculate Total"> <dt><input type="reset" value="Clear"> </dl> </div> <div style="float:left; background-color:lightgreen;"> <dl> <dt>Tax: <input type="text" onchange="reloadForm()" name="tax" value="<?php echo $tax;?>" size="10"> <dt>Total: <input type="text" name="total" value="<?php echo $total;?>" size="10"> </dl> </div> <script> function reloadForm() { document.getElementById("healthClubForm").submit(); } </script> </form> </body> </html>
内容的提问来源于stack exchange,提问作者fskiddles
相关产品推荐
相关产品推荐

