如果我提交表单,如果选择框在while循环中,我怎么能将值传递给process.php页面。有没有解决方案或选择?
<form role="form" method="post" action="process.php" >
<?php
$endDate = '2014-01-28';
$startDate = '2014-01-27';
$daydiff = floor( ( strtotime( $endDate ) - strtotime( $startDate ) ) / 86400 );
$x=0;
while($x<=$daydiff)
{
?>
<div class="form-group">
<label for="day_name">Day Name</label>
<select class="form-control" name="day_name">
<option value="monday">Monday</option>
<option value="tuesday">Tuesday</option>
<option value="wednesday">Wednesday</option>
<option value="thursday">Thursday</option>
</select>
</div>
<div class="form-group">
<label for="time">time</label>
<select class="form-control" name="time">
<option value="10am">10am</option>
<option value="11am">11am</option>
<option value="12pm">12pm</option>
<option value="1pm">1pm</option>
</select>
</div>
<?php
$x++;
}
?>
<button type="submit" name="submit">Submit</button>
</form>
答案 0 :(得分:0)
首先通过在选择框名称的末尾添加数字来更改HTML代码。 然后添加新的隐藏变量以保持选择框的计数。
$endDate = $_POST['enddate'];
$startDate = $_POST['startdate'];
$daydiff = floor(( strtotime($endDate) - strtotime($startDate) ) / 86400);
$x = 0;
while ($x <= $daydiff) {
?>
<select class="form-control" name="day_<?php echo $x; ?>">
<option value="monday">Monday</option>
<option value="tuesday">Tuesday</option>
<option value="wednesday">Wednesday</option>
<option value="thursday">Thursday</option>
</select>
<select class="form-control" name="time_<?php echo $x; ?>">
<option value="10am">10am</option>
<option value="11am">11am</option>
<option value="12pm">12pm</option>
<option value="1pm">1pm</option>
</select>
<?php
$x++;
}
?>
<input type="hidden" name="count" value="<?php echo $x ?>"/>
在表单提交中使用此代码来获取所需的数据。
for ($i = 0; $i < $_POST['count']; $i++) {
$day_var = 'day_' . $i;
$days_list[] = $_POST[$day_var];
$time_var = 'time_' . $i;
$time_list[] = $_POST[$time_var];
}
print_r($days_list);
print_r($time_list);
答案 1 :(得分:0)
您没有获得该值,因为在While循环中创建的所有<select>
框具有相同的名称。请更改这些选择框的名称。尝试下面的代码来呈现<select>
框:
<?php
$endDate = $_POST['enddate'];
$startDate = $_POST['startdate'];
$daydiff = floor( ( strtotime( $endDate ) - strtotime( $startDate ) ) / 86400 );
$x=0;
while($x<=$daydiff)
{?>
<select class="form-control" name="day_name_<?php echo $x;?>">
<option value="monday">Monday</option>
<option value="tuesday">Tuesday</option>
<option value="wednesday">Wednesday</option>
<option value="thursday">Thursday</option>
</select>
<select class="form-control" name="time_<?php echo $x;?>">
<option value="10am">10am</option>
<option value="11am">11am</option>
<option value="12pm">12pm</option>
<option value="1pm">1pm</option>
</select>
<?php
$x++;
}?>
现在,通过保留$daydiff
变量值
答案 2 :(得分:0)
$selectOption = $_POST['day_name'];
<select class="form-control" name="day_name">
<option value="monday">Monday</option>
<option value="tuesday">Tuesday</option>
<option value="wednesday">Wednesday</option>
<option value="thursday">Thursday</option>
</select>