我该如何着手修复我的PHP计算器

mwg9r5ms  于 2023-02-11  发布在  PHP
关注(0)|答案(1)|浏览(97)

嘿,伙计们,我正在为学校的PHP计算器工作,我似乎不能让它工作。我不擅长PHP,不能搞清楚。当我运行它的会员资格和计算总数,无论我选择什么选项,它会自动切换到黄金会员资格。有什么想法吗?下面是代码:

  1. <?php
  2. $tax = 0;
  3. $total = 0;
  4. $membership = "gold";
  5. $tennis = "no";
  6. $racquetball = "no";
  7. $golf = "no";
  8. $child_care = "no";
  9. $personal_trainer = "no";
  10. $swimming_pool = "no";
  11. function test_input($data) {
  12. $data = trim($data);
  13. $data = stripslashes($data);
  14. $data = htmlspecialchars($data);
  15. return $data;
  16. }
  17. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  18. if (!empty($_POST["tax"])) {
  19. $tax = test_input($_POST["tax"]);
  20. }
  21. if ($membership == "basic") {
  22. $total = 80;
  23. }
  24. elseif ($membership == "platinum") {
  25. $total = 100;
  26. }
  27. else {
  28. $total = 120;
  29. }
  30. if (isset($_POST["tennis"])) {
  31. $tennis = "yes";
  32. $total = $total + 15;
  33. }
  34. if (isset($_POST["racquetball"])) {
  35. $racquetball = "yes";
  36. $total = $total + 20;
  37. }
  38. if (isset($_POST["golf"])) {
  39. $golf = "yes";
  40. $total = $total + 25;
  41. }
  42. if (isset($_POST["child_care"])) {
  43. $child_care = "yes";
  44. $total = $total + 15;
  45. }
  46. if (isset($_POST["personal_trainer"])) {
  47. $personal_trainer = "yes";
  48. $total = $total + 20;
  49. }
  50. if (isset($_POST["swimming_pool"])) {
  51. $swimming_pool = "yes";
  52. $total = $total + 25;
  53. }
  54. $total = $total + $total * $tax;
  55. $total = round($total, 2);
  56. }
  57. ?>
  58. <html>
  59. <head>
  60. <title>Health Club (PHP)</title>
  61. </head>
  62. <body style="padding: 30px">
  63. <h2>Health Club (PHP)</h2>
  64. Franklin Covington <p>
  65. <form method="post" name="healthClubForm" id="healthClubForm"
  66. action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  67. <div style="float:left; width:130px; background-color:pink;">
  68. <dl>
  69. <dt>Membership
  70. <dt><input type="radio" name="membership" onchange="reloadForm"
  71. <?php if (isset($membership) && $membership=="basic") echo "checked";?>
  72. value="basic"> Basic
  73. <dt><input type="radio" name="membership" onchange="reloadForm"
  74. <?php if (isset($membership) && $membership=="platinum") echo "checked";?>
  75. value="platinum"> Platinum
  76. <dt><input type="radio" name="membership" onchange="reloadForm"
  77. <?php if (isset($membership) && $membership=="gold") echo "checked";?>
  78. value="Gold"> Gold
  79. </dl>
  80. </div>
  81. <div style="float:left; width:180px; background-color:yellow;">
  82. <dl>
  83. <dt>Additional Charges (1)
  84. <dt><input type="checkbox" onchange="reloadForm"
  85. <?php if (isset($tennis) && $tennis=="yes") echo "checked";?>
  86. name="tennis"> Tennis
  87. <dt><input type="checkbox" onchange="reloadForm"
  88. <?php if (isset($racquetball) && $racquetball=="yes") echo "checked";?>
  89. name="racquetball"> Racquetball
  90. <dt><input type="checkbox" onchange="reloadForm"
  91. <?php if (isset($golf) && $golf=="yes") echo "checked";?>
  92. name="golf"> Golf
  93. </dl>
  94. </div>
  95. <div style="float:left; width:180px; background-color:red;">
  96. <dl>
  97. <dt>Additional Charges (2)
  98. <dt><input type="checkbox" onchange="reloadForm"
  99. <?php if (isset($child_care) && $child_care=="yes") echo "checked";?>
  100. name="child_care"> Child Care
  101. <dt><input type="checkbox" onchange="reloadForm"
  102. <?php if (isset($personal_trainer) && $personal_trainer=="yes") echo "checked";?>
  103. name="personal_trainer"> Personal Trainer
  104. <dt><input type="checkbox" onchange="reloadForm"
  105. <?php if (isset($swimming_pool) && $swimming_pool=="yes") echo "checked";?>
  106. name="swimming_pool"> Swimming Pool
  107. </dl>
  108. </div>
  109. <div style="clear:both">&nbsp;</div>
  110. <div style="float:left; width:150px; background-color:lightcoral;">
  111. <dl>
  112. <dt><input type="submit" value="Calculate Total">
  113. <dt><input type="submit" value="Clear">
  114. </dl>
  115. </div>
  116. <div style="float:left; background-color:lightgreen;">
  117. <dl>
  118. <dt>Tax: <input type="text" onchange="reloadForm" name="tax" value="<?php echo $tax;?>" size="10">
  119. <dt>Total: <input type="text" name="total" value="<?php echo $total;?>" size="10">
  120. </dl>
  121. </div>
  122. <script>
  123. function reloadForm() {
  124. document.getElementById("healthClubForm").submit();
  125. }
  126. </script>`your text`
  127. </form>
  128. </body>
  129. </html>

我看了看我买的金子,看起来我没有做错什么。

eit6fx6z

eit6fx6z1#

因为您在第4行将会员价值写为黄金,提交后漏调用会员发帖请求

  1. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  2. if (!empty($_POST["tax"])) {
  3. $tax = test_input($_POST["tax"]);
  4. }
  5. $membership=$_POST["membership"];
  6. if ($membership == "basic") {
  7. $total = 80;
  8. }

添加此行$membership=$_POST[“成员资格”];

相关问题