Collecting data from different dropdown menus php

筅森魡賤 提交于 2019-12-21 03:02:46

问题


In my html form I have 3 dropdown menus for date of birth, day menu, month menu and year menu.

I want to collect day value from day menu,
month value from month menu,
year from year menu.

in php file I wrote for collecting the date of birth data; but it didn't work.

$date = $_POST['day'] . "-" . $_POST['month'] . "-" . $_POST['year'];

Help please.


回答1:


a simple example for you too learn from :)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>date example</title>
</head>
<body>

<?php
if(isset($_POST['day']) && isset($_POST['month']) && isset($_POST['year'])){
    echo 'You selected: '.$_POST['day'].'-'.$_POST['month'].'-'.$_POST['year'];
}
?>

<form method="POST" action="">
  <p><select size="1" name="day">
  <?php formDay(); ?>
  </select>-
  <select size="1" name="month">
  <?php formMonth(); ?>
  </select>-
  <select size="1" name="year">
  <?php formYear(); ?>
  </select> <input type="submit" value="Submit"></p>
</form>
</body>
</html>

<?php
//functions to loop day,month,year
function formDay(){
    for($i=1; $i<=31; $i++){
        echo '<option value="'.$i.'">'.$i.'</option>'."\n";
    }
}

function formMonth(){
    $month = strtotime('2011-01-01');
    $end = strtotime('2012-01-01');
    while($month < $end){
        echo '<option value="'.date('F', $month).'">'.date('F', $month).'</option>'."\n";
        $month = strtotime("+1 month", $month);
    }
}

function formYear(){
    for($i=1980; $i<=date('Y'); $i++){
        echo '<option value="'.$i.'">'.$i.'</option>'."\n";
    }
}

?>



回答2:


Well, it seems either you didn't post the data (did you use action="post"?) or the selects don't have a name attribute. But if neither fixes solve your problem, maybe you could post the rest of your PHP code and your HTML code too.



来源:https://stackoverflow.com/questions/6164333/collecting-data-from-different-dropdown-menus-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!