Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 循环出来的Select选项问题

循环出来的Select选项问题

Jun 23, 2016 pm 02:22 PM

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
为什么这一段按照你的方法改就不管用了呢?
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles