Collecting data from different dropdown menus php

2020-06-06 07:35发布

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.

2条回答
The star\"
2楼-- · 2020-06-06 08:08

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.

查看更多
唯我独甜
3楼-- · 2020-06-06 08:16

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

?>
查看更多
登录 后发表回答