javascript 检查下拉菜单并根据该选择创建 if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8562714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:49:18  来源:igfitidea点击:

Check drop down and create if statement based on that selection

phpjavascriptjquery

提问by Rob

I want to create a php variable depending on what was selected in a drop down.

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

This is the drop down:

这是下拉:

  <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>

Depending if "per hour" or "per annum" was selected, I want to do the following (I'm not quite sure syntax wise if this is correct and this part is on another page):

根据选择的是“每小时”还是“每年”,我想执行以下操作(如果这是正确的并且这部分在另一页上,我不太确定语法明智):

// 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;

How can this be done?

如何才能做到这一点?

Here's my full form:

这是我的完整表格:

<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>

回答by Timur

<select id="ddlTime" name="ddlTime">

and

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;
}

回答by Rob

I hope this help you :

我希望这对你有帮助:

<?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>