循环出来的Select选项问题

WBOY
Release: 2016-06-23 14:22:11
Original
1076 people have browsed it

select php

选择月份:		<select name="month">			<?php 			for($i="1"; $i<="12"; $i++){				if($i<10){					echo "<option>0".$i."月</option><br>";				}else{					echo "<option>".$i."月</option><br>";				}			} 			?>		</select>
Copy after login

这个月份是通过For循环出来的,默认是选择1月份,当选择其它月份再点提交后,又显示回1月份了,有什么办法可以停留在当前选择的月份呢?


回复讨论(解决方案)

这样写

<select name="month"><?php $m = 5; // 假定选中的是 5 月for($i="1"; $i<="12"; $i++){  printf("<option value=%d %s>%02d月</option>", $i, $m == $i ? ' selected' : '', $i);}?></select>
Copy after login
Copy after login
Copy after login

这样写

<select name="month"><?php $m = 5; // 假定选中的是 5 月for($i="1"; $i<="12"; $i++){  printf("<option value=%d %s>%02d月</option>", $i, $m == $i ? ' selected' : '', $i);}?></select>
Copy after login
Copy after login
Copy after login

我选择8月,点提交后怎么又回到5月了??

你难道不赋值吗?

你难道不赋值吗?
谢谢,value=%d %s>%02d月 这一句是什么意思呢?

这样写

<select name="month"><?php $m = 5; // 假定选中的是 5 月for($i="1"; $i<="12"; $i++){  printf("<option value=%d %s>%02d月</option>", $i, $m == $i ? ' selected' : '', $i);}?></select>
Copy after login
Copy after login
Copy after login

选择仓库:		<select name="depot">			<?php			$ckSQL = "SELECT MC001,MC002 FROM {$dataBase}CMSMC";			$ckQuery = sqlsrv_query($conn,$ckSQL);			if( $ckQuery === false) {				die( print_r( sqlsrv_errors(), true) );			}			$ck = $_POST["depot"];			while($ckRow = sqlsrv_fetch_array($ckQuery)) {					//echo "<option>".$ckRow[1]."</option>";				printf("<option >%s</option>", $ckRow[1], $ck == $ckRow[1] ? ' selected' : '', $ckRow[1]);			}			sqlsrv_free_stmt( $query);			?>			<option value="全部" <?php if($_POST["depot"] == "全部"){echo 'selected="selected"';} ?>>全部</option>		</select>
Copy after login
为什么这一段按照你的方法改就不管用了呢?
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template