-6

I have a little problem here. I have a list, which is:

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

What I need to do is that when the user selects value ="month", php code would do one action, and when the user selects value ="day", php code would do another action?

My php code looks like this:

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

I've tried to do so with $_POST['select'], but it doesn't work. Thanks for any help guys

Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98
user7435747
  • 417
  • 2
  • 5
  • 11
  • 2
    Did you at least try to look that up? Using Google, I immediately got this: http://stackoverflow.com/questions/17139501/using-post-to-get-select-option-value-from-html – domsson Feb 27 '17 at 11:51
  • 8
    Possible duplicate of [Using $\_POST to get select option value from HTML](http://stackoverflow.com/questions/17139501/using-post-to-get-select-option-value-from-html) – domsson Feb 27 '17 at 11:52

2 Answers2

0

You need to do $_POST['holiday'], so change:

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

to

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

You also need to change the line:

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

So it's not part of the original if statement:

if ($selectedValue == 'month') {
  // your code
}
else {
  echo "Lalalala";
}
Niraj Shah
  • 15,087
  • 3
  • 41
  • 60
  • I've changed it, but it remains the same. When i select value="month", the code prints a table, but without any information inside of it and the other value prints the same... :( – user7435747 Feb 27 '17 at 11:56
  • Updated code with more fixes. Do some basic debugging. – Niraj Shah Feb 27 '17 at 12:01
0

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

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