如何获取用户在php中选择的值?

时间:2017-02-27 11:49:44

标签: php mysql

我这里有点问题。我有一个清单,即:

<form action="display.php" method="post">
  <select name="holiday">
    <option value="month">Kiekvieno mėnesio skaičiavimas</option>
    <option value="day">Kiekvienos dienos skaičiavimas</option>
  </select>
  <br> <br>
  <input type="submit" name="select" value="Pasirinkti" />
</form>

我需要做的是当用户选择value =&#34; month&#34;时,php代码会做一个动作,当用户选择value =&#34; day&#34 ;, php code会做另一个动作吗?

我的php代码如下所示:

<?php
if($_POST['select']){
  // Storing selected value in a variable
  $selectedValue= $_POST['holiday'];
}
else if ($selectedValue == $_POST['month']) {
  $todaysDate = new DateTime();
  while ($employee = $select->fetch()){
    $employmentDateValue = new DateTime($employee['employment_date']);
    // Comparing employment date with today's date
    $differenceBetweenDates = date_diff($employmentDateValue, $todaysDate);
    $workMonths = $differenceBetweenDates->y * 12 + $differenceBetweenDates->m;
    $holidayDays = $workMonths *4;
    echo "<tr>";
    echo "<td>".$employee['name']."</td>";
    echo "<td>".$employee['surname']."</td>";
    echo "<td>".$employee['employment_date']."</td>";
    echo "<td>".$workMonths."</td>";
    echo "<td>".$holidayDays."</td>";
    echo "</tr>";
  }
}
else {
  echo "Lalalala";
}
?>

我已尝试使用$ _POST [&#39; select&#39;],但它无法正常工作。谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

您需要执行$_POST['holiday'],因此请更改:

if($_POST['select']){
  // Storing selected value in a variable
  $selectedValue= $_POST['holiday'];
}

if($_POST['holiday']){
  // Storing selected value in a variable
  $selectedValue = $_POST['holiday'];
}

您还需要更改一行:

else if ($selectedValue == $_POST['month']) {

所以它不是原始if声明的一部分:

if ($selectedValue == 'month') {
  // your code
}
else {
  echo "Lalalala";
}

答案 1 :(得分:0)

<?php
if($_POST['select']){
  // Storing selected value in a variable
  $selectedValue= $_POST['holiday'];

if ($selectedValue == 'month') {
 
}
else if ($selectedValue == 'day') {
 
}
else{
  echo "Lalalala";
}
}
?>