2015-05-20 133 views
-1

這就是我在編輯表單時顯示細節的方式,但是出現錯誤。在數據庫的下拉菜單中顯示選定的值

<select id="progcode" name="progcode"> 
    <option value="0">-none-</option> 
    <option value="1" <?php if($progcode == '1') echo 'selected="selected"' ?>>PreS1-AB</option> 
    <option value="2" <?php if($progcode == '2') echo 'selected="selected"' ?> >PreS1-MM</option> 
    <option value="3" <?php if($progcode == '3') echo 'selected="selected"' ?> >TutEng</option> 
</select> 

任何建議將不勝感激。

+0

什麼錯誤? $ progcode定義在哪裏?當選擇第二個或第三個選項時,你想在'option'上用'progcode'屬性做什麼? – panther

+0

我在頁面頂部定義了它。 – Tapash

+0

你會得到什麼錯誤? – redelschaap

回答

0

正確的格式是給予吹---這是工作正常,,,,,, 謝謝大家建議。

<select id="progcode" name="progcode"> 
         <option value="0">-none-</option> 
         <option value="1" <?php if($row["progcode"]==1) echo 'selected'; ?>>PreS1-AB</option> 
         <option value="2" <?php if($row["progcode"]==2) echo 'selected'; ?> >PreS1-MM</option> 
         <option value="3" <?php if($row["progcode"]==3) echo 'selected'; ?>>TutEng</option> 
        </select> 
0
//here the simple way 
<?php 
$servername = "localhost"; 
$user = "root"; 
$password = ""; 
$db = "ajaxcall"; 

$con = mysqli_connect($servername, $user, $password, $db); 
if (!$con) { 
die("Connection failed: " . mysqli_connect_error()); 
} 
?> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title></title> 
</head> 
<body> 
<select name="name"> 
<option value=""> -----------ALL----------- </option> 
<?php 
$query = "select DISTINCT id from crud"; 
$result = mysqli_query($con, $query); 
while ($rows = mysqli_fetch_array($result)) { 
echo "<option value='$rows[0]'> $rows[0] </option>"; 
} 
?> 
</select> 
</body> 
</html> 

<!DOCTYPE html> 
 
<!-- 
 
To change this license header, choose License Headers in Project Properties. 
 
To change this template file, choose Tools | Templates 
 
and open the template in the editor. 
 
--> 
 
<?php 
 
$servername = "localhost"; 
 
$user = "root"; 
 
$password = ""; 
 
$db = "ajaxcall"; 
 

 
$con = mysqli_connect($servername, $user, $password, $db); 
 
if (!$con) { 
 
    die("Connection failed: " . mysqli_connect_error()); 
 
} 
 
?> 
 
<html> 
 
    <head> 
 
     <meta charset="UTF-8"> 
 
     <title></title> 
 
    </head> 
 
    <body> 
 
     <select name="name"> 
 
      <option value=""> -----------ALL----------- </option> 
 
      <?php 
 
      $query = "select DISTINCT id from crud"; 
 
      $result = mysqli_query($con, $query); 
 
      while ($rows = mysqli_fetch_array($result)) { 
 
       echo "<option value='$rows[0]'> $rows[0] </option>"; 
 
      } 
 
      ?> 
 
     </select> 
 
    </body> 
 
</html>

相關問題