2011-12-19 41 views
1

我想創建一個php變量,具體取決於在下拉菜單中選擇的內容。檢查下拉菜單並根據該選擇創建if語句

這是下拉:

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

回答

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

我希望這可以幫助您:

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