【问题标题】:Options can be selected from php选项可以从php中选择
【发布时间】:2018-11-14 20:31:08
【问题描述】:

我正在处理一个 PHP 项目,到目前为止,它看起来应该是: 该网站应该提供他们选择的木材的实际尺寸
我的问题是,无论他们选择什么,我都会得到3/4" x 1-1/2"(如果他们点击英寸)或19 x 38 mm(如果他们点击指标)。 我熟悉一点编码,但我不是最擅长的,我希望能得到尽可能多的帮助。 这是我的 HTML 输入页面的代码:

<!DOCTYPE HTML>
<html lang="en" style="text-align:center; zoom:150%;">
  <head>
    <meta charset="utf-8">
    <title> Final - Lumber</title>
  </head>

  <body style="background-color: lightgrey;">
    <h1 style="color:white; background-color:black;">
      <?php
        //Includes the code to connect to the server
        //include_once 'includes/dbc.php';
      ?>
    </h1>
    <p> Please complete this form to submit your input: </p>

    <p><span style="color:#FF0000">* required filed</span></p>
    <form method="post" action="handler.php">
      <p>
        <label> What size of lumber would you want?
          <select name="size" required>
            <option value="NULL" disabled selected> -------- </option>
            <option value="1"> 1" X 2" </option>
            <option value="2"> 1" X 3" </option>
            <option value="3"> 1" X 4" </option>
            <option value="4"> 1" X 5" </option>
            <option value="5"> 1" X 6" </option>
            <option value="6"> 1" X 7" </option>
            <option value="7"> 1" X 8" </option>
            <option value="8"> 1" X 10" </option>
            <option value="9"> 1" X 12" </option>
            <option value="10"> 2" X 4" </option>
            <option value="11"> 2" X 6" </option>
            <option value="12"> 2" X 8" </option>
            <option value="13"> 2" X 10" </option>
            <option value="14"> 2" X 12" </option>
            <option value="15"> 3" X 6" </option>
            <option value="16"> 4" X 4" </option>
            <option value="17"> 4" X 6" </option>
          </select>
        </label>
          <span style="color:#FF0000">*</span>
      </p>

      <p>
        <label> Choose how you would like the actual sizes displayed?
        </label>
          <span style="color:#FF0000">*</span>
      </p>

      <input type="submit" name="inches" value="Inches">
      <input type="submit" name="metric" value="Metric">
    </form>
  </body>
</html>

这是我的 handler.php 代码,假设为他们选择的木材提供正确的尺寸

<?php
      //connects to the server
      //include_once 'includes/dbc.php';

      //Renaming values to make it easier to write
      $size = isset($_POST['size']);
      $metric = isset($_POST['metric']);
      $inches = isset($_POST['inches']);

      //If they click on the inches button
      if ($inches)
      {
        switch ($size) {
          case '$_POST["1"]':
            echo 'The actual size of a 1" x 2"<br><br>';
            echo '<span style="color:blue;">3/4" x 1-1/2"</span<br/>';
            break;
          case '$_POST["2"]':
            echo 'The actual size of a 1" x 3"<br><br>';
            echo '<span style="color:blue;">3/4” x 2-1/2”</span<br/>';
            break;
          case '3':
            echo 'The actual size of a 1" x 4"<br><br>';
            echo '<span style="color:blue;">3/4” x 3-1/2”</span<br/>';
            break;
          case '4':
            echo 'The actual size of a 1" x 5"<br><br>';
            echo '<span style="color:blue;">3/4” x 4-1/2”</span<br/>';
            break;
          case '5':
            echo 'The actual size of a 1" x 6"<br><br>';
            echo '<span style="color:blue;">3/4” x 5-1/2”</span<br/>';
            break;
          case '6':
            echo 'The actual size of a 1" x 7"<br><br>';
            echo '<span style="color:blue;">3/4” x 6-1/4”</span<br/>';
            break;
          case '7':
            echo 'The actual size of a 1" x 8"<br><br>';
            echo '<span style="color:blue;">3/4” x 7-1/4”</span<br/>';
            break;
          case '8':
            echo 'The actual size of a 1" x 10"<br><br>';
            echo '<span style="color:blue;">3/4” x 9-1/4”</span<br/>';
            break;
          case '9':
            echo 'The actual size of a 1" x 12"<br><br>';
            echo '<span style="color:blue;">3/4” x 11-1/4”</span<br/>';
            break;
          case '10':
            echo 'The actual size of a 2" x 4"<br><br>';
            echo '<span style="color:blue;">1-1/2” x 3-1/2”</span<br/>';
            break;
          case '11':
            echo 'The actual size of a 2" x 6"<br><br>';
            echo '<span style="color:blue;">1-1/2” x 5-1/2”</span<br/>';
            break;
          case '12':
            echo 'The actual size of a 2" x 8"<br><br>';
            echo '<span style="color:blue;">1-1/2” x 7-1/4”</span<br/>';
            break;
          case '13':
            echo 'The actual size of a 2" x 10"<br><br>';
            echo '<span style="color:blue;">1-1/2” x 9-1/4”</span<br/>';
            break;
          case '14':
            echo 'The actual size of a 2" x 12"<br><br>';
            echo '<span style="color:blue;">1-1/2” x 11-1/4”</span<br/>';
            break;
          case '15':
            echo 'The actual size of a 3" x 6"<br><br>';
            echo '<span style="color:blue;">2-1/2” x 5-1/2” </span<br/>';
            break;
          case '16':
            echo 'The actual size of a 4" x 4"<br><br>';
            echo '<span style="color:blue;">3-1/2” x 3-1/2” </span<br/>';
            break;
          case '17':
            echo 'The actual size of a 4" x 6"<br><br>';
            echo '<span style="color:blue;">3-1/2” x 5-1/2” </span<br/>';
            break;
          }

      }
      elseif ($metric) {
        switch ($size) {
          case '$_POST["1"]':
            echo 'The actual size of a 1" x 2"<br><br>';
            echo '<span style="color:blue;">19 x 38 mm</span<br/>';
            break;
          case '$_POST["2"]':
            echo 'The actual size of a 1" x 3"<br><br>';
            echo '<span style="color:blue;">3/4” x 2-1/2”</span<br/>';
            break;
          case '3':
            echo 'The actual size of a 1" x 4"<br><br>';
            echo '<span style="color:blue;">3/4” x 3-1/2”</span<br/>';
            break;
          case '4':
            echo 'The actual size of a 1" x 5"<br><br>';
            echo '<span style="color:blue;">3/4” x 4-1/2”</span<br/>';
            break;
          case '5':
            echo 'The actual size of a 1" x 6"<br><br>';
            echo '<span style="color:blue;">3/4” x 5-1/2”</span<br/>';
            break;
          case '6':
            echo 'The actual size of a 1" x 7"<br><br>';
            echo '<span style="color:blue;">3/4” x 6-1/4”</span<br/>';
            break;
          case '7':
            echo 'The actual size of a 1" x 8"<br><br>';
            echo '<span style="color:blue;">3/4” x 7-1/4”</span<br/>';
            break;
          case '8':
            echo 'The actual size of a 1" x 10"<br><br>';
            echo '<span style="color:blue;">3/4” x 9-1/4”</span<br/>';
            break;
          case '9':
            echo 'The actual size of a 1" x 12"<br><br>';
            echo '<span style="color:blue;">3/4” x 11-1/4”</span<br/>';
            break;
          case '10':
            echo 'The actual size of a 2" x 4"<br><br>';
            echo '<span style="color:blue;">1-1/2” x 3-1/2”</span<br/>';
            break;
          case '11':
            echo 'The actual size of a 2" x 6"<br><br>';
            echo '<span style="color:blue;">1-1/2” x 5-1/2”</span<br/>';
            break;
          case '12':
            echo 'The actual size of a 2" x 8"<br><br>';
            echo '<span style="color:blue;">1-1/2” x 7-1/4”</span<br/>';
            break;
          case '13':
            echo 'The actual size of a 2" x 10"<br><br>';
            echo '<span style="color:blue;">1-1/2” x 9-1/4”</span<br/>';
            break;
          case '14':
            echo 'The actual size of a 2" x 12"<br><br>';
            echo '<span style="color:blue;">1-1/2” x 11-1/4”</span<br/>';
            break;
          case '15':
            echo 'The actual size of a 3" x 6"<br><br>';
            echo '<span style="color:blue;">2-1/2” x 5-1/2” </span<br/>';
            break;
          case '16':
            echo 'The actual size of a 4" x 4"<br><br>';
            echo '<span style="color:blue;">3-1/2” x 3-1/2” </span<br/>';
            break;
          case '17':
            echo 'The actual size of a 4" x 6"<br><br>';
            echo '<span style="color:blue;">3-1/2” x 5-1/2” </span<br/>';
            break;
        }
      }


     ?>

【问题讨论】:

    标签: php switch-statement


    【解决方案1】:

    我的想法是你将$size 变量转换为布尔值。

    $size = (isset($_POST['size'])) ? $_POST['size'] : 0; //<-Whatever value you want if it's not set
    

    【讨论】:

      猜你喜欢
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多