2016-11-16 70 views
2

數據我有一個用戶填寫並擊中形式「保存&下一步」,這將帶他們到另一個頁面,用戶可以上傳圖片,並點擊「提交最終」。他們還可以回到以前的頁面編輯數據。自動選擇<select><options>標籤

當時,所有他以前填補了數據應在文本框中顯示。我使用會話變量來存儲數據並顯示它。

我卡在下拉框中。

<select name="District"> 
     <option value="East">East</option> 
     <option value="West">West</option> 
     <option value="North">North</option> 
     <option value="South">South</option> 
</select> 

當用戶提交,我在會議$_SESSION['District'] = $_POST['District'];存儲選擇的值,當用戶點擊回走的前一頁,我需要自動選擇在下拉菜單中該選項的值了。

我該如何做到這一點?在會議上,如果設置attibute比賽

回答

2

使用array會更容易。

<?php 
$options = array(
    'East', 'West', 'North', 'South', 
); 
?> 
<select name="District"> 
    <?php foreach($options as $option) { ?> 
     <option value="<?php echo $option; ?>" <?php echo (isset($_SESSION['District']) && $_SESSION['District'] == $option) ? "selected" : "" ?>><?php echo $option; ?></option> 
    <?php } ?> 
</select> 
+0

所有問題的答案是正確的你它的容易得多快。豎起大拇指 –

+0

謝謝哥們......這是一個在所有正確的最簡單的! –

1

校驗值選擇

<select name="District"> 
     <option <?php if (!empty($_POST['District']) && $_POST['District'] == 'East'){ echo 'selected'; }?> value="East">East</option> 
     <option <?php if (!empty($_POST['District']) && $_POST['District'] == 'West'){ echo 'selected'; }?> value="West">West</option> 
     <option <?php if (!empty($_POST['District']) && $_POST['District'] == 'North'){ echo 'selected'; }?> value="North">North</option> 
     <option <?php if (!empty($_POST['District']) && $_POST['District'] == 'South'){ echo 'selected'; }?> value="South">South</option> 
</select> 
1

只是檢查對一個存儲在會話中選擇的值。 您的選項將是這樣的:

<option value="East" <?php echo ($_SESSION['District']=="East" ? "selected" : ""; ?>>East</option> 

而且

1
<select name="District"> 
    <option value="East" <?php if($_SESSION['District'] == "East"):?>selected="selected"<?php endif; ?>>East</option> 
    ... Repeat with all options ... 
1

使用下面的代碼的權利會被選中:

<select name="District"> 
     <option value="East" <?php echo ($_SESSION['District'] == "East") ? "selected" : "" ?>>East</option> 
     <option value="West" <?php echo ($_SESSION['District'] == "West") ? "selected" : "" ?>>West</option> 
     <option value="North" <?php echo ($_SESSION['District'] == "North") ? "selected" : "" ?>>North</option> 
     <option value="South" <?php echo ($_SESSION['District'] == "South") ? "selected" : "" ?>>South</option> 
</select> 
1
<option value="East" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'East' ? 'selected="selected"' :'' ;?> >East</option> 
<option value="West" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'West' ? 'selected="selected"' :'' ;?>>West</option> 
<option value="North" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'North' ? 'selected="selected"' :'' ;?>>North</option> 
<option value="South" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'South' ? 'selected="selected"' :'' ;?>>South</option> 
相關問題