2017-02-27 50 views
-6

我在这里有一个小问题。我有一个列表,其中:如何获得用户在php中选择的值?

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

我需要做的是,当用户选择=“一个月”的价值,PHP代码会做一个动作,而当用户选择值=“天”, PHP代码会做另一个动作?

我的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 [“选择”],但它不工作。感谢您的帮助球员

+2

你至少尝试一下这事呢?使用谷歌,我马上得到这个:http://stackoverflow.com/questions/17139501/using-post-to-get-select-option-value-from-html – domsson

+8

可能重复的[使用$ \ _ POST获取选择选项值来自HTML](http://stackoverflow.com/questions/17139501/using-post-to-get-select-option-value-from-html) – domsson

回答

0

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

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

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"; 
} 
+0

我已经改变它,但它仍然是一样的。当我选择值=“月”,代码打印一张表,但没有任何信息在里面,其他值打印相同... :( – user7435747

+0

更新的代码与更多的修复程序。做一些基本的调试。 –