我想在这个论坛上寻求人们的专业建议,这些建议是我花了几天时间试图解决但却无济于事。
你看,我发布后无法在表单中检索所选选项的值,即使我能看到chrome developer中发布的值,它也会返回空白。我尝试删除iframe标记,然后立即看到select标记中的已发布值。我希望保持iframe标签不变,以便我的表单值在发布后保持不变。在保留iframe标记的同时,我尝试在标记之后移动php标记,但发布的选择值仍为空白。我想知道使用jquery自动填充select选项是否会导致任何问题。
请参阅下面的代码。谢谢。
<html>
<head>
<meta charset="utf-8">
<style>
.hide{ position:absolute; top:-1px; left:-1px; width:1px; height:1px; }
</style>
<link rel="stylesheet" href="css/datepicker.css">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="jquery-ui.css">
<script src="jquery.js"></script>
<script src="jquery-ui.js"></script>
</head>
<body>
<iframe name="hiddenFrame" class="hide"></iframe>
<form method="post" action="" target="hiddenFrame">
<p><label class="field" for="empdob">DateOfBirth:</label>Day<select name="dobday" id="dobday"></select>Month<select name="dobmonth" id ="dobmonth"></select>Year<select name="dobyear" id="dobyear"></select></p>
<p><input type="submit" name="submit" value="Submit"></p>
</form>
<?php
if(!empty($_POST['dobday'])) {
$dobday=$_POST['dobday'];
echo $_POST['dobday'];
}
if(!empty($_POST['dobmonth'])) {
$dobmonth=$_POST['dobmonth'];
echo $_POST['dobmonth'];
}
if(!empty($_POST['dobyear'])){
$dobyear=$_POST['dobyear'];
echo $_POST['dobyear'];
}
if((!empty($_POST['dobyear'])) && (!empty($_POST['dobmonth'])) && (!empty($_POST['dobday']))) {
$datecombine=$dobday.$dobmonth.$dobyear;
echo $datecombine;
$dobemp=DateTime::createFromFormat('jnY', $datecombine);
echo $dobemp->format('Y-m-d');
}
?>
<script>
$(function() {
var startyr = new Date().getFullYear();
var endyr = startyr - 100;
//populate our years select box
for (i = startyr; i > endyr; i--){
$('#dobyear').append($('<option />').val(i).html(i));
}
//populate our months select box
for (i = 1; i < 13; i++){
$('#dobmonth').append($('<option />').val(i).html(i));
}
//populate our Days select box
updateNumberOfDays();
//"listen" for change events
$('#dobyear, #dobmonth').change(function(){
updateNumberOfDays();
});
});
//function to update the days based on the current values of month and year
function updateNumberOfDays(){
$('#dobday').html('');
month = $('#dobmonth').val();
year = $('#dobyear').val();
days = daysInMonth(month, year);
for(i=1; i < days+1 ; i++){
$('#dobday').append($('<option />').val(i).html(i));
}
}
//helper function
function daysInMonth(month, year) {
return new Date(year, month, 0).getDate();
}
</script>
</body>
</html>