选中下拉列表并基于该选择创建 if 语句


Check drop down and create if statement based on that selection

我想根据下拉列表中选择的内容创建一个php变量。

这是下拉菜单:

  <td width="5" rowspan="2"><select id="ddlTime">
      <option selected="selected" value="perhour" name="perhour">per hour</option>
      <option value="perannum" name="perannum">per annum</option>
    </select></td>

根据选择了"每小时"还是"每年",我想执行以下操作(我不太确定语法是否正确,并且这部分在另一页上(:

// if per hour is selected:
$result_pharmacist = $_POST["pharmacist"];
$result_dispenser = $_POST["dispenser"];
// if per annum is selected:
$user_pharmacist = $_POST["pharmacist"];
$result_pharmacist = $user_pharmacist/37.5/52;
$user_dispenser = $_POST["dispenser"];
$result_dispenser = $user_dispenser/37.5/52;

如何做到这一点?

这是我的完整表格:

<form action="<?php the_permalink(); ?>calculations" method="post">
  <h2>Savings calculator</h2>
  <div class="calculator-divide"></div>
  <table border="0">
    <tr>
      <td colspan="3"><h3>Current service costs</h3></td>
    </tr>
    <tr>
      <td width="440"><p>Pharmacist</p></td>
      <td><p style="padding-left:5px!IMPORTANT;">&pound;
          <input style="width:145px!IMPORTANT;" value="22.00" type="text" name="pharmacist" />
        </p></td>
      <td width="5" rowspan="2"><select id="ddlTime">
          <option selected="selected" value="perhour" name="perhour">per hour</option>
          <option value="perannum" name="perannum">per annum</option>
        </select></td>
    </tr>
    <tr>
      <td><p>Dispenser / Technician</p></td>
      <td><p style="padding-left:5px!IMPORTANT;">&pound;
          <input style="width:145px!IMPORTANT;" value="8.00" type="text" name="dispenser" />
        </p></td>
    </tr>
    <tr>
      <td colspan="3">&nbsp;</td>
    </tr>
    <tr>
      <td colspan="3"><h3>Time taken to carry out manual dispensing tasks</h3></td>
    </tr>
    <tr>
      <td><p>Measure 50mls dose by hand including Pharmacist check</p></td>
      <td colspan="2"><p style="padding-left:5px!IMPORTANT;">
          <input value="1" type="text" name="measure-check" />
          Minute(s)</p></td>
    </tr>
    <tr>
      <td><p>Preparing labels from dispensary system</p></td>
      <td colspan="2"><p style="padding-left:5px!IMPORTANT;">
          <input value="0.5" type="text" name="labels" />
          Minute(s)</p></td>
    </tr>
    <tr>
      <td><p>Write up CD register</p></td>
      <td colspan="2"><p style="padding-left:5px!IMPORTANT;">
          <input value="2" type="text" name="cd-register" />
          Minute(s)</p></td>
    </tr>
    <tr>
      <td></td>
      <td colspan="3"><div class="estimate">
          <input style="margin-bottom:20px;" type="submit" value="Estimate my savings" />
        </div></td>
    </tr>
  </table>
</form>
<select id="ddlTime" name="ddlTime">

if( $_POST['ddlTime']=='perhour' ){
    // if per hour is selected:
    $result_pharmacist = $_POST["pharmacist"];
    $result_dispenser = $_POST["dispenser"];
}elseif( $_POST['ddlTime']=='perannum' ){
    // if per annum is selected:
    $user_pharmacist = $_POST["pharmacist"];
    $result_pharmacist = $user_pharmacist/37.5/52;
    $user_dispenser = $_POST["dispenser"];
    $result_dispenser = $user_dispenser/37.5/52;
}

我希望这对您有所帮助:

<?php
if(isset ($_POST['save']))
{
    $temp = $_POST['opsi'];
    echo $temp;
}
?>
<html>
    <body>
        <form action="newEmptyPHP.php" method="POST">
         <h3> Choose Your Option</h3>
         <select name="opsi">
                <option value=0 selected>- Customer -</option>
                 <option value="perannum" name="perannum">perannum</option>
                  <option value="perhour" name="perhour">perhour</option>
         </select>    <br> <br>
         <input type="submit" name="save" value="Save and Commit">
        </form>
    </body>
</html>